Yes and no. AddSingleton
will register a single instance of service per built service provider:
var services = new ServiceCollection();
services.AddSingleton<MySingleton>();
var sp1 = services.BuildServiceProvider();
var sp2 = services.BuildServiceProvider();
// prints "False":
Console.WriteLine(object.ReferenceEquals(sp1.GetService<MySingleton>(), sp2.GetService<MySingleton>()));
Though usually having multiple service providers (especially built from the same collection of services) per application is an error and almost all production level apps I've seen had only one.
As for "standard" Singleton - if we will not go down the rabbit hole of multiple application domains (maybe there are other edge cases also) then correctly implemented one (which can be not that trivial) should result in one instance per app.
So in majority of applications the difference between the two boils down to access and ownership patterns (i.e. using DI or not) which is covered by the duplicate in the linked answer. If your app already leverages DI I highly recommend to register singletons with AddSingleton
(and possibly use interfaces to extract the dependency contract) but note that you can always pass an instance (for example an implemented via Singleton pattern one) to it i.e. (which will change the result of object.ReferenceEquals
in the snippet above to True
):
services.AddSingleton(MySingleton.Instance);
P.S.
Note that "classic" Singleton can be considered as an anti-pattern.