简体   繁体   English

使用autofac SingleInstance在对wcf服务的每个请求上注入不同的标头值

[英]Injecting different header value upon each request to wcf service using autofac SingleInstance

I've googled a lot and none of the answers seem to be answering my question, hopefully it'll not be a duplicate. 我已经在Google上搜索了很多,但所有答案似乎都无法回答我的问题,希望它不会重复。

I'm working on a service which I would not rather rebuild completely and keep it as it was and just implement my part of a deal into it. 我正在开发一项服务,我不想完全重建并保持原样,而只是将交易的一部分实施到其中。

There is a service which has an instance of a wcf gateway created by autofac, it is a SingleInstance() as such: 有一个服务,其中有一个由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();
    }

I've read about OperationContextScope() to manipulate headers but since this gateway instance is registered by autofac I am unable to cast appropriately to IContextChannel. 我已经读过关于OperationContextScope()来处理标头的信息,但是由于此网关实例是由autofac注册的,因此无法正确地转换为IContextChannel。

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

Such cast gives me an exception since instance of desiredGateway is wrapped in some kind of container, which is not IContextChannel, but once I create my own instance of a desiredGateway using channel.CreateChannel() I am able to cast to IContextChannel. 这样的转换为我带来了一个例外,因为desireGateway的实例包装在某种容器中,而不是IContextChannel,但是一旦我使用channel.CreateChannel()创建了自己的desireGateway实例,便可以将其转换为IContextChannel。

Target is to be able to inject a header value upon each call to desiredGateway, is there any way to achieve this without rebuilding existing implementation too much? Target将能够在每次调用desirableGateway时注入标头值,有什么方法可以在不过度重建现有实现的情况下实现这一目标? Maybe there exists a cleaner way to achieve the above? 也许存在一种更清洁的方法来实现上述目标?

So I ended up removing those lines from gateway registration: 因此,我最终从网关注册中删除了这些行:

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

This allowed me to then cast to IContextChannel and I implemented interseption process within a class that owned an instance of the gateway using a generic method. 这样,我便可以转换为IContextChannel,并在使用通用方法的拥有网关实例的类中实现了跨节处理。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM