0

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 = "test@test.com",
                To = "test@test.com",
                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?

Isac Moura
  • 5,940
  • 3
  • 13
  • 27

2 Answers2

1

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;
        }
Isac Moura
  • 5,940
  • 3
  • 13
  • 27
1

Add an assembly of the given project or class library entry point:

builder.Services.AddMediatR(typeof(MediatrEntryPoint).Assembly , typeof(Program).Assembly);

If there is no entry point in the class library, then add an empty class and name it <Library>entryPoint, then add it to the assembly.

Jeremy Caney
  • 7,102
  • 69
  • 48
  • 77