[英]AddTransient or AddScoped for separate controller isntance per lisntener in .net core back ground application
I have hosted a asp.net kesterl inside background service application and running multiple instances on different port of same controlled with some specific objects.我在后台服务应用程序中托管了一个 asp.net kesterl,并在使用某些特定对象控制的同一端口的不同端口上运行多个实例。 What I want's if when ever a hit comes on controller the parameter i passed in initial initialization should be used.
我想要的是,如果 controller 受到打击,应该使用我在初始初始化中传递的参数。 sort of separate instance of controller per listner.
每个listner的controller的单独实例。 For test purpose I have tried the Singleton but it will result single instance.
出于测试目的,我尝试了 Singleton 但它会产生单实例。 Now I am checking which sort of instance (AddScoped or AddTransient) should I use for my scenario and how can i inject parameter during that.
现在我正在检查我应该为我的场景使用哪种类型的实例(AddScoped 或 AddTransient),以及如何在此期间注入参数。
My current code base looks like我当前的代码库看起来像
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;
}
...
}
Have achieved it via following code已通过以下代码实现
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>());
}
if there is any improvement needed then please let me know如果需要任何改进,请告诉我
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.