【问题标题】:unity xml configuration generic interfacesunity xml 配置通用接口
【发布时间】:2012-09-23 18:58:23
【问题描述】:

我正在使用 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]。请检查您的配置文件并验证此类型名称。

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

谢谢

弗兰斯·范霍文

【问题讨论】:

  • 您确定要在 XML 和 Unity 中执行此操作吗?
  • XML 非常冗长、容易出错且难以维护。您可以使用RegisterType 映射一个开放的泛型类型,但是在 Unity 中没有简单的方法来批量注册泛型类型。

标签: .net configuration unity-container


【解决方案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>

【讨论】:

  • 感谢您的回答。就我而言,它并不能解决问题。我在浏览 svc 文件时仍然遇到同样的错误。像以前一样,我的 xml 编辑器(Visual Studio)也在方括号“意外文本”上给出以下错误。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-06-26
  • 2020-08-31
  • 2017-06-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多