Autofac: Register injections in config files and use Assembly without referencing in the project

434 views Asked by At

I'm using Autofac to setup injections.

My intention is to make the modules pluggable.

It means using config file (xml, json) instead of using code (which I've already awared of).

However, is there possibility for Autofac to lookup an assembly without referencing it in the main (web) project file? For example, I'd like to add a new module into a deployed package without changing other dll files, only copying the new module's dll files (in the same folder with dll file of web project) and changing the config file.

1

There are 1 answers

0
Alexey Zimarev On

Yes, you can either just scan assemblies you want to find types or modules (modules are better since you give control on which types to register to the assembly itself):

var builder = new ContainerBuilder();
var path = "path";

var assemblyFiles = Directory.GetFiles(path, "Plugin.*.dll");
builder.RegisterAssemblyModules(
    assemblyFiles.Select(
        file => Assembly.LoadFile(Path.Combine(path, file))).ToArray());

If you prefer configuration files, you can use app.config/web.config as described in the documentation. You can find documentation on using Autofac 4 with new ASP.NET 5 JSON based communication, if you need it.