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?