相关疑难解决方法(0)

Microsoft Unity.如何在构造函数中指定某个参数?

我正在使用Microsoft Unity.我有一个接口ICustomerService及其实现CustomerService.我可以使用以下代码为Unity容器注册它们:

container.RegisterType<ICustomerService, CustomerService>(new TransientLifetimeManager());
Run Code Online (Sandbox Code Playgroud)

如果CustomerService在其构造函数中有某个参数(例如ISomeService1),我使用以下代码(我需要指定SomeService1):

container.RegisterType<ICustomerService, CustomerService>(new TransientLifetimeManager(), new InjectionConstructor(new SomeService1()));
Run Code Online (Sandbox Code Playgroud)

这里没问题.

CustomerService类在其构造函数中有两个参数(不是前一个示例中的一个参数)时会出现问题(例如ISomeService1ISomeService2).当我使用以下代码时它工作正常: container.RegisterType<ICustomerService, CustomerService>(new TransientLifetimeManager(), new InjectionConstructor(new SomeService1(), new SomeService2()));

问题是我不想指定SomeService2()第二个参数.我只想指定第一个参数 - SomeService1().但我得到的错误是我需要指定一个或两个参数.

如何只指定构造函数的第一个参数?

c# asp.net ioc-container unity-container

33
推荐指数
4
解决办法
3万
查看次数

标签 统计

asp.net ×1

c# ×1

ioc-container ×1

unity-container ×1