Previously, we had the following pipeline setup working for auditlogging purposes
public class AuditLogPostProcessor<TRequest, TResponse> :
IRequestPostProcessor<TRequest, TResponse>
where TRequest : IAuditLogRequest<TResponse>
Where IAuditLogRequest implements IRequest
public interface IAuditLogRequest<TResponse> : IRequest<TResponse>
All and only the Commands that implement the IAuditLogRequest reached the AuditLogPostProcessor.
Registration using SimpleInjector was as follows
_container.RegisterCollection(typeof(IRequestPostProcessor<,>),
new[] { typeof(GenericRequestPostProcessor<,>),typeof(AuditLogPostProcessor<,>) });
Currently, we use the ASP.NET Core DI for our Dependence Injection with the following registration.
services.AddScoped(typeof(IRequestPostProcessor<,>), typeof(GenericRequestPostProcessor<,>));
services.AddScoped(typeof(IRequestPostProcessor<,>), typeof(AuditLogPostProcessor<,>));
When a Command implements the IRequest, we get the error
TypeLoadException: GenericArguments[0], 'Command',
on 'AuditLogPostProcessor`2[TRequest,TResponse]' violates
the constraint of type parameter 'TRequest'.
So it seems the DI is not honoring the constraint. Can I get the desired behavior with the ASP.NET Core DI?
I was looking to do exactly the same thing. Apparently ASP.net core DI does not support this feature.
SRC: Support constrained open generic types
You need to use another container such as Autofac or StructureMap. Example: Autofac for ASP.NET core
It's very simple and it does not require much changes