❔ MediatR Issue with IRequest and Generic Method in ASP.NET Core
I'm facing an issue with MediatR in my ASP.NET Core project. I have an action method in a controller where I'm using MediatR's ISender.Send<TRequest> (where TRequest is GetCollectionsAsyncQuery) inside a generic method. However, I'm getting this exception:
"The type 'GetCollectionsAsyncQuery' cannot be used as type parameter 'TRequest' in the generic type or method 'ISender.Send (TRequest, CancellationToken)'. There is no implicit reference conversion from 'GetCollectionsAsyncQuery' to 'IRequest'."
Here's my HttpGet action method:
The relevant codes inside my Handler method and GetCollectionsAsyncQuery are:
16 Replies
My base controller's Handle methods are:
Things I've Tried
I've ensured GetCollectionsAsyncQuery implements IRequest<IEnumerable<GetCollectionsAsyncQueryResult>>. My Handler class processes the GetCollectionsAsyncQuery and returns a Task<IEnumerable<GetCollectionsAsyncQueryResult>>.
I've checked that MediatR and AutoMapper are correctly registered in the dependency injection container during my application's startup.
I have checked and confirmed that my AutoMapper configuration is fine. My mapping process maps the Collection entity to GetCollectionsAsyncQueryResult and it works correctly.
I verified there's no issue with my MediatR pipeline behaviours or pre/post processors.
Despite trying these things, the exception persists. Could anyone help me troubleshoot this?
Unknown User•17mo ago
Message Not Public
Sign In & Join Server To View
To have more flexibility and to be more reusable due to the ability to handle any type of object
Unknown User•17mo ago
Message Not Public
Sign In & Join Server To View
I think so. My goal here is to have a generic result for all of my actions instead of calling QueryorCommandResult new Instance every action
This the repository for better visual https://github.com/aldrin0408av/MineralWaterMonitoringSystem
GitHub
GitHub - aldrin0408av/MineralWaterMonitoringSystem
Contribute to aldrin0408av/MineralWaterMonitoringSystem development by creating an account on GitHub.
This is first approach I use
When It comes to MediatR, I don't recommend using generics. The only way mediatr is able to detect the correct handler is by the command/query.
If you want to use generics, than you are going to have to register them correctly in your service collection.
Have you done that?
Don't use
dynamic
. Here is an example of how you can register your generics. What is the BaseController looks like?
Something like this?
Unknown User•17mo ago
Message Not Public
Sign In & Join Server To View
how can I make a custom response like this one
Unknown User•17mo ago
Message Not Public
Sign In & Join Server To View
Thank you for your detailed feedback and suggestions concerning the use of static typing, direct mappings, and the single responsibility principle in our codebase. Your inputs are valuable and appreciated.
For the extension does it look like this? Instead of auto mapper?
Unknown User•17mo ago
Message Not Public
Sign In & Join Server To View
No, keep the register services from assembly seperate.
Let's say we have this generic command
And here is the handler for it
Here is how you register it
You will need to register each type. So, I'm using the generic
LikeCommand<T>
for Quote
and Comment
. Then you have to explicitly register the types so that MediatR is able to locate the correct handler.
The Boolean
in the registration part. Indicates, they response the commend is going to return when the operation has been performed.Was this issue resolved? If so, run
/close
- otherwise I will mark this as stale and this post will be archived until there is new activity.