2012-09-30 18 views
0

我想實現一個通用類型庫接口的構造函數注入。 我通過應用http://msdn.microsoft.com/en-us/library/ff660933(v=pandp.20).aspx文檔來完成此操作。在XML編輯器但是不接受,例如方括號:Xml配置IRepository <T>。 MSDN是否錯誤?

<alias alias="IGenericRepository" 
     type="Design.DataModel.Interfaces.IGenericRepository'1 [[Design.DataModel.Entities.GaugeModel, Design.DataModel]], Design.DataModel"/>  

的XML編輯器提供了以下錯誤:「意外的文本」。 完整的XML節是:

<unity xmlns="http://schemas.microsoft.com/practices/2010/unity"> 
<sectionExtension prefix="" type="Microsoft.Practices.Unity.InterceptionExtension.Configuration.InterceptionConfigurationExtension, Microsoft.Practices.Unity.Interception.Configuration" /> 

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

瀏覽SVC結果卻導致一個錯誤:「類型名稱或別名GaugeModelbaseService無法解析」

XML必須等同到以下流利的API配置(工作正常):

public class WcfServiceFactory : UnityServiceHostFactory 
{ 
    #region Methods 

    protected override void ConfigureContainer(IUnityContainer container) 
    { 
     //container.LoadConfiguration(); 
     container.AddNewExtension<EnterpriseLibraryCoreExtension>(); 
     container.AddNewExtension<Interception>(); 
     container.RegisterType<IGaugeModelbaseService, GaugeModelbaseService>(
      new InterceptionBehavior<PolicyInjectionBehavior>(), new Interceptor<TransparentProxyInterceptor>()); 
     container.RegisterType<IContractMapper, ContractMapper>(new HierarchicalLifetimeManager()); 
     container.RegisterType<IGenericRepository<GaugeModel>, GenericSqlRepository<GaugeModel>>(
      new HierarchicalLifetimeManager()); 
    } 

    #endregion 
} 

我錯過了xml配置中的東西嗎?請幫忙。

+0

[另一個問題]的副本(http://stackoverflow.com/questions/3486837/unity-2-0-registering-generic-types-via-xml)。 –

+0

部分是的。但這個問題沒有給我答覆。爲什麼xml編輯器不接受方括號?爲什麼它不起作用?我想念什麼? –

+0

仔細閱讀Chris Tavares的回答。他首先爲開放泛型類型創建一個別名,並使用該別名爲封閉泛型類型定義別名。並忽略xml編輯器的警告。這是一個錯誤的否定。 –

回答

0

這是從您的配置文件直接複製/粘貼?如果是這樣,這個問題是一個錯字:

<alias alias="GaugeModelbaseService" type="Design.ContractImplememtations.GaugeModelbaseService, Design.ContractImplementations"/> 

注: 'Implememtations'

你的語法是正確的,否則。