我正在使用 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]。请检查您的配置文件并验证此类型名称。
我尝试了其他几个,最终导致脑死亡。请帮忙。
谢谢
弗兰斯·范霍文