❔ Mediatr Notification using a Base Class Not Publishing

I'm using a base class as a constraint on a generic notification in mediatr. When I publish my notification via mediatr my notification handler isn't found. I believe mediatr is supposed to support this idea; so maybe I'm just chasing a red-herring.
<Project Sdk="Microsoft.NET.Sdk.Worker">

<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<Nullable>enable</Nullable>
<ImplicitUsings>enable</ImplicitUsings>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="MediatR" Version="12.1.1" />
<PackageReference Include="Microsoft.Extensions.Hosting" Version="7.0.1" />
</ItemGroup>
</Project>
<Project Sdk="Microsoft.NET.Sdk.Worker">

<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<Nullable>enable</Nullable>
<ImplicitUsings>enable</ImplicitUsings>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="MediatR" Version="12.1.1" />
<PackageReference Include="Microsoft.Extensions.Hosting" Version="7.0.1" />
</ItemGroup>
</Project>
using MediatR;

IHost host = Host.CreateDefaultBuilder(args)
.ConfigureServices(services =>
{
services.AddMediatR(o=>o.RegisterServicesFromAssembly(typeof(Program).Assembly));
})
.Build();


host.Start();

var mediator = host.Services.GetRequiredService<IMediator>();

Console.WriteLine("Sending Event One");
await mediator.Publish(new EventNotification<EventOne>());

Console.WriteLine("Sending Event Two");
await mediator.Publish(new EventNotification<EventTwo>());

class EventHandler<TModel> : NotificationHandler<EventNotification<TModel>> where TModel : TestEvent
{
protected override void Handle(EventNotification<TModel> notification)
{
Console.WriteLine(notification.Model.DateTime);
}
}

class EventNotification<T> : INotification where T : TestEvent
{
public T Model { get; set; }
}

abstract class TestEvent
{
public DateTime DateTime = DateTime.Now;
}

class EventOne : TestEvent
{

}

class EventTwo:TestEvent
{

}
using MediatR;

IHost host = Host.CreateDefaultBuilder(args)
.ConfigureServices(services =>
{
services.AddMediatR(o=>o.RegisterServicesFromAssembly(typeof(Program).Assembly));
})
.Build();


host.Start();

var mediator = host.Services.GetRequiredService<IMediator>();

Console.WriteLine("Sending Event One");
await mediator.Publish(new EventNotification<EventOne>());

Console.WriteLine("Sending Event Two");
await mediator.Publish(new EventNotification<EventTwo>());

class EventHandler<TModel> : NotificationHandler<EventNotification<TModel>> where TModel : TestEvent
{
protected override void Handle(EventNotification<TModel> notification)
{
Console.WriteLine(notification.Model.DateTime);
}
}

class EventNotification<T> : INotification where T : TestEvent
{
public T Model { get; set; }
}

abstract class TestEvent
{
public DateTime DateTime = DateTime.Now;
}

class EventOne : TestEvent
{

}

class EventTwo:TestEvent
{

}
2 Replies
Mayor McCheese
Mayor McCheese9mo ago
I've talked to a few folks and they think this should work too. I might open a mediatr issue.
Accord
Accord9mo ago
Looks like nothing has happened here. I will mark this as stale and this post will be archived until there is new activity.