2
votes

I've built an ASP.NET MVC 3 Application hosting a WCF-Service. The service class doing the actual work resides within a class library. I'm trying to use the Windsor WCF facility to wire it up on service request.

Here's my Windsor container factory:

public class WindsorContainerFactory
{
    private static IWindsorContainer container;
    private static readonly object sync = new Object();

    public static IWindsorContainer Current()
    {
        if(container == null) {
            lock (sync)
            {
                if (container == null)
                {
                    container = new WindsorContainer();
                    container.Install(new ControllerInstaller());
                    container.Install(new NHibernateSessionInstaller());
                    container.Install(new RepositoryInstaller());
                    container.Install(new ServiceInstaller());
                }
            }
        }
        return container;
    }
}

In Global.asax, I call WindsorContainerFactory.Current() once to guarantee the factory is beeing built:

    protected void Application_Start()
    {
        WindsorContainerFactory.Current();
        ControllerBuilder.Current.SetControllerFactory(typeof(WindsorControllerFactory));
        ...

I install my service by the following class:

public class ServiceInstaller : IWindsorInstaller
{
    public void Install(Castle.Windsor.IWindsorContainer container, Castle.MicroKernel.SubSystems.Configuration.IConfigurationStore store)
    {
        container.Kernel.AddFacility<WcfFacility>();
        container.Kernel.Register(
            Component
            .For<ICustomerService>()
            .ImplementedBy<CustomerService>()
            .Named("CustomerService"));
    }
}

Then I added a new WCF service to the project, deleted the code behind file and modified the svc markup as follows:

<%@ ServiceHost Language="C#" Debug="true"    
Factory="Castle.Facilities.WcfIntegration.DefaultServiceHostFactory, 
Castle.Facilities.WcfIntegration" Service="CustomerService" %>

As you can see, there are other components beeing installed within windsor (Repositories, Controllers,...), but this seems to work well.

My problem is, that the WCF-client (console app) gets the error: HttpContext.Current is null. PerWebRequestLifestyle can only be used in ASP.Net

Client code:

        CustomerServiceClient c = new Services.Customers.CustomerServiceClient();
        CustomerResponse resp = c.GetCustomerBySurname("Lolman");
        c.Close();            

Can anyone point me to the right direction? Thank you in advance!

1

1 Answers

3
votes

Try enabling AspNetCompatibility. Check this link for help