2014-01-17 77 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" 
相关问题