Comments (8)
Hello I may be misunderstanding your question but for me this code (as a program.cs) where I just create a non-generic INotificationHandler which implements INotificationHandler (which is my base event) seems to get called fine
using MediatR;
using Microsoft.Extensions.DependencyInjection;
using mytest;
// Create a new service collection and add mediatr
var services = new ServiceCollection();
services.AddMediatR(cfg => cfg.RegisterServicesFromAssembly(typeof(Program).Assembly));
// Build the service provider
var serviceProvider = services.BuildServiceProvider();
using var scope = serviceProvider.CreateScope();
var mediatr = scope.ServiceProvider.GetRequiredService<IMediator>();
await mediatr.Publish(new Notification1());
await mediatr.Publish(new Notification2());
internal record NotificationBase : INotification
{
}
internal record Notification1 : NotificationBase
{
}
internal record Notification2 : NotificationBase
{
}
internal class NotificationHandler1 : INotificationHandler<Notification1>
{
public Task Handle(Notification1 notification, CancellationToken cancellationToken)
{
Console.WriteLine("NotificationHandler1");
return Task.CompletedTask;
}
}
internal class NotificationHandler2 : INotificationHandler<Notification2>
{
public Task Handle(Notification2 notification, CancellationToken cancellationToken)
{
Console.WriteLine("NotificationHandler2");
return Task.CompletedTask;
}
}
internal class NotificationHandlerBase : INotificationHandler<NotificationBase>
{
public Task Handle(NotificationBase notification, CancellationToken cancellationToken)
{
Console.WriteLine($"NotificationHandlerBase with {notification.ToString()}");
return Task.CompletedTask;
}
}
Works and I get the output below
NotificationHandler1
NotificationHandlerBase with Notification1 { }
NotificationHandler2
NotificationHandlerBase with Notification2 { }
from mediatr.
You're not awaiting anything. Publish
returns a Task and needs to be awaited. It works in a console because of the disposing at the end of the execution.
from mediatr.
I've added the awaits to my answer.
from mediatr.
Yes, I have the same problem. Notification solves 1-to-many handler problem, but the problem I have now is many events-to-1 handler. I've tried with playing around the event class' hierarchies, but some of my event just don't make sense from hierarchy point of view.
Any idea how to solve this?
from mediatr.
Events to one handler? Why not create separate handlers that depend on a common class?
from mediatr.
The way to achieve what you're after is here: #993 (comment)
from mediatr.
This issue is stale because it has been open 60 days with no activity. Remove stale label or comment or this will be closed in 14 days.
from mediatr.
This issue was closed because it has been stalled for 14 days with no activity.
from mediatr.
Related Issues (20)
- Can't get IPipelineBehavior to work in F# HOT 3
- I have problem with IRequestPreProcessor pipeline
- Logging any Exception thrown in a handler HOT 2
- Specify Lifetime in Handlers feature missing HOT 2
- Seperate Interfaces to Contract project to provide abstraction layer HOT 8
- Improve base package readme
- Implement IRequest, how to enter Behavior HOT 1
- IRequestExceptionHandler from other project not loaded
- Question:
- Question: How I can setup MediatR 12.1.0 in Ninject module? HOT 2
- How do I register INotification with generics? HOT 2
- long running INotificationHandler HOT 3
- Question: Can multiple MediatR handlers be packaged into a single class HOT 3
- .NET8 upgrade from .NET Core 2.1 : Command cannot be used as type parameter 'TRequest' for 'IRequestHandler<TRequest>' HOT 3
- Irequest in library and handler in web api project HOT 2
- what use net8 aot publish HOT 2
- v12.3.0 Breaking Change HOT 27
- Question: Why IRequestExceptionHandler is not catching? HOT 1
- Question: How to register generic requests HOT 8
- Can't register generic handlers in latest version HOT 5
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from mediatr.