如何在使用 RegisterAssemblyType 时向 Autofac 注册装饰器

本文关键字:注册 Autofac 时向 RegisterAssemblyType | 更新日期: 2023-09-27 18:31:14

这是我的代码

public interface ICommandHandler<T>
{
    void Handle(T command);
}
public class CreateUserCommandHandler : ICommandHandler<CreateUserCommand>
{
    public void Handle(CreateUserCommand command)
    {
        // do something with the command
    }
}
public class LoggingCommandDecorator<TCommand> : ICommandHandler<TCommand>
{
    private readonly ICommandHandler<TCommand> _commandHandler;
    public LoggingCommandDecorator(ICommandHandler<TCommand> commandHandler)
    {
        _commandHandler = commandHandler;
    }
    public void Handle(TCommand command)
    {
        Debug.WriteLine("Logging...");
        _commandHandler.Handle(command);
    }
}

这是我的注册:

private void SetupAutofac()
{
    var builder = new ContainerBuilder();
    // Register your MVC controllers.
    builder.RegisterControllers(typeof(WebApiApplication).Assembly);
    // OPTIONAL: Register model binders that require DI.
    builder.RegisterModelBinders(Assembly.GetExecutingAssembly());
    builder.RegisterModelBinderProvider();
    var assemblies = AppDomain.CurrentDomain.GetAssemblies();
    builder.RegisterAssemblyTypes(assemblies)
            .As(o => o.GetInterfaces()
            .Where(i => i.IsClosedTypeOf(typeof(ICommandHandler<>)))
            .Select(i => new KeyedService("Handler", i)));
    builder.RegisterGenericDecorator(typeof(LoggingCommandDecorator<>),
                            typeof(ICommandHandler<>),
                            "Handler", "DecoratedHandler");

    var container = builder.Build();
    DependencyResolver.SetResolver(new AutofacDependencyResolver(container));
}

当我运行此代码时,出现以下异常:

没有找到任何构造函数 'Autofac.Core.Activators.Reflection.DefaultConstructorFinder' on type "AspectDemo.Controllers.HomeController"可以使用 可用服务和参数:无法解析参数 'AspectDemo.Business.ICommandHandler 1[AspectDemo.Business.Users.CreateUserCommand] createUserHandler' of constructor 'Void .ctor(AspectDemo.Business.ICommandHandler 1[AspectDemo.Business.Users.CreateUserCommand])'.

当我使用以下作为我的注册时,我没有得到例外,但我也没有任何装饰器。

builder.RegisterAssemblyTypes(assemblies)
       .AsClosedTypesOf(typeof(ICommandHandler<>))
       .AsImplementedInterfaces();

我需要做什么才能使用装饰器?

注意:现在我只使用一个装饰器,但最终我认为我有大约 4-5 个装饰器。

如何在使用 RegisterAssemblyType 时向 Autofac 注册装饰器

使用 RegisterGenericDecorator 方法的 toKey 参数时,它会导致命名注册,因此必须解析命名ICommandHandler

builder.RegisterGenericDecorator(
    typeof(LoggingCommandDecorator<>), 
    typeof(ICommandHandler<>), 
    fromKey : "Handler",
    toKey : "DecoratedHandler");

然后你可以这样解决它:

container.ResolveKeyed<ICommandHandler<CreateUserCommand>>("DecoratedHandler");

toKey参数是可选的:

builder.RegisterGenericDecorator(
    typeof(LoggingCommandDecorator<>), 
    typeof(ICommandHandler<>), 
    fromKey : "Handler");

然后你可以这样解决它:

container.Resolve<ICommandHandler<CreateUserCommand>>();

当您拥有中间装饰器时,toKey很有用:

builder.RegisterGenericDecorator(
    typeof(LoggingCommandDecorator<>), 
    typeof(ICommandHandler<>), 
    fromKey : "Original", 
    toKey : "Logging");
builder.RegisterGenericDecorator(
    typeof(AuthorizationCommandDecorator<>), 
    typeof(ICommandHandler<>), 
    fromKey : "Logging");

在这种情况下,ICommandHandler<CreateUserCommand>将由LoggingCommandDecorator<>AuthorizationCommandDecorator<>装饰