gpt4 book ai didi

c# - 在windows服务项目中注册autofac

转载 作者:行者123 更新时间:2023-11-30 22:04:26 25 4
gpt4 key购买 nike

我想在我的 Windows 服务项目中使用 autofac,但出现此错误:

“请求的服务‘InsideView.Business.Services.BusinessServiceFactory’尚未注册。要避免此异常,请注册一个组件以提供服务,使用 IsRegistered() 检查服务注册,或使用 ResolveOptional()解决可选依赖项的方法。”

我的代码:

       var builder = new ContainerBuilder();

foreach (var type in typeof(IMyService).Assembly.GetTypes().Where(type => type.Name.EndsWith("Service") && type.IsClass))
{
var siblingInterface = type.GetInterfaces().First(i => i.Name == "I" + type.Name);
builder.RegisterType(type).Keyed(siblingInterface, typeof(IBusinessService)).InstancePerLifetimeScope();
}

builder.RegisterType<BusinessServiceFactory>()
.As<IBusinessServiceFactory>().InstancePerLifetimeScope();


IContainer container = builder.Build();

var businessFactory = container.Resolve<BusinessServiceFactory>()

有什么想法吗?谢谢

最佳答案

当您将服务注册为 IBusinessServiceFactory 时,您还必须解析 IBusinessServiceFactory,而不是类类型 BusinessServiceFactory,就像您在样本。

关于c# - 在windows服务项目中注册autofac,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25246531/

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