我创建了一个名为SendMail的通知类,如下图所示。
public class SendMail : INotification
{
public string From { get; set; }
public string To { get; set; }
public string Subject { get; set; }
public string Message { get; set; }
}
然后,我实现了SendMailNotificationHandler。
public class SendMailNotificationHandler: INotificationHandler<SendMail>
{
public Task Handle(SendMail notification, CancellationToken cancellationToken)
{
Console.WriteLine("Testing");
return Task.CompletedTask;
}
最后,我做了一个在系统中发布通知的程序 在我的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;
}
问题是,当调试时,我看到发布方法被运行了,但NotificationHandler从来没有捕捉到新邮件创建的通知。我不知道这是怎么回事。谁能帮忙解决这个问题?
问题出在我的SimpleInjectorBootstrap.cs中。我不得不在我的GetAssemblies方法中添加以下代码。
private static IEnumerable<Assembly> GetAssemblies()
{
yield return typeof(ExceptionPipelineBehavior<,>).GetTypeInfo().Assembly;
//added line
yield return typeof(ReportCreatedNotificationHandler).GetTypeInfo().Assembly;
}