c# - Autofac WcfIntegration - 通过 net.tcp 通信时无法激活 WCF 客户端服务

标签 c# wcf tcp dependency-injection autofac

我有一个在 IIS 7 下托管并使用 Autofac 的 WCF 集成的 WCF 客户端服务。该服务由另一个 WCF 服务使用基本 Http 绑定(bind)调用。自从大约 3 个月前开始使用该服务以来,一切都运行良好。

但是,当我尝试通过 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;
    }

从上面的Autofac注册调用客户端的WCF服务:

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" %>

最佳答案

发生这种情况可能是因为您在 WCF 服务的 Global.asaxApplication_Start() 方法中进行了 Autofac 注册。

Global.asax仅支持 HTTP。要与绑定(bind)无关(例如 TCP),您需要 WCF 服务的另一个起点。

一个选项是AppInitialize方法。在 WCF 服务的根目录中创建一个 App_Code 文件夹,并添加一个类(名称并不重要),并添加一个具有此签名的方法 public static void AppInitialize() .

无论用于触发服务的绑定(bind)如何,该方法中的代码都将在服务启动时执行。您可以将 Autofac 注册放在这里。

有关AppInitialize的更多信息可以找到here .

关于c# - Autofac WcfIntegration - 通过 net.tcp 通信时无法激活 WCF 客户端服务,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34991011/

相关文章:

c# - 确定缓冲区应该有多大

c# - 如何通过 rpc 发送字典

wcf - "WCF endpoint"的定义是什么?

c# - 用于SSRS和WCF测试的Soap请求的QUERY方法

linux - 在 Arch Linux 中为 neo4j 启用 HTTP TCP 连接请求

c# - 套接字在一定时间后受到限制

c# - AJAXToolkit动态隐藏选项卡?

c# - WPF - 在大量控件上设置 Tab 键顺序

c# - .NET MVC 对模型的依赖注入(inject)?

WCF 数据契约与类序列化