We have a requirement to send an authentication cookie across when making a request from a client proxy to each of our WCF service methods. The code to create the cookie works fine but I'm trying to modify the Autofac service registration code to add a new endpoint behaviour that will add the cookie to the request header. I'm using Fiddler as a proxy to see whether the cookie is added to the request.
Here's what I have so far, which doesn't work:
private static void RegisterService<TService>(ContainerBuilder builder)
{
builder.Register(c => new ChannelFactory<TService>(string.Format("BasicHttpBinding_{0}", typeof(TService).Name))).SingleInstance();
builder.Register(c =>
{
var channel = c.Resolve<ChannelFactory<TService>>();
if (!channel.Endpoint.Behaviors.Contains(typeof(SamsTrustBehaviour)))
{
channel.Endpoint.Behaviors.Add(new SamsTrustBehaviour());
}
return channel.CreateChannel();
}).UseWcfSafeRelease();
}
I can pass a dependency into my client proxy code and call a method that adds the SamsTrustBehaviour
to the endpoint when each request is made, and that works fine. Ideally though, I'd prefer to do this at registration time. Here's the code that works, which is called initially upon each request to the service:
public void AttachSamsToken<T>(T serviceInterface) where T : class
{
var clientBase = serviceInterface as ClientBase<T>;
if (clientBase != null && !clientBase.Endpoint.Behaviors.Contains(typeof(SamsTrustBehaviour)))
{
clientBase.Endpoint.Behaviors.Add(new SamsTrustBehaviour());
}
}
So it seems that adding to ClientBase<T>.Endpoint
works whereas adding to ChannelFactory<T>.Endpoint
does not.
Is there a way to add a behaviour to a WCF channel endpoint during registration that persists until a request is actually made from a client proxy?