Dependency injection 使用ASP.NET Core DI注册受MediatR管道约束的后处理器
以前,我们有以下管道设置用于审核日志记录Dependency injection 使用ASP.NET Core DI注册受MediatR管道约束的后处理器,dependency-injection,asp.net-core,mediatr,Dependency Injection,Asp.net Core,Mediatr,以前,我们有以下管道设置用于审核日志记录 public class AuditLogPostProcessor<TRequest, TResponse> : IRequestPostProcessor<TRequest, TResponse> where TRequest : IAuditLogRequest<TResponse> 因此,DI似乎没有遵守约束。我可以使用ASP.NET Core D
public class AuditLogPostProcessor<TRequest, TResponse> :
IRequestPostProcessor<TRequest, TResponse>
where TRequest : IAuditLogRequest<TResponse>
因此,DI似乎没有遵守约束。我可以使用ASP.NET Core DI获得所需的行为吗?我们有类似的设置 如果处理程序实现了IRequestPostProcessor,则只需将其添加到DI中:
services.AddTransient(typeof(IPipelineBehavior<,>), typeof(RequestPostProcessorBehavior<,>));
services.AddTransient(typeof(IPipelineBehavior),typeof(RequestPostProcessorBehavior));
我想做完全相同的事情。显然ASP.net core DI不支持此功能
SRC:
您需要使用其他容器,如Autofac或StructureMap。
例如:
它非常简单,不需要太多更改
_container.RegisterCollection(typeof(IRequestPostProcessor<,>),
new[] { typeof(GenericRequestPostProcessor<,>),typeof(AuditLogPostProcessor<,>) });
services.AddScoped(typeof(IRequestPostProcessor<,>), typeof(GenericRequestPostProcessor<,>));
services.AddScoped(typeof(IRequestPostProcessor<,>), typeof(AuditLogPostProcessor<,>));
TypeLoadException: GenericArguments[0], 'Command',
on 'AuditLogPostProcessor`2[TRequest,TResponse]' violates
the constraint of type parameter 'TRequest'.
services.AddTransient(typeof(IPipelineBehavior<,>), typeof(RequestPostProcessorBehavior<,>));