【问题标题】:Injecting different header value upon each request to wcf service using autofac SingleInstance使用 autofac SingleInstance 对 wcf 服务的每个请求注入不同的标头值
【发布时间】:2018-10-27 02:06:57
【问题描述】:

我用谷歌搜索了很多,但似乎没有一个答案能回答我的问题,希望它不会重复。

我正在开发一项服务,我不想完全重建并保持原样,只是将我的交易部分实施到其中。

有一个服务有一个由 autofac 创建的 wcf 网关实例,它是一个 SingleInstance() :

public static void RegisterMyService(ContainerBuilder builder)
    {
        builder.Register(c => new DesiredGatewayInterceptor());
        builder
            .Register(
                c =>
                {
                    const string BindingName = "BasicHttpBinding_My_PortType";
                    Uri endpointAddress = null;
                    ClientSection servicesSection = (ClientSection)WebConfigurationManager.GetSection("system.serviceModel/client");
                    foreach (ChannelEndpointElement endpoint in servicesSection.Endpoints)
                    {
                        if (endpoint.Name == BindingName)
                        {
                            endpointAddress = endpoint.Address;
                            break;
                        }
                    }

                    ChannelFactory<DesiredGateway> channel = new ChannelFactory<DesiredGateway>(
                        new BasicHttpBinding(BindingName),
                        new EndpointAddress(endpointAddress));

                    NameValueCollection section = (NameValueCollection)ConfigurationManager.GetSection("CredentialsConfiguration");

                    channel.Credentials.UserName.UserName = section["DesiredGatewayUser"];
                    channel.Credentials.UserName.Password = section["DesiredGatewayPassword"];
                    return channel;
                })
            .SingleInstance();

        builder
            .Register(c => c.Resolve<ChannelFactory<DesiredGateway>>().CreateChannel())
            .InterceptTransparentProxy(typeof(DesiredGateway))
            .InterceptedBy(typeof(DesiredGatewayInterceptor))
            .UseWcfSafeRelease();
    }

我已阅读有关 OperationContextScope() 来操作标头的信息,但由于此网关实例是由 autofac 注册的,因此我无法正确地转换为 IContextChannel。

using (OperationContextScope scope = new OperationContextScope((IContextChannel)desiredGateway))
{
   // Do some stuff with headers now
}

这样的转换给了我一个例外,因为desiredGateway 的实例被包装在某种容器中,这不是IContextChannel,但是一旦我使用channel.CreateChannel() 创建了我自己的desiredGateway 实例,我就可以转换为IContextChannel。

目标是能够在每次调用desiredGateway 时注入一个标头值,有没有什么方法可以在不过度重建现有实现的情况下实现这一点?也许存在一种更清洁的方法来实现上述目标?

【问题讨论】:

  • 你首先想做什么?为什么要修改标题? WCF 表示 SOAP 和 WS-。消息结构是标准化的(这就是 WS- 的用途)并且不受解释的影响。例如,已经支持 WS-Authentication。您是否尝试连接到非标准服务,例如 ebXML?在这种情况下,您需要使用 WCF 的 自己的机制来拦截和格式化消息。这不是 DI 容器的工作。您可以将您的客户端配置为使用您自己的 IClientMessageFormatter,或者覆盖操作并使用您自己的消息标头
  • 代码的第二部分是来自具有 wcf 网关实例的服务中的方法内的示例,客户端点击端点服务方法,然后指向网关方法,并在该端点内服务方法我想调整一个标题,例如,客户端用户 XYZ 命中一个端点,然后这个用户名需要作为标题传递给网关。

标签: c# wcf autofac


【解决方案1】:

所以我最终从网关注册中删除了这些行:

.InterceptTransparentProxy(typeof(DesiredGateway))
.InterceptedBy(typeof(DesiredGatewayInterceptor))

这使我可以转换为 IContextChannel,然后我在一个拥有网关实例的类中使用通用方法实现了 interseption 过程。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-03-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多