Home > database >  Call "AddScoped" within / inside "AddTransient" in DotNet Core?
Call "AddScoped" within / inside "AddTransient" in DotNet Core?

Time:09-19

I do have basic understanding between AddTransient Vs AddScoped Vs AddSingleton. I know how these 3 behave individually but I am not sure if we can call these 3 instance types within each other and if YES then how my application will behave in that scenario?

I tried reading couple of blogs online but couldn't get answer to this specific case. I am not sure if it's possible to call these instance within each other or not possible at all?

Thanks.

CodePudding user response:

Singleton cannot consume a Transient or Scoped Service.

A Transient service can consume any of the three.

A Scoped service can consume any of the three.

A Transient injected into a Scoped service takes on the lifetime of the Scoped service.

Within a scope, multiple Transients of the same type that depend on Scoped services will get new instances of the Transient service, but the same instance of the Scoped service will be injected into each.

And, of course, any Transient or Scoped service that depends on a Singleton service will always receive the single instance of that service.

CodePudding user response:

enter image description here

A long Lifetime service cannot request a shorter Lifetime service..
Because during its lifetime, several short-term services may change, and it would not be correct to randomly select any of them.

  • Related