简体   繁体   English

Unity 不使用类的默认构造函数

[英]Unity not using the default constructor of the class

I have this class :我有这门课:

public class Repo
{
   public Repo() : this(ConfigurationManager.AppSettings["identity"],       ConfigurationManager.AppSettings["password"])

    {

    }

   public Repo(string identity,string password)
   {
       //Initialize properties.
   }

}

I added a line to web.config so that this type will be automatically constructed by Unity container.我在 web.config 中添加了一行,以便 Unity 容器自动构建此类型。

but during the execution of my application, I receive this error message :但在我的应用程序执行期间,我收到此错误消息:

  "System.InvalidOperationException : the parameter identity could not be resolved when attempting to call constructor Repo(String identity, String password)  -->Microsoft.Practices.ObjectBuilder2.BuildFailedException : The current Build operation ...."

1) Why isn't Unity using the default constructor ? 1) 为什么 Unity 不使用默认构造函数?

2) Suppose I want Unity to use the second constructor (the parametized one), How do I pass that information to Unity via the configuration file ? 2)假设我希望 Unity 使用第二个构造函数(参数化的构造函数),我如何通过配置文件将该信息传递给 Unity?

Unity by default picks the constructor with the most parameters. Unity 默认选择参数最多的构造函数。 You have to tell Unity to use a different one explicitly.您必须明确告诉 Unity 使用不同的。

One way to do this is with the [InjectionConstructor] attribute like this:一种方法是使用 [InjectionConstructor] 属性,如下所示:

using Microsoft.Practices.Unity;

public class Repo
{
   [InjectionConstructor]
   public Repo() : this(ConfigurationManager.AppSettings["identity"], ConfigurationManager.AppSettings["password"])
   {

   }

   public Repo(string identity,string password)
   {
       //Initialize properties.
   }
}

A second way of doing this, if your opposed to cluttering up classes/methods with attributes, is to specify which constructor to use when configuring your container using an InjectionConstructor :第二种方法是,如果您反对使用属性混淆类/方法,则是在使用InjectionConstructor配置容器时指定要使用的构造函数:

IUnityContainer container = new UnityContainer();
container.RegisterType<Repo>(new InjectionConstructor());

From the documentation :文档

How Unity Resolves Target Constructors and Parameters Unity 如何解析目标构造函数和参数

When a target class contains more than one constructor, Unity will use the one that has the InjectionConstructor attribute applied.当目标类包含多个构造函数时,Unity 将使用应用了 InjectionConstructor 属性的构造函数。 If there is more than one constructor, and none carries the InjectionConstructor attribute, Unity will use the constructor with the most parameters.如果有多个构造函数,并且没有一个带有 InjectionConstructor 属性,Unity 将使用参数最多的构造函数。 If there is more than one such constructor (more than one of the "longest" with the same number of parameters), Unity will raise an exception.如果有多个这样的构造函数(多个具有相同参数数量的“最长”构造函数),Unity 将引发异常。

Just try to register type this way:只需尝试以这种方式注册类型:

<register type="IRepo" mapTo="Repo">
  <constructor />
</register>

Because of no param element specified in constructor element it should call default constructor.由于在constructor元素中没有指定param元素,它应该调用默认构造函数。

You can also do this registration in code:您也可以在代码中进行此注册:

container.RegisterType<IRepo, Repo>(new InjectionConstructor());

I had a more simple problem that resulted in this error.我有一个更简单的问题导致了这个错误。

The container that I was using was the wrong container .我用的容器错误的容器 I had accidentally created two different containers and my container.RegisterType<Interface,ConcreteObject> was inside of another container from the one being used.我不小心创建了两个不同的容器,我的container.RegisterType<Interface,ConcreteObject>位于正在使用的另一个容器内。

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

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