0
votes

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?

1
What are you trying to do in the first place? Why modify headers? WCF means SOAP and WS-. The message structure is standardized (that's what the WS- are for) and not subject to interpretation. WS-Authentication for example is already supported. Are you trying to connect to a non-standard service perhaps, like ebXML? In that case you need to use WCF's own mechanisms to intercept and format messages. That's not a job for the DI container. You could configure your client to use your own IClientMessageFormatter, or override operations and use your own message HeadersPanagiotis Kanavos
The 2nd part of a code is sample from within a method in a service which has an instance of wcf gateway, a client hits an endpoint service method which then points to a gateway method, and within that endpoint service method I would like to adjust a header for instance, clients user XYZ hits an endpoint and then this user name needs to be passed to gateway as a header.KlapekApokalipsy

1 Answers

0
votes

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.