autofac 解决键值问题
autofac resolve issue for keyed values
我目前正在开发一项功能,并在 Autofac 中添加了这样的构建器代码
builder.RegisterType<ILTLoPublisher<ScheduleUpdateEvent>>()
.AsImplementedInterfaces()
.InstancePerRequest()
.Keyed<IILTLoPublisher<ScheduleUpdateEvent>>(AuditType.Schedule);
builder.RegisterType<ILTLoPublisher<ScheduleUpdatePart>>()
.AsImplementedInterfaces()
.InstancePerRequest()
.Keyed<IILTLoPublisher<ScheduleUpdatePart>>(AuditType.Part);
builder.RegisterType<ILTLoPublisher<ScheduleUpdateTest>>()
.AsImplementedInterfaces()
.InstancePerRequest()
.Keyed<IILTLoPublisher<ScheduleUpdateTest>>(AuditType.Test);
此代码 运行 作为一个控制台应用程序服务,对此的调用来自 api service.I 希望它被调用如下
AutoFacModule autofac = new AutoFacModule();
var builder = new ContainerBuilder();
autofac.LoadBuilder(builder);
Container = builder.Build();
using (var scope = Container.BeginLifetimeScope())
{
var _publisher1 = scope.ResolveKeyed<IILTLoPublisher<ScheduleUpdateEvent>>(AuditType.Schedule);
var _publisher2 = scope.ResolveKeyed<IILTLoPublisher<ScheduleUpdatePart>>(AuditType.Part);
var _publisher2 = scope.ResolveKeyed<IILTLoPublisher<ScheduleUpdateTest>>(AuditType.Test);
}
当我尝试在我的实现中使用以下代码解决它时 class
var _publisher = scope.ResolveKeyed<IILTLoPublisher<ScheduleUpdateEvent>>(AuditType.Schedule);
我收到以下错误
Unable to resolve the type Apiconnector.Integrations.Vilt.Service.Providers.Custom.Publish.ILTLoPublisher`1[LMS.ILT.ScheduleUpdateEvent]' because the lifetime scope it belongs in can't be located
您不能使用 InstancePerRequest
除非正在解析的对象是 Web 请求的一部分(如问题评论中所述)。更具体地说:
- 执行的应用程序必须是 Web 应用程序。
- 正在执行的应用程序需要 Autofac web integration 到位。
- 作为对入站 Web 请求的响应的一部分,解决方案必须在该 Web 应用程序中发生 - 例如,作为 MVC 控制器或 ASP.NET 核心中间件的一部分。
“按请求”语义与发出请求的客户端无关——它是关于服务器处理请求。
You might want to spend some time with the documentation on the topic. 其中有一节介绍如何为您的应用程序实现自定义的按请求语义。
如果您正在创建的是一个控制台应用程序,它接收来自客户端的请求(例如,自托管网络应用程序),那么您需要:
- 为您的应用程序类型添加现有的 Autofac 网络集成(我们支持 ASP.NET 网络 API 和 ASP.NET 核心自托管方案);或者
- 如果您不使用 ASP.NET,请执行一些自定义操作(请参阅我链接的文档)。
如果您创建的是一个控制台应用程序作为客户端发出请求,那么您应该忽略InstancePerRequest
。相反:
- 围绕每个请求创建一个新的生命周期范围(就像您正在做的那样)并将其视为一个工作单元。
- 将组件注册为
InstancePerLifetimeScope
,这样在该生命周期范围内只有一个组件。
就是说,如果没有最少的复现,就很难看到您在提供任何指导之外做了什么。
既然你提到你对这一切还很陌生,非常值得你花时间检查一下the Autofac documentation to start understanding concepts like this as well as looking in the Examples repo,那里有许多不同应用程序的工作示例向您展示事情如何运作的类型。
我目前正在开发一项功能,并在 Autofac 中添加了这样的构建器代码
builder.RegisterType<ILTLoPublisher<ScheduleUpdateEvent>>()
.AsImplementedInterfaces()
.InstancePerRequest()
.Keyed<IILTLoPublisher<ScheduleUpdateEvent>>(AuditType.Schedule);
builder.RegisterType<ILTLoPublisher<ScheduleUpdatePart>>()
.AsImplementedInterfaces()
.InstancePerRequest()
.Keyed<IILTLoPublisher<ScheduleUpdatePart>>(AuditType.Part);
builder.RegisterType<ILTLoPublisher<ScheduleUpdateTest>>()
.AsImplementedInterfaces()
.InstancePerRequest()
.Keyed<IILTLoPublisher<ScheduleUpdateTest>>(AuditType.Test);
此代码 运行 作为一个控制台应用程序服务,对此的调用来自 api service.I 希望它被调用如下
AutoFacModule autofac = new AutoFacModule();
var builder = new ContainerBuilder();
autofac.LoadBuilder(builder);
Container = builder.Build();
using (var scope = Container.BeginLifetimeScope())
{
var _publisher1 = scope.ResolveKeyed<IILTLoPublisher<ScheduleUpdateEvent>>(AuditType.Schedule);
var _publisher2 = scope.ResolveKeyed<IILTLoPublisher<ScheduleUpdatePart>>(AuditType.Part);
var _publisher2 = scope.ResolveKeyed<IILTLoPublisher<ScheduleUpdateTest>>(AuditType.Test);
}
当我尝试在我的实现中使用以下代码解决它时 class
var _publisher = scope.ResolveKeyed<IILTLoPublisher<ScheduleUpdateEvent>>(AuditType.Schedule);
我收到以下错误
Unable to resolve the type Apiconnector.Integrations.Vilt.Service.Providers.Custom.Publish.ILTLoPublisher`1[LMS.ILT.ScheduleUpdateEvent]' because the lifetime scope it belongs in can't be located
您不能使用 InstancePerRequest
除非正在解析的对象是 Web 请求的一部分(如问题评论中所述)。更具体地说:
- 执行的应用程序必须是 Web 应用程序。
- 正在执行的应用程序需要 Autofac web integration 到位。
- 作为对入站 Web 请求的响应的一部分,解决方案必须在该 Web 应用程序中发生 - 例如,作为 MVC 控制器或 ASP.NET 核心中间件的一部分。
“按请求”语义与发出请求的客户端无关——它是关于服务器处理请求。
You might want to spend some time with the documentation on the topic. 其中有一节介绍如何为您的应用程序实现自定义的按请求语义。
如果您正在创建的是一个控制台应用程序,它接收来自客户端的请求(例如,自托管网络应用程序),那么您需要:
- 为您的应用程序类型添加现有的 Autofac 网络集成(我们支持 ASP.NET 网络 API 和 ASP.NET 核心自托管方案);或者
- 如果您不使用 ASP.NET,请执行一些自定义操作(请参阅我链接的文档)。
如果您创建的是一个控制台应用程序作为客户端发出请求,那么您应该忽略InstancePerRequest
。相反:
- 围绕每个请求创建一个新的生命周期范围(就像您正在做的那样)并将其视为一个工作单元。
- 将组件注册为
InstancePerLifetimeScope
,这样在该生命周期范围内只有一个组件。
就是说,如果没有最少的复现,就很难看到您在提供任何指导之外做了什么。
既然你提到你对这一切还很陌生,非常值得你花时间检查一下the Autofac documentation to start understanding concepts like this as well as looking in the Examples repo,那里有许多不同应用程序的工作示例向您展示事情如何运作的类型。