gpt4 book ai didi

c# - DependencyResolutionException 检测到循环组件依赖性 : How to use Autofac to inject decorator?

转载 作者:太空宇宙 更新时间:2023-11-03 23:34:52 25 4
gpt4 key购买 nike

我有一个名为 ILastActivityUpdator 的接口(interface),如下所示:

public interface ILastActivityUpdator
{
void UpdateLastActivity(int userId);
}

除此之外,我还有两个实现此接口(interface)的类,分别是 LastActivityUpdator 和装饰器 AnonymousUserLastActivityUpdator。后者将 ICookieProvider 作为额外参数,并在方法实现中使用它。即,

public LastActivityUpdator(IUnitOfWork unitOfWork) 
: base(unitOfWork)
{ }

public void UpdateLastActivity(int userId)
{
// Do some stuff.
}

装饰器:

public AnonymousUserLastActivityUpdator(
IUnitOfWork unitOfWork,
ICookieProvider cookieProvider,
ILastActivityUpdator lastActivityUpdator)
: base(unitOfWork)
{
this.lastActivityUpdator = lastActivityUpdator;
this.cookieProvider = cookieProvider;
}

public void UpdateLastActivity(int userId)
{
lastActivityUpdator.UpdateLastActivity(userId);

cookieProvider.SetCookie();
}

我想在 AnonymousUserService 中注入(inject) AnonymousUserLastActivityUpdator,在 UserService 中注入(inject) LastActivityUpdator

为此,我在 IoCConfig 中的 RegisterDependencies 方法包含以下从 Autofac FAQ Decorator section 中获取的注册信息.

builder.RegisterType() .Keyed(AuthenticatedStatus.Anonymous) .InstancePerRequest(); builder .RegisterType() .Keyed(AuthenticatedStatus.Authenticated) .InstancePerRequest();

builder.RegisterType<LastActivityUpdator>()
.Named<ILastActivityUpdator>("lastActivityUpdator");
builder.RegisterType<AnonymousUserLastActivityUpdator>()
.Named<ILastActivityUpdator>("lastActivityUpdator");

// Then register the decorator. The decorator uses the
// named registrations to get the items to wrap.
builder.RegisterDecorator<ILastActivityUpdator>(
(c, p, inner) => new AnonymousUserLastActivityUpdator(
c.Resolve<IOogstplannerUnitOfWork>(),
c.Resolve<ICookieProvider>(),
inner),
fromKey: "lastActivityUpdator");

不幸的是,当我运行代码时出现错误:

Autofac.Core.DependencyResolutionException Circular component dependency detected:
Services.AnonymousUserService ->
Services.AnonymousUserService ->
Services.AnonymousUserLastActivityUpdator ->
Services.AnonymousUserLastActivityUpdator ->
Services.AnonymousUserLastActivityUpdator.

此外,我不明白为什么会神奇地注入(inject)正确的实现。有谁知道如何解决这个问题?

最佳答案

出现此错误是因为 C# 编译器不知道要采用 RegisterDecorator 方法的哪个重载。

如果你强制它:

builder.RegisterDecorator<ILastActivityUpdator>(
new Func<IComponentContext, ILastActivityUpdator, ILastActivityUpdator>(
(c, inner) => new AnonymousUserLastActivityUpdator(inner)
),
fromKey: "lastActivityUpdator");

你将有一个新的错误:

AnonymousUserLastActivityUpdator does not contain a constructor that takes 1 arguments

这个错误是由这一行引起的:

new AnonymousUserLastActivityUpdator(inner)

要解决此错误,您必须更改创建 AnonymousUserLastActivityUpdator 实例的方式

例如:

// only the type being decorated should be registered here
builder.RegisterType<LastActivityUpdator>()
.Named<ILastActivityUpdator>("lastActivityUpdator");

builder.RegisterDecorator<ILastActivityUpdator>(
(c, inner) => new AnonymousUserLastActivityUpdator(c.Resolve<IUnitOfWork>(),
c.Resolve<ICookieProvider>(),
inner),
fromKey: "lastActivityUpdator")
.As<ILastActivityUpdator>();

甚至更好:

builder.RegisterType<LastActivityUpdator>()
.Named<ILastActivityUpdator>("lastActivityUpdator");
builder.RegisterType<AnonymousUserLastActivityUpdator>()
.Named<ILastActivityUpdator>("anonymousLastActivityUpdator");

builder.RegisterDecorator<ILastActivityUpdator>(
(c, inner) => c.ResolveNamed<ILastActivyUpdator>("anonymousLastActivityUpdator",
TypedParameter.From(inner)),
fromKey: "lastActivityUpdator")
.As<ILastActivityUpdator>();

关于c# - DependencyResolutionException 检测到循环组件依赖性 : How to use Autofac to inject decorator?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30902440/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com