Autofac WCF integration + sessions
        Posted  
        
            by 
                Michael Sagalovich
            
        on Stack Overflow
        
        See other posts from Stack Overflow
        
            or by Michael Sagalovich
        
        
        
        Published on 2011-08-12T14:17:12Z
        Indexed on 
            2012/09/27
            9:37 UTC
        
        
        Read the original article
        Hit count: 999
        
I am having an ASP.NET MVC 3 application that collaborates with a WCF service, which is hosted using Autofac host factory. Here are some code samples:
.svc file:
<%@ ServiceHost 
Language="C#" 
Debug="true"
Service="MyNamespace.IMyContract, MyAssembly" 
Factory="Autofac.Integration.Wcf.AutofacServiceHostFactory, Autofac.Integration.Wcf" %>
Global.asax of the WCF service project:
protected void Application_Start(object sender, EventArgs e)
{
    ContainerBuilder builder = new ContainerBuilder();        
    //Here I perform all registrations, including implementation of IMyContract
    AutofacServiceHostFactory.Container = builder.Build();            
}
Client proxy class constructor (MVC side):
ContainerBuilder builder = new ContainerBuilder();
builder.Register(c => new ChannelFactory<IMyContract>(
    new BasicHttpBinding(),
    new EndpointAddress(Settings.Default.Url_MyService)))
    .SingleInstance();
builder.Register(c => c.Resolve<ChannelFactory<IMyContract>>().CreateChannel())
    .UseWcfSafeRelease();
_container = builder.Build();
This works fine until I want WCF service to allow or require sessions ([ServiceContract(SessionMode = SessionMode.Allowed)], or [ServiceContract(SessionMode = SessionMode.Required)]) and to share one session with the MVC side. I changed the binding to WSHttpBinding on the MVC side, but I am having different exceptions depending on how I tune it. 
I also tried changing AutofacServiceHostFactory to AutofacWebServiceHostFactory, with no result.
I am not using config file as I am mainly experimenting, not developing real-life application, but I need to study the case. But if you think I can achieve what I need only with config files, then OK, I'll use them.
I will provide exception details for each combination of settings if required, I'm omitting them not to make the post too large. Any ideas on what I can do?
© Stack Overflow or respective owner