小编Nat*_*ung的帖子

AddSingleton<>() 与 AddSingleton()

我正在我的项目中实现依赖注入。我遇到过两种添加单例服务的方法 -

services.AddSingleton(new MyCustomService())
Run Code Online (Sandbox Code Playgroud)

services.AddSingleton<MyCustomService>()
Run Code Online (Sandbox Code Playgroud)

两种方法似乎都有效。这两种方法有什么区别,我应该使用哪一种?

c# dependency-injection .net-core asp.net-core

7
推荐指数
1
解决办法
8833
查看次数