简体   繁体   English

解决C#Unity构造函数时出错

[英]Error resolving c# Unity constructor

Question Background: 问题背景:

I am currently configuring a Unity container and am having issues setting the constructor of a class. 我目前正在配置Unity容器,在设置类的构造函数时遇到问题。

The class in question's constructor is set as shown. 所设置的类的构造函数如下所示。 It takes in 3 string parameters and then 2 objects that I am setting up in the container. 它接受3个字符串参数,然后接受我在容器中设置的2个对象。

public VersionControlFacade(string serverPath, string username, string password, IConnectionManager connectionManager, IPromoManager promoManager)

The configured Unity container for the above class is as shown: 为上述类配置的Unity容器如下所示:

container.RegisterType<IPromoManager, promotionManager>();
container.RegisterType<IConnectionManager, connectionManager>();
container.RegisterType<ITfsVersionControlFacade, TfsVersionControlFacade>(new InjectionConstructor(connectionString, username, password));

The Error: 错误:

Currently when trying to resolve the UnityContainer object, the following exception is being thrown: 当前,当尝试解析UnityContainer对象时,将引发以下异常:

The type VersionControlFacade does not have a constructor that takes the parameters (String, String, String) 类型VersionControlFacade没有使用参数(字符串,字符串,字符串)的构造函数

I understand this, I am indeed passing in two other parameters, but it was my belief that as I have registered IPromoManager and IConnectionManager that these would be resolved and automatically injected into the VersionControlFacade constructor? 我知道这一点,我确实确实传入了另外两个参数,但是我相信当我注册IPromoManagerIConnectionManager ,这些参数将被解析并自动注入到VersionControlFacade构造函数中吗?

Can anyone tell me where the logic is wrong here, and what I can do to resolve it? 谁能告诉我这里的逻辑哪里错了,我该怎么做才能解决呢?

I believe you need: 我相信您需要:

container.RegisterType<ITfsVersionControlFacade, TfsVersionControlFacade>(new InjectionConstructor(connectionString, username, password, typeof(IConnectionManager), typeof(IPromoManager)));

See 看到

Unity InjectionConstructor for multiparam constructor overriding only single one 用于多参数构造函数的Unity InjectionConstructor仅覆盖单个一个

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

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