简体   繁体   中英

C# dependency injection transient scope with static class

C# question here. If I create a function app that injects a service in the startup.

services.AddTransient<IMyCoolService, MyCoolService>();

If there is a static class inside of MyCoolService , will that static get created every time this service is injected?

Edit: I'm aware static class cannot be created, but what I'm asking is, since the service is transient, and gets created each time, does that mean that the static class within it, is different each time as well?

Thanks

No, though the service is created several times, each of these instances uses the "same" static members of the static class.

A static class cannot be instantiated. If there is a static field that is used by the service, its content will be the same for all the instances of the service.

If you want to create an instance of the static class for each service instance, you have to remove the static keyword from the class and the members and register it as transient also.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM