IPipelineBehaviour with custom type for TResponse (ie. Result<TResponse>)
See original GitHub issueI have several commands for which I want to apply cross-cutting concerns. The thing is, my handlers return Result<TResponse>
and not just TResponse
.
public readonly struct Result<T> { }
public class LoggingBehaviour<TRequest, TResponse> : IPipelineBehavior<TRequest, Result<TResponse>>
{
public async Task<Result<TResponse>> Handle(TRequest request, CancellationToken cancellationToken, RequestHandlerDelegate<Result<TResponse>> next)
{
Log.Information($"BEFORE");
var response = await next();
Log.Information($"AFTER");
return response;
}
}
// in Startup.cs
services.AddTransient(typeof(IPipelineBehavior<,>), typeof(LoggingBehaviour<,>))
but I receive a run-time error when my commands are executed
An unhandled exception has occurred while executing the request.
System.ArgumentException: Implementation type LoggingBehaviour`2[Command1,Result[Response]]' can't be converted to service type 'MediatR.IPipelineBehavior`2[Command1,Result[Response]]'
I suppose my registration is not right, but can’t figure out how to do it properly.
Can you help me? Thanks!
Issue Analytics
- State:
- Created 4 years ago
- Reactions:1
- Comments:7 (3 by maintainers)
Top Results From Across the Web
Response Caching with MediatR in ASP.NET Core
In this article, we are going to implement Response Caching with MediatR in ASP.NET Core using its awesome Pipeline Behaviours.
Read more >MediatR IPipelineBehavior<TRequest, TResponse> errors ...
0.0 I started getting the below compilation error. The type 'TRequest' cannot be used as type parameter 'TRequest' in the generic type or...
Read more >Advanced features of the MediatR package - Pipeline ...
The Handler processes the Request and returns a response back. ... The new class shall implement the IPipelineBehavior interface, ...
Read more >CQRS and MediatR in ASP.NET Core
We first define a LoggingBehavior class, taking two types of parameters TRequest and TResponse , and implementing the IPipelineBehavior<TRequest ...
Read more >MediatR Pipeline Examples
My authorization handler applies the former based on generic type (TRequest, TResponse) and the latter based on an attribute I add to my...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
I think there are issues with registering open generics on the default DI container for dotnet core - you may need to use a more advanced container. Here is my code using Simple Injector
here is the comment from jimmy on the issue - its not open generics but constrained open generics https://github.com/jbogard/MediatR/issues/305#issuecomment-417002645
not sure if this is the same as the exception you’re seeing though