The thread-safety section talks about services:
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.
However, it would help to also add a word or two about the thread-safety of a _singleton factory_ when using something like AddSingleton<TService>(IServiceCollection, Func<IServiceProvider,TService>), as in:
c#
services.AddSingleton<IFoo>(sp =>
{
// initialization code...
return new Foo(/* ... */);
});
One would imagine that the factory will be called lazily, when the service is requested for the first time. What happens, though, if multiple threads request the singleton service simultaneously that in turn incurs its initialization?
I'm guessing it's the first case but it would help to clarify in that section. It avoids:
โ Do not edit this section. It is required for docs.microsoft.com โ GitHub issue linking.
Hello @atifaziz ... I'm working on this topic _TODAY!_ ๐
Samples are updated, and I'm getting into the topic content now. I'll get back to you soon.
btw ... My name is "Luke," so rest assured I'll use the Force on this.
... and the Force says to ping @davidfowl ...
@davidfowl, I can wordsmith your answers to questions 1 & 2 for that section of the topic.
Yes initialization code runs once and everything waits on it.
@davidfowl Thanks for the clarification.
Here's a proposal of the section could be expanded:
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.
The factory method of single service, such as the second argument to
AddSingleton<TService>(IServiceCollection, Func<IServiceProvider,TService>), does not need to be thread-safe. Like a type (static) constructor, it's guaranteed to be called once and by one thread alone.
Thanks ... I'll get this on the PR that I'm putting up ~Monday~ soon (still working on this).
Most helpful comment
Hello @atifaziz ... I'm working on this topic _TODAY!_ ๐
Samples are updated, and I'm getting into the topic content now. I'll get back to you soon.
btw ... My name is "Luke," so rest assured I'll use the Force on this.