Posts

Showing posts from April, 2019

Resolve IEnumerable of open generic types with Autofac

This time let's talk about Autofac, probably one of the most popular Dependency Injection container. In the past few months, I have been working on a project in which Autofac is used to provide service instances. In some of the singleton services, I needed to access some request scoped services, so I built a fun type called IScoped<T> to be able to use per HTTP request lifetime to resolve service types. And it works. However, today, when I was building a service in which I needed a list of IScoped<T> , I ran into a problem. One of the cool features that Autofac provides is implicit support for resolving IEnumerable<T> . Say if we register two implementation types as the same interface type, we could resolve an instance of IEnumerable of the interface type to retrieve all instances of both implementations types in one go. That's very cool, isn't it? However, it doesn't work as what I expect when resolving a list of IScoped<T> , where T has mu