question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Sending object as IRequest throws

See original GitHub issue

Hello, 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:closed
  • Created 7 months ago
  • Comments:5 (1 by maintainers)

github_iconTop GitHub Comments

2reactions
jbogardcommented, Feb 23, 2023

Now!

1reaction
knopacommented, Feb 23, 2023

Thanks for quick update. any ETA on release?

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found