I need to get, from an IComponentContext
, a list of registered Type
's that implement a particular interface.
I don't want actual instances of the types, but rather a list of Type
of which I could get instances.
I want to use this list to generate subscriptions on a message bus.
How do I get all registered implementations of an interface in Autofac?
From Visual Studio, you can get it via NuGet. The package name is Autofac. Alternatively, the NuGet package can be downloaded from the GitHub repository (https://github.com/autofac/Autofac/releases).
After you have your components registered with appropriate services exposed, you can resolve services from the built container and child lifetime scopes. You do this using the Resolve() method: var builder = new ContainerBuilder(); builder. RegisterType<MyComponent>(). As<IService>(); var container = builder.
RegistrationExtensions. AsImplementedInterfaces MethodSpecifies that a type from a scanned assembly is registered as providing all of its implemented interfaces.
I figured this out --
var types = scope.ComponentRegistry.Registrations
.SelectMany(r => r.Services.OfType<IServiceWithType>(), (r, s) => new { r, s })
.Where(rs => rs.s.ServiceType.Implements<T>())
.Select(rs => rs.r.Activator.LimitType);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With