繁体   English   中英

C# 依赖注入瞬态 scope 与 static class

[英]C# dependency injection transient scope with static class

C# 问题在这里。 如果我创建一个在启动时注入服务的 function 应用程序。

services.AddTransient<IMyCoolService, MyCoolService>();

如果 MyCoolService 内部有MyCoolService ,那么每次注入此服务时都会创建 static 吗?

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?

谢谢

不,尽管服务被创建了多次,但这些实例中的每一个都使用 static class 的“相同”static 成员。

无法实例化 static class。 如果服务使用了 static 字段,则其内容对于服务的所有实例都是相同的。

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.

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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