Autofac-通过xml配置将实例传递给构造函数参数
我有一个将MemoryCache作为构造函数参数的实现 我的xml配置:Autofac-通过xml配置将实例传递给构造函数参数,xml,dependency-injection,ioc-container,autofac,Xml,Dependency Injection,Ioc Container,Autofac,我有一个将MemoryCache作为构造函数参数的实现 我的xml配置: <autofac defaultAssembly="Test.Caching"> <component name="memoryCache" type="System.Runtime.Caching.MemoryCache, System.Runtime.Caching"> <parameters> <paramete
<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>
我在项目中引用了-System.Runtime.Caching。我认为您需要一个接口来进行声明:
<component name="memoryCache"
type="System.Runtime.Caching.MemoryCache, System.Runtime.Caching"
service="your.interface.IMemoryCache, your.interface"
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)
<component name="memoryCache"
type="System.Runtime.Caching.MemoryCache, System.Runtime.Caching"
service="your.interface.IMemoryCache, your.interface"