【发布时间】:2019-10-02 19:57:38
【问题描述】:
我有以下接口:
public interface ICommandHandler<T>
{
void Handle(T command);
}
public class TransactionalCommandHandlerDecorator<T> : ICommandHandler<T>
{
private readonly ICommandHandler<T> _handler;
public TransactionalCommandHandlerDecorator(ICommandHandler<T> handler)
{
_handler = handler;
}
public void Handle(T command)
{
}
}
我有一个实现两个命令处理程序的具体类:
public class Handler : ICommandHandler<CreateLocation>
,ICommandHandler<ModifyLocation>
{
public void Handle(CreateLocation command)
{
}
public void Handle(ModifyLocation command)
{
}
}
我的注册如下:
builder.RegisterAssemblyTypes(Assembly.GetExecutingAssembly())
.AsClosedTypesOf(typeof(ICommandHandler<>))
.InstancePerLifetimeScope();
builder.RegisterGenericDecorator(typeof(TransactionalCommandHandlerDecorator<>), typeof(ICommandHandler<>));
解析“处理程序”类会导致 autofac 在无限循环中循环解析装饰器和处理程序,这会导致 StackOverflowException。如果我将“处理程序”更改为仅实现一个接口,那么它将毫无问题地工作。
知道如何解决这个问题吗?
【问题讨论】:
-
对我来说似乎是一个错误。我想建议在 Autofac 问题页面上报告这个,但显然,你 already done that :)
-
您是否尝试从程序集扫描中排除装饰器,以便它不会尝试自行装饰?
-
是的。我使用以下代码从程序集扫描中排除了“TransactionalCommandHandlerDecorator”:.Where(a=> !typeof(TransactionalCommandHandlerDecorator).IsAssignableFrom(a)) 但这没有帮助。
标签: c# dependency-injection decorator autofac