Search code examples
c#dependency-injectionautofacdryioc

Convert Autofac Open Generic Interface Registration to DryIoc Registration


I want to register a mediator Im using from github, which provides a sample for registering it using Autofac.

I think my problem lies with this Autofac line:

builder.RegisterAssemblyTypes(assembly)
    .AsClosedTypesOf(typeof(IMessageHandler<,>))
    .AsImplementedInterfaces();

I'm not 100% sure what the line of code does,AsClosedTypesOf to be more specific.

I tried to translate it to DryIoc as:

container.RegisterMany(
    new Type[] { typeof(IMessageHandler<,>) },
    serviceTypeCondition: s => s.IsInterface, 
    setup: Setup.With(openResolutionScope: true));

But I get the following error:

DryIoc.ContainerException: 'Registering abstract implementation type SimpleMediator.Core.IMessageHandler<,> when it is should be concrete. Also there is not FactoryMethod to use instead.'

This is my registration code so far:

        container.RegisterMany(
            new Type[] { typeof(IMessageHandler<,>) },
            serviceTypeCondition: s => s.IsInterface, 
            setup: Setup.With(openResolutionScope: true));

        //var assemblies = AppDomain.CurrentDomain.GetAssemblies().Where(a => !a.IsDynamic);
        container.RegisterDelegate<ServiceFactoryDelegate>(c => t =>
        {
            return c.Resolve(t);
        });

        container.RegisterMany<ServiceFactory>();
        container.RegisterMany<Mediator>();
        container.RegisterMany(new Type[] { typeof(MessageProcessor<,>) });

        container.Register<IService1, Service1>(Reuse.Singleton);
        container.Register<IService2, Service2>(Reuse.Singleton);

        var service1 = container.Resolve<IService1>();
        var service2 = container.Resolve<IService2>();

        service1.GetDevice("asdf");

Without using RegisterMany for the IMessageHandler interface the program encounters the error:

System.ArgumentException: 'No handler of signature IMessageHandler`2 was found for DeviceEventMessage Arg_ParamName_Name'

Basically what I understand is that I need to register the message handlers I have defined, which inherit from IMessageHandler<,>

Mediator defines IMessageHandler as such:

public interface IEventHandler<in TEvent>
    : IMessageHandler<TEvent, Unit> where TEvent : IMessage<Unit>
{
}

And I implement it in the service:

public class Service2 : IService2, IEventHandler<DeviceEventMessage>
{
    public async Task<Unit> HandleAsync(
        DeviceEventMessage message,
        IMediationContext mediationContext,
        CancellationToken cancellationToken)
    {
        return Unit.Result;
    }
}

Where service1 actually calls the mediator in GetDevice("asdf") which requires the handler to be resolved.

How would I register classes implementing IMessageHandler<,> in DryIoc?


Solution

  • Update: fixed the working sample

    Added s.GetGenericDefinitionOrNull() in serviceTypeCondition

    Answer

    The exception is exactly about the problem here:

    container.RegisterMany(
        new Type[] { typeof(IMessageHandler<,>) },
        serviceTypeCondition: s => s.IsInterface,
        setup: Setup.With(openResolutionScope: true));
    

    This RegisterMany overload expects the first argument to be a collection of concrete Implementation types.

    For your case you probably need something like this instead:

    container.RegisterMany(
        new[] { typeof(Service1).GetAssembly() },
        serviceTypeCondition: s => s.GetGenericDefinitionOrNull() == typeof(IMessageHandler<,>));
    

    Btw, not sure what is the reason for openResolutionScope: true.