Search code examples
c#.net.net-coremediatormediatr

Notification Handler don't receiving notification after mediator.publish in .NET-Core


I created a notification class called SendMail as follows:

 public class SendMail : INotification
    {
        public string From { get; set; }
        public string To { get; set; }
        public string Subject { get; set; }
        public string Message { get; set; }
    }

After that, I implemented the SendMailNotificationHandler:

public class SendMailNotificationHandler: INotificationHandler<SendMail>
    {
        public Task Handle(SendMail notification, CancellationToken cancellationToken)
        {
            Console.WriteLine("Testing");

            return Task.CompletedTask;
        }

And at last I did the procedure to publish a notification in the system in my RequestHandler:

public Task<OperationResult> Handle(CreateMailRequest request, CancellationToken cancellationToken)
        {
            // Just for example
            _repo.Insert(anything);

            _mediator.Publish(new SendMail
            {
                From = "[email protected]",
                To = "[email protected]",
                Subject = "New message",
                Message = "New test message"
            }, cancellationToken);


            return OperationResult.Success().AsTask;
        }

The problem is, when debugging I see the publish method is run, but NotificationHandler never catches this notification of a new mail created.
I don't what is happening. Could someone help with this?


Solution

  • The problem was in my SimpleInjectorBootstrap.cs. I had to add the following code in my GetAssemblies method:

    private static IEnumerable<Assembly> GetAssemblies()
            {
                yield return typeof(ExceptionPipelineBehavior<,>).GetTypeInfo().Assembly;
    
                //added line
                yield return typeof(ReportCreatedNotificationHandler).GetTypeInfo().Assembly;
            }