简体   繁体   English

ASP.NET Core线程中的默认IServiceProvider是否安全?

[英]Is the default IServiceProvider in ASP.NET Core thread safe?

I'm building a small ASP.NET Core app that also has a bunch of separate business logic that runs in its own thread. 我正在构建一个小型ASP.NET Core应用程序,该应用程序还具有在其自己的线程中运行的一堆单独的业务逻辑。 I was already using a IServiceProvider in the side program, so when I found out that ASP.NET Core also uses its own IServiceProvider , so I thought I could re-use only a single instance. 我已经在辅助程序中使用了IServiceProvider ,因此当我发现ASP.NET Core也使用其自己的IServiceProvider ,我以为我只能重用一个实例。

Now the question is, is the IServiceProvider that the web host uses thread safe? 现在的问题是,Web主机使用线程的IServiceProvider是否安全? My setup basically looks like this 我的设置基本上像这样

var host = new WebHostBuilder()
            .UseKestrel()
            .UseContentRoot(Directory.GetCurrentDirectory())
            .UseIISIntegration()
            .UseStartup<Startup>()
            .UseApplicationInsights()
            .Build();

Task.Run(() => {
  // here I access the IServiceProvider via `host.Services`
  new Foo(host.Services).Run();
});

host.Run();

The default service provider implementation . 默认的服务提供者实现 You can see services factories are stored in ConcurrentDictionary and building an expression for creating service object is executed in separate thread except the first one. 您可以看到服务工厂存储在ConcurrentDictionary并且在第一个线程之外的单独线程中执行构建用于创建服务对象的表达式 And I am sure this expression is also thread-safe (likely stateless). 而且我确信此表达式也是线程安全的(可能是无状态的)。 So the default ServiceProvider is thread-safe. 因此,默认的ServiceProvider是线程安全的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM