如何使用WebRole在Azure下使用WCF配置autofac

时间:2013-03-09 08:19:00

标签: wcf azure autofac

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

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

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

<%@ 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中设置的,但似乎有些东西正在重置分配。有什么建议?

1 个答案:

答案 0 :(得分:1)

答案似乎是各种因素的结合。

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

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

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

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

参考:   - autofac wcf registration error

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

相关问题