AddSingleton <>()vs AddSingleton()

问题描述 投票:1回答:2

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

services.AddSingleton(new MyCustomService())

services.AddSingleton<MyCustomService>()

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

c# asp.net-core .net-core dependency-injection
2个回答
1
投票
由您决定使用哪一个。但是,如果您的MyCustomService可能还有其他依赖性,我想AddSingleton<TService>(IServiceCollection)可能会更方便。

0
投票
© www.soinside.com 2019 - 2024. All rights reserved.