I have ASP.NET WebApi with one service which is added as a singleton in the following way:
services.AddSingleton<IService, Service>();
As I can see constructor of this service is called not once (like we suppose to be as a singleton) but multiple times. From what I understand it’s executed once per thread. Thread pool contains multiple threads.
And question is. Is one thread from the pool reused? Is it true and possible that another request will get the instance of service which was created by earlier request? Seems that yes and it’s why we should use AddSingleton only for stateless services. But to be honest I don’t know how to check it and simulate the case to get instance of service which was created earlier.
Or I understand it incorrectly?
Tried to execute multiple request to get the same instance of the service.
2
Answers
The pool threads are reused. If you add it to the services as you did and use dependency injection everyone who accesses your server will have the same instance. So for example :
Your service has an :
int foo=Random.Range(0,5)
and the value is now 5everyone will see this singleton with the
int foo=5
.If you use for example : ‘
AddScoped
‘ instead of ‘AddSingleton
‘ everyone will have a new instance of the object. So someone would seefoo=1
, another one will seefoo=3
and so on.So yes the service will get the instance that was created earlier.
I hope I was able to clarify it for you 🙂
MS.DI will guarantee that the constructor of a singleton is only called at most once, even in a multi-threaded environment, except when:
Whether or not singletons are resolved from multiple threads doesn’t change the fact that a single container guarantees only one instance of a singleton.