2

I have a WCF client service hosted under IIS 7 and using Autofac's WCF Integration. This service is called by another WCF service, using basic Http Bindings. Everything has worked well since the service started being used, about 3 months ago.

However, when I try calling this service over net.tcp, I am able to do it and receive callbacks for a period of time (about 8 hours usually) after that I keep receiving this error:

The requested service, 'net.tcp://ecomsvc.webhost.com:12345/EcomSvc.svc' could not be activated.

Exception from the hosting server is:

Exception: System.ServiceModel.ServiceActivationException: The service '/EcomSvc.svc' cannot be activated due to an exception during compilation. The exception message is: The AutofacServiceHost.Container static property must be set before services can be instantiated.. ---> System.InvalidOperationException: The AutofacServiceHost.Container static property must be set before services can be instantiated.

Service's markup:

<%@ ServiceHost Service="EcomService.Contract.IEcomSvc, EcomService.Contract" Factory="Autofac.Integration.Wcf.AutofacHostFactory, Autofac.Integration.Wcf" %>

Autofac registration:

private static void SetupDependencyContainer()
    {
        var builder = new ContainerBuilder();

        // Register service implementations.
        builder.RegisterType<EcomSvc>().As<IEcomSvc>();

        // Set the dependency resolver.
        var container = builder.Build();
        AutofacHostFactory.Container = container;
    }

The WCF service calling the client from above Autofac registration:

private static void SetupDiContainer()
    {
        var builder = new ContainerBuilder();

        // Register service implementations
        builder.RegisterType<HandlerSvc>().As<IHandlerSvc>();
    builder.RegisterType<HandlerService().InstancePerLifetimeScope();

        ConfigureServices(builder);

        //register other dependencies
        builder.RegisterType<ProxyCache>().As<IProxyCache>().InstancePerLifetimeScope();

        // Set the dependency resolver.
        var container = builder.Build();
        AutofacHostFactory.Container = container;
    }

    private static void ConfigureServices(ContainerBuilder builder)
    {
        RegisterService<IEcomSvc>(builder, "EcomServiceTCP"); 
    }

    public static void RegisterService<T>(ContainerBuilder builder, string endpoint)
    {
        builder.Register(c => new ChannelFactory<T>(endpoint))
            .SingleInstance();

        builder.Register(c => c.Resolve<ChannelFactory<T>>().CreateChannel())
            .As<T>().UseWcfSafeRelease();
    }

Markup:

<%@ ServiceHost
Service="HandlerService.IHandlerSvc, HandlerService"
Factory="Autofac.Integration.Wcf.AutofacServiceHostFactory, Autofac.Integration.Wcf" %>

1 Answers1

2

This probably happens because you do the Autofac registration in the Application_Start() method of the Global.asax of the WCF-service.

Global.asaxis HTTP-only. To be binding-agnostic (e.g. TCP) you need another starting-point of the WCF-service.

One option is the AppInitialize-method. Create an App_Code folder in the root of your WCF-service and add a class (the name is not important) and add a method with this signature public static void AppInitialize().

The code in this method will be executed when the service starts, regardless of the binding used to trigger the service. You can put the Autofac-registration here.

More information about AppInitialize can be found here.

Community
  • 1
  • 1