Giter Club home page Giter Club logo

Comments (8)

Shiney avatar Shiney commented on August 15, 2024

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.

jbogard avatar jbogard commented on August 15, 2024

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.

Shiney avatar Shiney commented on August 15, 2024

I've added the awaits to my answer.

from mediatr.

FaridAhamat avatar FaridAhamat commented on August 15, 2024

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.

jbogard avatar jbogard commented on August 15, 2024

Events to one handler? Why not create separate handlers that depend on a common class?

from mediatr.

zachpainter77 avatar zachpainter77 commented on August 15, 2024

The way to achieve what you're after is here: #993 (comment)

from mediatr.

github-actions avatar github-actions commented on August 15, 2024

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.

github-actions avatar github-actions commented on August 15, 2024

This issue was closed because it has been stalled for 14 days with no activity.

from mediatr.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.