.net 核心后台应用程序中每个侦听器的单独控制器实例的 AddTransient 或 AddScoped
AddTransient or AddScoped for separate controller isntance per lisntener in .net core back ground application
我在后台服务应用程序中托管了一个 asp.net kesterl,并且 运行 在不同端口上的多个实例由一些特定对象控制。
我想要的是,如果当控制器出现故障时,应该使用我在初始初始化中传递的参数。每个监听器的控制器实例。
出于测试目的,我尝试了 Singleton,但它会产生单个实例。
现在我正在检查我应该为我的场景使用哪种实例(AddScoped 或 AddTransient)以及我如何在此期间注入参数。
我当前的代码库看起来像
internal static IHostBuilder CreateHostBuilder(string listener, CommunicationChannelElement communicationChannelElement)
{
//here i want how i can use addScoped or AddTransient with communication paramter
return Host.CreateDefaultBuilder().ConfigureServices(services => services.AddSingleton(communicationChannelElement)).
ConfigureWebHostDefaults(webBuilder => webBuilder.UseUrls(listener).
UseStartup<Startup>());
}
private void StartBackGroundWebListener(CommunicationChannelElement communicationChannelElement)
{
CreateHostBuilder(_objSettings, communicationChannelElement).Build().Run();//separate thread using task parallel library
}
public void StartAll()
{
StartBackGroundWebListener("http://10.1.2.3:0001",communicationChannelElement1);
StartBackGroundWebListener("http://10.1.2.3:0002",communicationChannelElement2);
StartBackGroundWebListener("http://10.1.2.3:0003",communicationChannelElement3);
}
public class CallBackController : ControllerBase
{
private readonly CommunicationChannelElement CommunicationChannelElement;
public CallBackController(CommunicationChannelElement communicationChannelElement)
{
CommunicationChannelElement = communicationChannelElement;
}
...
}
已通过以下代码实现
public class CallBackController : ControllerBase
{
private readonly CommunicationChannelElement CommunicationChannelElement;
public CallBackController(IServiceProvider serviceProvider)
{
CommunicationChannelElement = serviceProvider.GetService<CommunicationChannelElement>();
}
....
}
internal static IHostBuilder CreateHostBuilder(string listner, CommunicationChannelElement communicationChannelElement)
{
return Host.CreateDefaultBuilder().ConfigureServices(services => services.AddTransient(ctx => communicationChannelElement)).
ConfigureWebHostDefaults(webBuilder => webBuilder.UseUrls(listner).UseStartup<ComvivaStartup>());
//return Host.CreateDefaultBuilder().ConfigureServices(services => services.AddSingleton(communicationChannelElement)).
// ConfigureWebHostDefaults(webBuilder => webBuilder.UseUrls(listner).UseStartup<ComvivaStartup>());
}
如果需要任何改进,请告诉我
我在后台服务应用程序中托管了一个 asp.net kesterl,并且 运行 在不同端口上的多个实例由一些特定对象控制。 我想要的是,如果当控制器出现故障时,应该使用我在初始初始化中传递的参数。每个监听器的控制器实例。 出于测试目的,我尝试了 Singleton,但它会产生单个实例。 现在我正在检查我应该为我的场景使用哪种实例(AddScoped 或 AddTransient)以及我如何在此期间注入参数。
我当前的代码库看起来像
internal static IHostBuilder CreateHostBuilder(string listener, CommunicationChannelElement communicationChannelElement)
{
//here i want how i can use addScoped or AddTransient with communication paramter
return Host.CreateDefaultBuilder().ConfigureServices(services => services.AddSingleton(communicationChannelElement)).
ConfigureWebHostDefaults(webBuilder => webBuilder.UseUrls(listener).
UseStartup<Startup>());
}
private void StartBackGroundWebListener(CommunicationChannelElement communicationChannelElement)
{
CreateHostBuilder(_objSettings, communicationChannelElement).Build().Run();//separate thread using task parallel library
}
public void StartAll()
{
StartBackGroundWebListener("http://10.1.2.3:0001",communicationChannelElement1);
StartBackGroundWebListener("http://10.1.2.3:0002",communicationChannelElement2);
StartBackGroundWebListener("http://10.1.2.3:0003",communicationChannelElement3);
}
public class CallBackController : ControllerBase
{
private readonly CommunicationChannelElement CommunicationChannelElement;
public CallBackController(CommunicationChannelElement communicationChannelElement)
{
CommunicationChannelElement = communicationChannelElement;
}
...
}
已通过以下代码实现
public class CallBackController : ControllerBase
{
private readonly CommunicationChannelElement CommunicationChannelElement;
public CallBackController(IServiceProvider serviceProvider)
{
CommunicationChannelElement = serviceProvider.GetService<CommunicationChannelElement>();
}
....
}
internal static IHostBuilder CreateHostBuilder(string listner, CommunicationChannelElement communicationChannelElement)
{
return Host.CreateDefaultBuilder().ConfigureServices(services => services.AddTransient(ctx => communicationChannelElement)).
ConfigureWebHostDefaults(webBuilder => webBuilder.UseUrls(listner).UseStartup<ComvivaStartup>());
//return Host.CreateDefaultBuilder().ConfigureServices(services => services.AddSingleton(communicationChannelElement)).
// ConfigureWebHostDefaults(webBuilder => webBuilder.UseUrls(listner).UseStartup<ComvivaStartup>());
}
如果需要任何改进,请告诉我