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.

دیدگاهتان را بنویسید

نشانی ایمیل شما منتشر نخواهد شد. بخش‌های موردنیاز علامت‌گذاری شده‌اند *