带有依赖注入参数的Unity Call构造函数 [英] Unity Call constructor with parameter on Dependency Injection

查看:94
本文介绍了带有依赖注入参数的Unity Call构造函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我创建了一个Unity Dependency Injection项目。在app config中创建了一个构造函数。错误被抛出为无法识别的元素'构造函数'。请帮助我。谢谢。



Hi,
I created a unity Dependency Injection project. Created a constructor in app config. The error thrown as "Unrecognized element 'constructor'. Please help me on this. Thank you.

<?xml version="1.0"?>
<configuration>
  <configSections>
    <section name="unity" type="Microsoft.Practices.Unity.Configuration.UnityConfigurationSection,Microsoft.Practices.Unity.Configuration"/>
    <section name="unity1" type="Microsoft.Practices.Unity.Configuration.UnityConfigurationSection,Microsoft.Practices.Unity.Configuration"/>
  </configSections>
  <unity>
    <containers>
      <container>
        <types>
          <type type="Sample1.IDB,Sample1" mapTo="Sample1.oracle,Sample1"  name="Oracle"> </type>
          <type type="Sample1.IDB,Sample1" mapTo="Sample1.sql,Sample1"  name="Sql"></type>
          <type type="Sample1.IDB,Sample1" mapTo="Sample1.oracle,Sample1"  name="parOracle">
            <constructor>
              <param name="status" value="oracle" parameterType="System.String"/>
            </constructor>
          </type>
          <type type="Sample1.IDB,Sample1" mapTo="Sample1.sql,Sample1"  name="parSql">
            <constructor>
              <param name="status" value="sql" parameterType="System.String"/>
            </constructor>
          </type>
        </types>
      </container>
    </containers>
  </unity>
  <appSettings>
    <add key="Types" value="Oracle" />
  </appSettings>

</configuration>



< br $>


IUnityContainer container = new UnityContainer();



UnityConfigurationSection section =(UnityConfigurationSection)ConfigurationManager.GetSection(团结);



这里它的抛出错误为无法识别的元素'构造函数'。

在应用程序配置中我更改了要注册的类型。然后它的抛出错误为无法识别的元素'寄存器'。它在类型和类型中正常工作。但是在添加构造函数后抛出错误。



实际错误是无法识别的元素'constructor'。(D:\Sample1 \ Sample1 \ Sample1 \bin\Debug \Sample1.vshost.exe.Config第14行)




IUnityContainer container = new UnityContainer();

UnityConfigurationSection section = (UnityConfigurationSection)ConfigurationManager.GetSection("unity");

here its throwing error as "Unrecognized element 'constructor'.
In app config I changed the Types to register. Then its throwing error as "Unrecognized element 'register'. Its working normaly in Types and Type. But after adding the constructor throwing an error.

Actual error is Unrecognized element 'constructor'. (D:\Sample1\Sample1\Sample1\bin\Debug\Sample1.vshost.exe.Config line 14)

推荐答案

use <register/> instead of type, there is no tag named type in unity

<register type="Sample1.IDB,Sample1" mapTo="Sample1.oracle,Sample1"  name="parOracle">
            <constructor>
              <param name="status" value="oracle" parameterType="System.String"/>
            </constructor>
</register>

enjoy :)


这篇关于带有依赖注入参数的Unity Call构造函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆