C# 处理程序会为同一个通知触发多次,但自动关联会按预期工作
我有带StructureMap和Mediatr的ASP.NET Core 3.1 以前,当我使用默认DI容器时,我会让我的子处理程序触发两次。那里的功能非常有限,所以我切换到StructureMap。但是,当我对扫描器应用不同的设置时,我仍然无法获得预期的行为。它是执行两次的基处理程序或子处理程序,或者两者都执行。唯一的工作情况是,我没有为C# 处理程序会为同一个通知触发多次,但自动关联会按预期工作,c#,.net-core,structuremap,mediatr,C#,.net Core,Structuremap,Mediatr,我有带StructureMap和Mediatr的ASP.NET Core 3.1 以前,当我使用默认DI容器时,我会让我的子处理程序触发两次。那里的功能非常有限,所以我切换到StructureMap。但是,当我对扫描器应用不同的设置时,我仍然无法获得预期的行为。它是执行两次的基处理程序或子处理程序,或者两者都执行。唯一的工作情况是,我没有为typeof(INotificationHandler)指定任何内容,由于(可能?)的原因,一切都可以正常工作,但我觉得通知处理程序调用之间存在延迟,所以我希
typeof(INotificationHandler)
指定任何内容,由于(可能?)的原因,一切都可以正常工作,但我觉得通知处理程序调用之间存在延迟,所以我希望在应用程序启动时正确配置容器
下面是我的通知和通知处理程序。
预期行为:发布子通知时,触发两个处理程序一次
public class ApplicationUserRegisteredNotification : INotification
{
public ApplicationUser User { get; }
public ApplicationUserRegisteredNotification(ApplicationUser user)
{
User = user;
}
}
public class SpecificUserRegisteredNotification : ApplicationUserRegisteredNotification
{
public SpecificUserProfile SpecificUser { get; }
public SpecificUserRegisteredNotification(SpecificUserProfile specificUser)
: base(specificUser.User)
{
SpecificUser = specificUser;
}
}
public class SendEmailConfirmationEmail : INotificationHandler<ApplicationUserRegisteredNotification>
{
// some code
}
public class SendAdminVerifySpecificUserRegistration : INotificationHandler<SpecificUserRegisteredNotification>
{
// some code
}
Program.cs
public class Program
{
public static void Main(string[] args)
{
CreateHostBuilder(args).Build().Run();
}
public static IWebHostBuilder CreateHostBuilder(string[] args) =>
WebHost.CreateDefaultBuilder(args)
.UseStructureMap()
.UseStartup<Startup>();
}
公共类程序
{
公共静态void Main(字符串[]args)
{
CreateHostBuilder(args.Build().Run();
}
公共静态IWebHostBuilder CreateHostBuilder(字符串[]args)=>
WebHost.CreateDefaultBuilder(args)
.UseStructureMap()
.UseStartup();
}
在StructureMap之前,我的配置是这样的
services.AddTransient<INotificationHandler<SpecificUserRegisteredNotification>, SendEmailConfirmationEmail>();
services.AddTransient<INotificationHandler<OtherSpecificUserRegisteredNotification>, SendEmailConfirmationEmail>();
services.AddTransient<IRequestHandler<RegisterSpecificCommand, BaseCommandResponse>, RegisterApplicationUserCommandHandler>();
services.AddTransient<IRequestHandler<RegisterOtherSpecificCommand, BaseCommandResponse>, RegisterApplicationUserCommandHandler>();
services.AddTransient();
services.AddTransient();
services.AddTransient();
services.AddTransient();
services.AddTransient<INotificationHandler<SpecificUserRegisteredNotification>, SendEmailConfirmationEmail>();
services.AddTransient<INotificationHandler<OtherSpecificUserRegisteredNotification>, SendEmailConfirmationEmail>();
services.AddTransient<IRequestHandler<RegisterSpecificCommand, BaseCommandResponse>, RegisterApplicationUserCommandHandler>();
services.AddTransient<IRequestHandler<RegisterOtherSpecificCommand, BaseCommandResponse>, RegisterApplicationUserCommandHandler>();