Autofa c generi c декоратор не работает в версии 5.1.2 - PullRequest
0 голосов
/ 27 марта 2020

У меня есть блок кода, как показано ниже

using System;
using System.Reflection;
using System.Threading;
using System.Threading.Tasks;
using Autofac;
using MediatR;

namespace DecoratorConsoleApp
{
    public class Program
    {
        private static void Main()
        {
            var mediator = Register();
            Task.Run(async () => { await mediator.Send(new CreateCustomerCommand()); }).Wait();
            Console.ReadLine();
        }

    public static IMediator Register()
    {
        var builder = new ContainerBuilder();

        builder.RegisterAssemblyTypes(typeof(IMediator).GetTypeInfo().Assembly).AsImplementedInterfaces();

        builder
            .RegisterAssemblyTypes(typeof(CreateCustomerCommandHandler).GetTypeInfo().Assembly)
            .AsClosedTypesOf(typeof(IRequestHandler<,>))
            .AsImplementedInterfaces();

        builder.Register<ServiceFactory>(ctx =>
        {
            var c = ctx.Resolve<IComponentContext>();
            return t => c.Resolve(t);
        });

        builder.RegisterGenericDecorator(typeof(DiagnosticDecorator<,>), typeof(ICommandHandler<,>));
        var container = builder.Build();

        return container.Resolve<IMediator>();

    }
}

public interface ICommand<out TResponse> : IRequest<TResponse> { }

public interface ICommandHandler<in TRequest, TResponse> : IRequestHandler<TRequest, TResponse> where TRequest : ICommand<TResponse> { }

public class CreateCustomerCommand : ICommand<Guid> { }

public class CreateCustomerCommandHandler : ICommandHandler<CreateCustomerCommand, Guid>
{
    public async Task<Guid> Handle(CreateCustomerCommand request, CancellationToken cancellationToken)
    {
        await Task.CompletedTask;
        Console.WriteLine("test");
        return new Guid();
    }
}

public class DiagnosticDecorator<TRequest, TResponse> : ICommandHandler<TRequest, TResponse> where TRequest : ICommand<TResponse>
{
    private readonly ICommandHandler<TRequest, TResponse> _decorated;

    public DiagnosticDecorator(ICommandHandler<TRequest, TResponse> decorated)
    {
        _decorated = decorated;
    }

    public async Task<TResponse> Handle(TRequest request, CancellationToken cancellationToken)
    {
        Console.WriteLine("before");
        var response = await _decorated.Handle(request, cancellationToken);
        Console.WriteLine("after");
        return response;
    }
}
}

MediatR версия 8.0.1 Я пытаюсь запустить его. DiagnosticDecorator не работает Autofac.5.1.2. Я понизил Autofac версию до 4.9.0. Оно работает. , В чем разница между версиями?

...