我正在使用Unity構造函數注入。通過運行時API構造方法注入成功用下面的代碼:統一xml配置通用接口
{
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文件給出以下e RROR:
The type name or alias IGenericRepository'1[Design.DataModel.Entities.GaugeModel, Design.DataModel] could not be resolved. Please check your configuration file and verify this type name.
我試了其他並最終導致腦死亡。請幫忙。
感謝
弗蘭斯·範霍文
你確定你想這樣做的XML和使用Unity? – Steven
是的,爲什麼我不應該? –
XML非常詳細,容易出錯,並且很難保存。你可以使用'RegisterType'來映射一個開放的泛型類型,但是沒有簡單的方法在Unity中進行泛型類型的批量註冊。 – Steven