[英]How to register a decorator with Autofac: "Circular component dependency detected"
I have got a Circular dependency to extend the functionality of an object adding a cache abstraction.我有一个循环依赖来扩展添加缓存抽象的对象的功能。
public class CachedDecorator : IParameter
{
public CachedDecorator (IParameter decorated);
...
}
public class MyImplementationParameter : IParameter
{
...
}
Using .Net core Dependency injection container i can do something like this to create those dependencies and it works:使用 .Net core Dependency injection container 我可以做这样的事情来创建这些依赖项并且它可以工作:
services.AddSingleton<IParameter>(provider => new CachedDecorator
(provider.GetRequiredService<MyImplementationParameter >()));
How Can I replicate that declaration using Autofac?如何使用 Autofac 复制该声明?
I am trying something like this:我正在尝试这样的事情:
builder.RegisterType<CachedDecorator>()
.As<IParameter>()
.WithParameter(
new ResolvedParameter(
(pi, ctx) => pi.ParameterType == typeof(MyImplementationParameter ),
(pi, ctx) => ctx.Resolve<MyImplementationParameter>()));
But i get a "Circular component dependency detected: "但我收到“检测到循环组件依赖项:”
As of Autofac 4.9, you can register your decorator as follows:从 Autofac 4.9 开始,您可以按如下方式注册您的装饰器:
builder.RegisterType<MyImplementationParameter>().As<IParameter>();
builder.RegisterDecorator<CachedDecorator, IParameter>();
See the documentation for more information.有关更多信息,请参阅文档。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.