MediatR INotification Handler执行不保证顺序!!!
注册:

源码调试:


public class NewUserM : INotification
{
public string Username { get; set; }
public string Password { get; set; }
}
public class EmailHandler: INotificationHandler<NewUserM>
{
private static NLog.Logger log = NLog.LogManager.GetCurrentClassLogger();
public Task Handle(NewUserM notification, CancellationToken cancellationToken)
{
Thread.Sleep(3000);
log.Info("EmailHandler OK");
return Task.FromResult(true);
}
}
public class NewUserMHandler : INotificationHandler<NewUserM>
{
private static NLog.Logger log = NLog.LogManager.GetCurrentClassLogger();
public Task Handle(NewUserM notification, CancellationToken cancellationToken)
{
log.Info("NewUserMHandler OK");
return Task.FromResult(true);
}
}
//Controller
/// <summary>
/// 模拟多处理器
/// </summary>
/// <param name="user"></param>
/// <returns></returns>
public ActionResult Register2(NewUserM user)
{
_mediator.Publish(user);
return RedirectToAction("Login");
}
//startup
var types = new[] {
typeof(NewUserHandler),
typeof(LogHandler),
typeof(NewUserMHandler),
typeof(EmailHandler),
};
services.AddMediatR(types);
over