2014-01-17 78 views
0

我有一個將MemoryCache作爲構造函數參數的實現。Autofac - 通過xml配置將實例傳遞給構造函數參數

我的XML配置:

<autofac defaultAssembly="Test.Caching"> 
    <component name="memoryCache" 
     type="System.Runtime.Caching.MemoryCache, System.Runtime.Caching"> 
     <parameters> 
      <parameter name="name" value="TestCache"/> 
     </parameters> 
    </component> 

    <component type="My.Caching.TaskCache, Test.Caching" service="Test.Caching.ITaskCache" > 
     <parameters> 
      <parameter name="cache" value="testCache" /> 
     </parameters> 
    </component> 
</autofac> 

我使用Autofac.Configuration,並通過註冊XML:

builder.RegisterModule(new ConfigurationSettingsReader("autofac", "<configPath>")); 

我獲得以下錯誤:

System.Configuration.ConfigurationErrorsException: The type 'System.Runtime.Caching.MemoryCache, System.Runtime.Caching' could not be found. It may re 
quire assembly qualification, e.g. "MyType, MyAssembly". 
    at Autofac.Configuration.ConfigurationRegistrar.LoadType(String typeName, Assembly defaultAssembly) 
    at Autofac.Configuration.ConfigurationRegistrar.RegisterConfiguredComponents(ContainerBuilder builder, SectionHandler configurationSection) 
    at Autofac.Configuration.ConfigurationRegistrar.RegisterConfigurationSection(ContainerBuilder builder, SectionHandler configurationSection) 
    at Autofac.Configuration.Core.ConfigurationModule.Load(ContainerBuilder builder) 
    at Autofac.Module.Configure(IComponentRegistry componentRegistry) 
    at Autofac.ContainerBuilder.Build(IComponentRegistry componentRegistry, Boolean excludeDefaultModules) 
    at Autofac.ContainerBuilder.Build(ContainerBuildOptions options) 

我引用 - 項目中的System.Runtime.Caching。

回答

1

我認爲你需要爲你聲明的接口:

<component name="memoryCache" 
type="System.Runtime.Caching.MemoryCache, System.Runtime.Caching" 
service="your.interface.IMemoryCache, your.interface" 
相關問題