How to register Generic Interfaces with a Interface Type in AutoFac?

Question

I'm trying to register a Generic Interface with multiple Types using Autofac and I like to call:

container.Resolve<Enumerable<IGenericInterface<IInterface>>>(); 

To resolve all the Implementations of IGenericInterface with a type of IInterface.

I already tried to register the concrete classes that have the generic interface with a concrete class as generic interface with the Interface:

builder.Register(r => new TalkToMeLoud())
    .As<IGenericInterface<Hello>>()
    .As<IGenericInterface<Bye>>()
    .As<IGenericInterface<IInterface>>()
    .InstancePerDependency();

But this will throw an error:

An unhandled exception of type 'System.ArgumentException' occurred in Autofac.dll

Additional information: The type 'TestConsoleApp.TalkToMeLoud' is not assignable to service 'TestConsoleApp.IGenericInterface`1[[TestConsoleApp.IInterface, TestConsoleApp, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]]'.

Because this is part of a migration from Unity to Autofac I know this can be done by Unity like this:

Container.ResolveAll(typeof(IGenericInterface<IInterface>))

Does anyone know how to solve this problem?

PS: This is my testproject


Show source
| generics   | c#   | interface   | .net   | autofac   2017-01-06 10:01 1 Answers

Answers to How to register Generic Interfaces with a Interface Type in AutoFac? ( 1 )

  1. 2017-01-06 13:01

    The message is quite clear:

    The type 'TalkToMeLoud' is not assignable to service 'IGenericInterface'.

    In other words, you will either have to:

    • Implement IGenericInterface<IInterface> on TalkToMeLoud or
    • Make IGenericInterface<T> covariant by setting T as out argument. i.e. IGenericInterface<out T>.

    The CLR will not be able to cast TalkToMeLoud to IGenericInterface<IInterface> even though it might implement IGenericInterface<Hello> where Hello implements IInterface. If your not familiar with the concept of variance, you might want do a little google search and read answers like this one.

    So in your case you probably need to make your interface covarant; this allows you to cast from IGenericInterface<Hello> to IGenericInterface<IInterface>. This only works however when T is only used as output argument in the interface.

Leave a reply to - How to register Generic Interfaces with a Interface Type in AutoFac?

◀ Go back