Autofac Scanning Assemblies for certain class type
16,027
Solution 1
I think you need to specify the base class of your Plugins on registration. The call AsImplementedInterfaces registers the type with its implemented interfaces and not by its base type. You should update your registration to register your plugins as PluginBase.
Here´s the code:
var assemblies = AppDomain.CurrentDomain.GetAssemblies();
var builder = new ContainerBuilder();
builder.RegisterAssemblyTypes(assemblies)
.Where(t => t.BaseType == typeof(PluginBase))
.As<PluginBase>();
var container = builder.Build();
var pluginClasses = container.Resolve<IEnumerable<PluginBase>>();
Solution 2
Maybe do is this way:
builder
.RegisterAssemblyTypes(AppDomain.CurrentDomain.GetAssemblies())
.Where(t => t.GetInterfaces()
.Any(i => i.IsAssignableFrom(typeof (IDependency))))
.AsImplementedInterfaces()
.InstancePerDependency();
In this code I use IDependency
as a marker interface. You may replace it with your PluginBase
class and remove Where
method.
The point is to use IsAssignableFrom
method.
Related videos on Youtube
Author by
Jon
Updated on June 04, 2022Comments
-
Jon almost 2 years
I've started using
Autofac
and want to scan some DLL's and getAutofac
to register some of the classes within them.The classes that I'm interested in all inherit from a PluginBase class but the below code doesn't seem to be registering them. Can anyone help?
var assemblies = AppDomain.CurrentDomain.GetAssemblies(); var builder = new ContainerBuilder(); builder.RegisterAssemblyTypes(assemblies) .Where(t => t.BaseType == typeof(PluginBase)) .AsImplementedInterfaces() .AsSelf(); var container = builder.Build(); var pluginClasses = container.Resolve<IEnumerable<PluginBase>>(); //pluginClasses is empty!!!!
-
Jon over 12 yearsWill that allow for classes that don't implement interfaces and those that do? I'm working with legacy code so old stuff wont have interfaces but new stuff will
-
Wojteq over 12 yearsIt should work with
Where(t => t.IsAssignableFrom(typeof(PluginBase))
. If not, I'll try to help you. -
Wojteq over 12 yearsOf course remove
.AsImplementedInterfaces()
. I didn't notice this line. -
Jon over 12 yearsThat doesn't work either. PluginBase is abstract if that helps
-
Jon over 12 yearsIf I put my original code back and just take out the where clause it works
-
Jon over 12 yearsThat works! Could I add the AsImplementedInterfaces for newer classes that do implement interfaces?
-
Wojteq over 12 yearsAre the classes that extends
PluginBase
in your current AppDomain? -
Jehof over 12 yearsI think these two calls should work together. Then the type gets registered with by its base type and all interfaces it implements.
-
Jon over 12 yearsOops, just re-read what I wrote and doesn't make sense. If I have a class that inherits from PluginBase and has a constructor argument of an interface this won't work even with AsImplementedInterfaces. Do I do a new builder registration or can I amend the current code?
-
Jon over 12 yearsYes they are. Jehof's seems to be getting somewhere but I still stuck
-
Jon over 12 yearsI just got it working by calling RegisterAssemblyTypes again. Thanks for your help.
-
Nicholas Blumhardt over 12 yearsJust a note, you don't need the
Where
call if you useAs
. -
Omer Faruk Zorlu over 5 yearsThat does not works when assembly are not loaded. AppDomain.CurrentDomain.GetAssemblies is only returns loaded assemblies.