【问题标题】:How do I configure autofac with WCF under Azure with a WebRole如何使用 WebRole 在 Azure 下使用 WCF 配置 autofac
【发布时间】:2013-03-09 08:19:00
【问题描述】:

我正在尝试为 Azure 配置一个简单的项目,以使用带有 Http 绑定的 WCF 服务,并使用 Autofac 的 WCF 集成。目前我正在本地测试并在 IIS (Azure) 下托管。

首先,我将项目设置为在没有 Autofac 的情况下运行,并且可以实例化服务并查看通过浏览器公开的 WSDL 端点。

然后我修改了服务 svc 文件并添加了工厂定义。

<%@ ServiceHost Language="C#" Debug="true" Service="EposDataService" Factory="Autofac.Integration.Wcf.AutofacServiceHostFactory, Autofac.Integration.Wcf" %>

我接下来修改了WebRole类:

 public override void Run()
    {
        // This is a sample worker implementation. Replace with your logic.
        Trace.WriteLine("Worker entry point called", "Information");

        var builder = new ContainerBuilder();
        builder.RegisterModule(new ServiceModule());

        //using (m_Container = builder.Build())
        m_Container = builder.Build();
        //{
            AutofacHostFactory.Container = m_Container;
            while (true)
            {
                // sleep 30 minutes. we don't really need to do anything here.
                Thread.Sleep(1800000);
                Trace.WriteLine("Working", "Information");
            }
        //}    
    }

但是,当我在本地部署服务并尝试访问服务时,出现错误:

The AutofacServiceHost.Container static property must be set before services can be instantiated.

Exception Details: System.InvalidOperationException: The AutofacServiceHost.Container static property must be set before services can be instantiated.

我不明白的是,静态属性是在 WebRole 中设置的,但看起来好像有什么东西正在重置分配。有什么建议?

【问题讨论】:

标签: wcf azure autofac


【解决方案1】:

答案似乎是多种因素的组合。

首先,我将 Autofac 注册和引导从 WebRole 移至 Global.asax。这有助于解决我第一次收到的错误消息。

其次,我更正了服务注册中的一个错误,并从以下位置重新配置了注册:

builder.RegisterType<EposDataService>().As<IEposDataService>()

builder.RegisterType<EposDataService>().AsSelf();

参考: - autofac wcf registration error

第三,我完全限定了 svc 中的服务名称。 参考资料:

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-11-22
    • 2021-01-25
    • 1970-01-01
    • 1970-01-01
    • 2016-08-11
    • 2011-12-22
    相关资源
    最近更新 更多