以前,我们有以下管道设置用于审计日志记录
public class AuditLogPostProcessor<TRequest, TResponse> :
IRequestPostProcessor<TRequest, TResponse>
where TRequest : IAuditLogRequest<TResponse>
IAudiLogRequest 在哪里实现 IRequest
public interface IAuditLogRequest<TResponse> : IRequest<TResponse>
所有且只有实现 IAudiLogRequest 的命令到达 AuditLogPostProcessor。
使用 SimpleInjector 注册如下
_container.RegisterCollection(typeof(IRequestPostProcessor<,>),
new[] { typeof(GenericRequestPostProcessor<,>),typeof(AuditLogPostProcessor<,>) });
目前,我们使用 ASP.NET Core DI 进行依赖注入,注册如下。
services.AddScoped(typeof(IRequestPostProcessor<,>), typeof(GenericRequestPostProcessor<,>));
services.AddScoped(typeof(IRequestPostProcessor<,>), typeof(AuditLogPostProcessor<,>));
当命令实现 IRequest 时,我们得到错误
TypeLoadException: GenericArguments[0], 'Command',
on 'AuditLogPostProcessor`2[TRequest,TResponse]' violates
the constraint of type parameter 'TRequest'.
因此,DI 似乎没有遵守约束。我可以使用 ASP.NET Core DI 获得所需的行为吗?