Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ASP.NET Core pipeline multithreading

Does ASP.NET Core pipeline handle requests by multithreading? If it does, how do you configure the number of threads? And also, should singleton services be thread safe?

like image 638
Roman Kolesnikov Avatar asked Jun 29 '16 11:06

Roman Kolesnikov


1 Answers

The first question was already answered in the comment above (look into KestrelServerOptions)

Regarding thread safetly, the answer is in the documentation:

Singleton lifetime services are created the first time they are requested (or when ConfigureServices is run if you specify an instance there) and then every subsequent request will use the same instance. If your application requires singleton behavior, allowing the services container to manage the service's lifetime is recommended instead of implementing the singleton design pattern and managing your object's lifetime in the class yourself.

That means all requests for the service pull the same object, which means no per-thread objects, and thus no thread safety.

Thread safety

Singleton services need to be thread safe. If a singleton service has a dependency on a transient service, the transient service may also need to be thread safe depending how it’s used by the singleton.

Coudn't be more clear. Since the objects are not created per thread, they are not thread safe by default (though it's possible some services are designed to be).

like image 195
James Wilkins Avatar answered Sep 28 '22 02:09

James Wilkins