gpt4 book ai didi

c# - 使用 svc-less 服务的 Autofac WCF 注册异常

转载 作者:太空宇宙 更新时间:2023-11-03 13:08:28 26 4
gpt4 key购买 nike

我正在尝试将 Autofac 设置为我正在处理的新 WCF 项目的 DI 容器。我们正在使用无 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;
}
}

运行时,出现以下异常:

An exception of type 'System.ArgumentException' occurred in Autofac.Integration.Wcf.dll but was not handled in user code.
Additional information: The service contract type 'tktktktk.Services.AccountClassService' has not been registered in the container.

当我在代码中放置断点并检查容器时,我可以在 ComponentRegistry.Registrations 集合中看到我的所有服务,包括“tktktktk.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,我不再在此代码中遇到任何断点,这表明它现在已被完全绕过。

最佳答案

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

The service 'MyService' configured for WCF is not registered with the Autofac container. 

事实证明一切都已正确注册,但是 web.config 中的“添加工厂”元素需要服务的程序集名称,就像 .svc 文件存在时一样:

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

Autofac documentation没有在示例中提及或显示它。此外,Visual Studio 会提示并在属性下划线,但服务会正常运行。

关于c# - 使用 svc-less 服务的 Autofac WCF 注册异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29992005/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com