相关疑难解决方法(0)

.NET Core DI,将参数传递给构造函数的方法

拥有以下服务构造函数

public class Service : IService
{
     public Service(IOtherService service1, IAnotherOne service2, string arg)
     {

     }
}
Run Code Online (Sandbox Code Playgroud)

使用.NET Core IOC机制传递参数有哪些选择?

_serviceCollection.AddSingleton<IOtherService , OtherService>();
_serviceCollection.AddSingleton<IAnotherOne , AnotherOne>();
_serviceCollection.AddSingleton<IService>(x=>new Service( _serviceCollection.BuildServiceProvider().GetService<IOtherService>(), _serviceCollection.BuildServiceProvider().GetService<IAnotherOne >(), "" ));
Run Code Online (Sandbox Code Playgroud)

还有其他方法吗?

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

52
推荐指数
4
解决办法
2万
查看次数