简体   繁体   English

AddTransient 或 AddScoped 用于单独的 controller 在 .net 核心后台应用程序中的每个监听器

[英]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.

相关问题 从AddTransient到AddScoped的ASP.NET Core依赖项注入 - ASP.NET Core dependency injection from AddTransient to AddScoped AddTransient、AddScoped 和 AddSingleton 服务的区别 - AddTransient, AddScoped and AddSingleton Services Differences Lamar AddScoped 未按预期工作网络核心 webapi - Lamar AddScoped not working as expected net core webapi 在启动时调用 AddTransient 中的异步方法 - Asp.Net Core - Call async method in AddTransient in Startup - Asp.Net Core 在 Asp.Net Core 的 AddTransient 方法中使用 generics - Use generics in AddTransient method in Asp.Net Core 从HTML发布到单独的.NET Core应用程序 - Post from HTML to a separate .NET Core Application 在.NET Core内置DI容器中为AddScoped服务实现自定义处理 - Implement custom dispose for AddScoped services in .NET Core built-in DI Container ASP.net 核心:根据 controller 和/或操作配置响应序列化文化 - ASP net core: Configure response serialization culture per controller and/or action 更改服务。AddScoped<unitofwork> (); 到 services.AddTransient(); 因为没有 dispose 方法?</unitofwork> - Change services.AddScoped<UnitOfWork>(); to services.AddTransient(); because of no dispose method? ASP.NET Core 中每个控制器的不同 JSON 反序列化设置 - Different JSON deserialization settings per controller in ASP.NET Core
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM