统一xml配置通用接口

时间:2012-09-23 18:58:23

标签: .net configuration unity-container

我正在使用Unity进行构造函数注入。通过运行时API的构造函数注入成功,使用以下代码:

{
using ContractImplementations;
using Contracts;
using DataAccess;
using DataModel.Entities;
using DataModel.Interfaces;

using Microsoft.Practices.Unity;

using Unity.Wcf;

/// <summary>
/// The wcf service factory.
/// </summary>
public class WcfServiceFactory : UnityServiceHostFactory
{
    #region Methods
    /// <summary>
    /// Configure container.
    /// </summary>
    /// <param name="container">
    /// The container.
    /// </param>
    protected override void ConfigureContainer(IUnityContainer container)
    {
        container
           .RegisterType<IGaugeModelbaseService, GaugeModelbaseService>()
           .RegisterType<IContractMapper, ContractMapper>(new HierarchicalLifetimeManager())
           .RegisterType<IGenericRepository<GaugeModel>, GenericSqlRepository<GaugeModel>>(new HierarchicalLifetimeManager());
    }

    #endregion
}

}

由于与AppFabric和EntLib的集成,我必须在XML配置中配置容器。问题:关于注册泛型类型的主题,统一文档并不清楚。根据文档,我必须做这样的事情:

<?xml version="1.0" encoding="utf-8"?>

                  

<namespace name="Design.ModelbaseSvc" />
<assembly name="Design.ModelbaseSvc" />
<namespace name="Design.ContractImplementations" />
<assembly name="Design.ContractImplementations" />
<namespace name="Design.DataModel" />
<assembly name="Design.DataModel" />
<namespace name="Design.DataAccess" />
<assembly name="Design.DataAcces" />


<container>
  <register type="IGaugeModelbaseService" mapTo="GaugeModelbaseService">
    <interceptor type="InterfaceInterceptor" />
  </register>
  <register type="IContractMapper" mapTo="ContractMapper">
    <lifetime type="hierarchical" />
  </register>
  <register type="IGenericRepository'1[Design.DataModel.Entities.GaugeModel, Design.DataModel]" mapTo="GenericSqlRepository'1[Design.DataModel.Entities.GaugeModel, Design.DataModel]">       
    <lifetime type="hierarchical" />
  </register>
</container>

我不明白我做错了什么,但这不起作用: - XML编辑器在使用括号“[]”时出错 - 浏览svc文件会出现以下错误:

  

无法解析类型名称或别名IGenericRepository'1 [Design.DataModel.Entities.GaugeModel,Design.DataModel]。请检查配置文件并验证此类型名称。

我尝试了其他几个,最终导致脑死亡。请帮忙。

由于

Frans Verhoeven

1 个答案:

答案 0 :(得分:1)

对于每种类型you must specify the assembly and the full namespace,您都可以使用aliases

对于泛型类型,如果不使用别名,则必须使用双方括号:

<register 
      type="MyNameSpace.IGenericRepository'1[[Design.DataModel.Entities.GaugeModel, Design.DataModel]], MyAssembly"       
     mapTo="MyNameSpace.GenericSqlRepository'1[[Design.DataModel.Entities.GaugeModel, Design.DataModel]], MyAssembly">       
    <lifetime type="hierarchical" />
</register>