2016-01-25 109 views
2

我有一個WCF客戶端服務在IIS 7下託管並使用Autofac的WCF集成。此服務由另一個WCF服務調用,使用基本Http綁定。自從3個月前開始使用該服務以來,一切工作都已開始以及Autofac WcfIntegration - 通過net.tcp進行通信時無法激活WCF客戶端服務

然而,當我嘗試調用過的net.tcp這項服務,我能做到這一點,接收回調一段時間(通常約8小時)後,我不斷收到此錯誤:

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

從託管服務器

的例外是:

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.

服務的標記:

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

Autofac登記:

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; 
    } 

WCF服務從上面Autofac註冊調用客戶端:

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(); 
    } 

標記:

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

回答

1

這可能是因爲你做的Autofac登記在WCF服務的Global.asax的方法Application_Start()

Global.asax是僅限HTTP。要與綁定無關(例如TCP),您需要WCF服務的另一個起點。

一個選項是AppInitialize-方法。在WCF服務的根目錄中創建一個App_Code文件夾,並添加一個類(名稱不重要),並添加一個包含此簽名public static void AppInitialize()的方法。

無論用於觸發服務的綁定,服務啓動時都會執行此方法中的代碼。您可以在這裏放置Autofac註冊。

有關AppInitialize的更多信息,請參見here

相關問題