Sending object as IRequest throws
See original GitHub issueHello,
I just upgraded from MediatR 11.1.0 to 12.0.0 and found that you cannot send requests as IRequest
anymore. Sending them using their concrete type, or IBaseRequest
, or object
still works.
Is this a bug or a feature?
Regards, Philip
Here is a minimal reproducing example:
Program.cs:
using MediatR;
using Microsoft.Extensions.DependencyInjection;
using System.Reflection;
var services = new ServiceCollection();
services.AddMediatR(o => o.RegisterServicesFromAssembly(Assembly.GetExecutingAssembly()));
var sp = services.BuildServiceProvider();
var mediator = sp.GetRequiredService<IMediator>();
// Sending as MyCommand, IBaseRequest, or object works:
MyCommand cmd0 = new MyCommand() { TypeName = "MyCommand" };
await mediator.Send(cmd0);
IBaseRequest cmd1 = new MyCommand() { TypeName = "IBaseRequest" };
await mediator.Send(cmd1);
object cmd2 = new MyCommand() { TypeName = "object" };
await mediator.Send(cmd2);
// Sending as IRequest throws:
IRequest cmd3 = new MyCommand() { TypeName = "IRequest" };
await mediator.Send(cmd3);
class MyCommand : IRequest
{
public string? TypeName { get; set; }
}
class MyCommandHandler : IRequestHandler<MyCommand>
{
public Task Handle(MyCommand request, CancellationToken cancellationToken)
{
Console.WriteLine($"Handling command. Sent as {request.TypeName}");
return Task.CompletedTask;
}
}
Project file:
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net6.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="MediatR" Version="12.0.0" />
<PackageReference Include="Microsoft.Extensions.Hosting" Version="7.0.1" />
</ItemGroup>
</Project>
Issue Analytics
- State:
- Created 7 months ago
- Comments:5 (1 by maintainers)
Top Results From Across the Web
MediatR Send object without typing
This is how I achieved to call MediatR from a RabbitMqRequestHandler. Basically, I wanted to fire any command type using a queue message....
Read more >Possibility to use Send(object) and Publish(object) without ...
When Send(object) or Publish(object) is used with an object that does not implement IRequest or INotification, an exception is thrown.
Read more >Method that is aware of interface underlying type without ...
To get around it I created an interface called IRequest to be able to pass different types of objects into my method, but...
Read more >CQRS with Mediatr and ASP.NET Core - Steve Gordon
We do this by calling SendAsync on the IMediatr object. We send a UserQuery object with the Id property set. This message will...
Read more >Mediator design pattern in modern applications
IMediator object has two methods: Send and Publish. Send method is used for the command type of requests, the Publish method serves more...
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
Now!
Thanks for quick update. any ETA on release?