C# Autofac WCF注册异常与无svc服务

C# Autofac WCF注册异常与无svc服务,c#,wcf,autofac,C#,Wcf,Autofac,我正在尝试将Autofac设置为我的DI容器,用于我正在进行的一个新WCF项目。我们正在使用无svc配置和自托管。没有Autofac,只是简单地使用穷人的DI,一切都完全按照预期工作,但当我将Autofac添加到混合中时,有些地方出了问题 我用来设置一切的代码是: public class CustomServiceHostFactory : ServiceHostFactory { protected override ServiceHost CreateServiceHost(T

我正在尝试将Autofac设置为我的DI容器,用于我正在进行的一个新WCF项目。我们正在使用无svc配置和自托管。没有Autofac,只是简单地使用穷人的DI,一切都完全按照预期工作,但当我将Autofac添加到混合中时,有些地方出了问题

我用来设置一切的代码是:

  public class CustomServiceHostFactory : ServiceHostFactory
{
    protected override ServiceHost CreateServiceHost(Type serviceType, Uri[] baseAddresses)
    {
        var container = InitializeDIContainer();
        var customHost = new CustomServiceHost(serviceType, baseAddresses);
        // Exception is being thrown on the line below
        customHost.AddDependencyInjectionBehavior(serviceType, container);
        return customHost;
    }

    private static IContainer InitializeDIContainer()
    {
        var builder = new ContainerBuilder();
        builder.RegisterAssemblyTypes(typeof (AccountRepository).Assembly)
            .Where(t => t.Name.EndsWith("Repository"))
            .As(t => t.GetInterfaces().FirstOrDefault(
                i => i.Name == "I" + t.Name));

        builder.RegisterAssemblyTypes(typeof (AccountService).Assembly)
            .Where(t => t.Name.EndsWith("Service"))
            .As(t => t.GetInterfaces().FirstOrDefault(
                i => i.Name == "I" + t.Name));

        builder.RegisterType<tktktktkDbContext>().As<IDataContextAsync>();
        builder.RegisterType<UnitOfWork>().As<IUnitOfWorkAsync>();

        var container = builder.Build();
        return container;
    }
}
当我在代码中放置断点并检查容器时,我可以在ComponentRegistry.Registrations集合中看到我的所有服务,包括“Tktk.services.AccountClassService”对象

我曾尝试重新编写web.config文件以使用“Autofac.Integration.Wcf.AutofacServiceHostFactory”工厂,但我的应用程序在达到这一点之前就失败了

我想我错过了某个地方的一步,但不知该做什么。任何帮助都将不胜感激

更新

我修改了web.config以使用“Autofac.Integration.Wcf.AutofacServiceHostFactory”,如图所示。我现在得到以下错误:

WebHost failed to process a request.
 Sender Information: System.ServiceModel.ServiceHostingEnvironment+HostingManager/45653674
 Exception: System.ServiceModel.ServiceActivationException: The service '/Account/AccountClassService.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.
更新2


我尝试了下面答案中的建议,得到了与上面相同的错误。我注意到的一点是,当我在代码中添加
AutofacServiceHost.Container=Container
时,它不会编译。我把它切换到
AutofacHostFactory.Container
,它编译得很好。此外,随着web.config更改为使用
Autofac.Integration.Wcf.AutofacServiceHostFactory
,我不再在这段代码中遇到任何断点,这表明它现在被完全绕过了

以下是您使用自定义主机的情况,另一方面,Autofac将其内置的ServiceHost作为开箱即用功能提供

您需要设置
AutofacServiceHost
类的属性容器:

 public class CustomServiceHostFactory : ServiceHostFactory
    {
        protected override ServiceHost CreateServiceHost(Type serviceType, Uri[] baseAddresses)
        {
            var container = InitializeDIContainer();

            // Add this line and try again.
            AutofacServiceHost.Container = container;

            var customHost = new CustomServiceHost(serviceType, baseAddresses);

            customHost.AddDependencyInjectionBehavior(serviceType, container);
            return customHost;
        }

        private static IContainer InitializeDIContainer()
        {
            var builder = new ContainerBuilder();
            builder.RegisterAssemblyTypes(typeof (AccountRepository).Assembly)
                .Where(t => t.Name.EndsWith("Repository"))
                .As(t => t.GetInterfaces().FirstOrDefault(
                    i => i.Name == "I" + t.Name));

            builder.RegisterAssemblyTypes(typeof (AccountService).Assembly)
                .Where(t => t.Name.EndsWith("Service"))
                .As(t => t.GetInterfaces().FirstOrDefault(
                    i => i.Name == "I" + t.Name));

            builder.RegisterType<tktktktkDbContext>().As<IDataContextAsync>();
            builder.RegisterType<UnitOfWork>().As<IUnitOfWorkAsync>();

            var container = builder.Build();
            return container;
        }
    }
公共类CustomServiceHostFactory:ServiceHostFactory
{
受保护的重写ServiceHost CreateServiceHost(类型serviceType,Uri[]baseAddresses)
{
var container=initializedContainer();
//添加此行,然后重试。
AutofacServiceHost.Container=容器;
var customHost=新的CustomServiceHost(serviceType,baseAddresses);
AddDependencyInjectionBehavior(服务类型,容器);
返回客户主机;
}
私有静态IContainer InitializedContainer()
{
var builder=new ContainerBuilder();
builder.RegisterAssemblyTypes(typeof(AccountRepository).Assembly)
.Where(t=>t.Name.EndsWith(“存储库”))
.As(t=>t.GetInterfaces().FirstOrDefault(
i=>i.Name==“i”+t.Name));
builder.RegisterAssemblyTypes(typeof(AccountService).Assembly)
.Where(t=>t.Name.EndsWith(“服务”))
.As(t=>t.GetInterfaces().FirstOrDefault(
i=>i.Name==“i”+t.Name));
builder.RegisterType().As();
builder.RegisterType().As();
var container=builder.Build();
返回容器;
}
}

我自己也遇到了这个问题。我通过使用
命名的
样式注册成功地解决了这个问题

```

builder.RegisterType()
。命名为(“myservice”);
//在你的配置中:。。
```

注意,在我的例子中,我没有使用apsnetcompatibilitymode

我认为这是AutofacHostFactory中的一个错误,它使用了错误的servicename标识符来解析wcfservice类型。
使用
命名的注册样式,您基本上可以确保注册的类型是已知的,并且AutofacHostFactory使用的名称是匹配的

我还尝试了无svc配置。我按照文档中的步骤操作,收到以下错误消息:

The service 'MyService' configured for WCF is not registered with the Autofac container. 
事实证明,所有内容都已正确注册,但是web.config中的“add factory”元素需要服务的程序集名称,就像.svc文件(如果存在)一样:

<add factory="Autofac.Integration.Wcf.AutofacServiceHostFactory" relativeAddress="~/MyService.svc" service="NameSpace.Service, AssemblyName" />

没有提到这一点,也没有在示例中显示。此外,VisualStudio将在属性下加下划线,但服务将正确运行

必须使用“Autofac.Integration.Wcf.AutofacServiceHostFactory”进行无SVC注册。因为否则服务将不会被实例化。添加添加AutofacServiceHostFactory时出现的错误。我也有同样的问题!请问解决方法是什么?好的,我试过了,我也犯了同样的错误。我注意到的一点是,当我在代码中添加
AutofacServiceHost.Container=Container
时,它不会编译。我把它切换到
AutofacHostFactory.Container
,它编译得很好。另外,随着web.config更改为使用
Autofac.Integration.Wcf.AutofacServiceHostFactory
,我不再在这段代码中遇到任何断点,这表明它现在被完全绕过了?非常感谢,你救了我一天!
The service 'MyService' configured for WCF is not registered with the Autofac container. 
<add factory="Autofac.Integration.Wcf.AutofacServiceHostFactory" relativeAddress="~/MyService.svc" service="NameSpace.Service, AssemblyName" />