Register all autofac modules from assembly by one

2019-04-28 08:35发布

问题:

I can register all Autofac modules (classes derived from Autofac.Module) one by one using line like this

builder.RegisterModule(new LoggingInjectionModule());

But if I have 10 or more modules I want just to specify assembly, where Autofac can find all modules, that it need to register. Is there is any way to register all modules from specific assembly, namespace by single line or two lines?

回答1:

You can use the different overloads (see API doc) of the RegisterAssemblyModules method:

builder.RegisterAssemblyModules(Assembly.GetExecutingAssembly());