Experiences with NuGet, Autofac and Autofac WCF

Days ago I started to work on a new multi-tenant WCF,EF,MVC project. A Visual Studio solution consisted of some projects. Data layer was handled by EF, communication between pieces of software was handled with WCF and front end was baked with ASP.NET MVC. Additionally all references were made via NuGet.

During this project I faced several problems. First of them was Autofac. Autofac is a nice .Net IoC library but I had some problems with it. I never knew that Autofac WCF is a separate assembly. One reason was that I thought NuGet will download all references for me. I spent many time to configuring Autofac WCF but because I had no reference to Autofac WCF I thought it is because my version of Autofac is old. So tried to compile it.

Another problem with Autofac was that documentations has a lot content on hosting a WCF service via Autofac but many poor content on how to consume a WCF service via Autofac. Because of this I was confused how to consume a WCF service via Autofac.

Initializing Autofac with MVC was not so problematic. Just using sample codes. ASP.NET MVC is great on receiving instances on constructors.

NuGet bothered a little. It was because I was not very familiar with it. I was using it incorrectly.

Entity Framework was not very problematic in first steps. Hope to not have no problems with it as a NHibernate fun.

Wrong problem with Autofac WCF Integration

Autofac is a brand new .Net Dependency Injection / IoC just like Castle Windsor, etc. Autofac like Castle has a nice WCF integration facility. I was trying hard to establish a self hosted WCF server using Autofac. But was not successful at all. I guessed that is because of IP, Windows Firewall, Administration rights, version incompatibility and everything you say.

I tried Autofac samples but couldn’t get them running. After all I tried Alex’s solution for second or third time and realized that his sample works very fine. By watching error messages from Microsoft WcfTestClient more carefully that was clear that an internal error was occurring. But how an internal error could be viewed by WcfTestClient? Yes you are right, by using IncludeExceptionDetailInFaults. I found that in a question on SO here. So my modified version of Alex’s sample goes as here:

private static void StartServerOriginal()
{
    ContainerBuilder builder = new ContainerBuilder();
    builder.Register(c => new MySecurityManager()).As<IMySecurityManager>();

    using (IContainer container = builder.Build())
    {
        Uri address = new Uri("http://localhost:8046/MySecurityManager");
        ServiceHost host = new ServiceHost(typeof(MySecurityManager), address);
        host.AddServiceEndpoint(typeof(IMySecurityManager), new BasicHttpBinding(), string.Empty);

        IComponentRegistration registration;
        if (!container.ComponentRegistry.TryGetRegistration(new TypedService(typeof(IMySecurityManager)), out registration))
        {
            Console.WriteLine("The service contract has not been registered in the container.");
            Console.ReadLine();
            Environment.Exit(-1);
        }

        (host.Description.Behaviors[typeof(ServiceDebugBehavior)] as ServiceDebugBehavior).IncludeExceptionDetailInFaults = true;
        host.Description.Behaviors.Add(new AutofacDependencyInjectionServiceBehavior(
            container, typeof(MySecurityManager), registration));
        host.Description.Behaviors.Add(new ServiceMetadataBehavior { HttpGetEnabled = true, HttpGetUrl = address });

        host.Open();

        Console.WriteLine("The host has been opened.");
        Console.ReadLine();

        host.Close();
        Environment.Exit(0);
    }
}

By running WcfTestClient again I found that some of my own service contract’s exceptions were not marked as Serializable!

Oh Autofac, please forgive me, as I am a former user of Castle WCF Facility, I was thinking root cause of problem was you! Sorry, the problem was in my own code.