简体   繁体   中英

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.

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:

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.

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.

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? 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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