The topic shows this example:
Add{LIFETIME}<{SERVICE}>(new {IMPLEMENTATION})
Examples:
services.AddScoped(new MyDep());
services.AddScoped(new MyDep("A string!"));
But it seems to make no sense having a scoped or transient object constructed like that. In fact, I could not find any overload directly receiving an instance in AddScoped and AddTransient methods. I wonder if I missed something or it's a documentation error.
⚠ Do not edit this section. It is required for docs.microsoft.com ➟ GitHub issue linking.
Hello @natenho ... This was reviewed and signed off by engineering (aspnet/AspNetCore.Docs #13209) ... actually, one of the finest software engineers on the blue-green dot 🌎 imo.
I think it's fine. We don't want to re-ping engineering when they've signed off already. 🏃😅 If you want to discuss it further tho, open a Stack Overflow question or hit up the devs on these chats ...
If you ultimately feel that the document is incorrect about it, ping me back here. We can re-open the issue and ping engineering again for review. Let's just not do that unless you're fairly sure that the text/example is broken.
From @serpent5 ...
I think the OP is right there. Both of the functions called out won't compile and don't make sense for a scoped service.
@natenho Indeed, it is possible that it was missed in both content generation and review.
@anurse Plz put an :eye: on this one.
https://docs.microsoft.com/en-us/aspnet/core/fundamentals/dependency-injection?view=aspnetcore-2.2#service-registration-methods
... should I change those examples in the last row of the table to use AddSingleton?
... do u see anything else in that table that we should fix up while we're here?
Yeah, I believe it's correct that calling AddScoped with anything other than a type name or factory is wrong since the DI container can't control the lifetime.
In fact, the versions that take a specific instance are always going to be singleton since what you're doing is registering a specific instance in the DI container. There's no way to control the lifetime there. I believe we don't even dispose instances we're provided when the container is disposed. It's almost a completely different lifetime, "External", which is a super-set of Singleton.
Most helpful comment
Yeah, I believe it's correct that calling
AddScopedwith anything other than a type name or factory is wrong since the DI container can't control the lifetime.In fact, the versions that take a specific instance are always going to be singleton since what you're doing is registering a specific instance in the DI container. There's no way to control the lifetime there. I believe we don't even dispose instances we're provided when the container is disposed. It's almost a completely different lifetime, "External", which is a super-set of Singleton.