简体   繁体   English

如何使用 Autofac 注册装饰器:“检测到循环组件依赖项”

[英]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.

相关问题 DependencyResolutionException检测到循环组件依赖关系:如何使用Autofac注入装饰器? - DependencyResolutionException Circular component dependency detected: How to use Autofac to inject decorator? 循环组件依赖检测到具有 autofac 依赖注入的存储库模式 - Circular component dependency detected Repository pattern with autofac dependency injections 用于多个接口的装饰器 - Autofac中的循环依赖性谜语 - Decorator for multiple interfaces - a circular dependency riddle in Autofac 激活 Autofac .NET Core Web API 时检测到循环组件依赖项 - Circular component dependency detected while activating Autofac .NET Core Web API 如何解决Autofac循环依赖? - How to solve Autofac circular dependency? Autofac循环组件依赖关系-通用存储库 - Autofac Circular Component Dependency - Generic Repository 如何为CQRS的通用对象注册autofac装饰器 - How to register an autofac decorator for a generic object for CQRS 如何使用Autofac实现多依赖注入装饰器 - How to implement multi dependency injection decorator with autofac 具有枚举的Autofac循环依赖 - Autofac Circular Dependency With Enumeration 如何访问整个AutoFac容器以在Orchard中注册依赖项? - How to access overall AutoFac container to register a dependency in Orchard?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM