Read the modules from web.config in autofac to change the container configuration according to the solution configurations

I created several Autofac modules ... Now I want to register some of them in my container using web.config ... In my web.config I wrote:

<autofac defaultAssembly="Autofac.Example"> <modules> <module type="DebugModuleTest1"></module> <module type="DebugModuleTest2"></module> </modules> </autofac> 

Now I need to create a container. But the autofile documentation is not clear to me. I do not understand what I need to do to read my modules and create a container.

 public class MyCustomContainer { public void Build(HttpConfiguration config) { var builder = new ContainerBuilder(); Microsoft.Extensions.Configuration.ConfigurationBuilder x = new Microsoft.Extensions.Configuration.ConfigurationBuilder(); //var sec = x.AddInMemoryCollection().Build().GetSection("autofac"); // var y = x.AddXmlFile("Web.config"); var y = new ConfigurationBuilder().SetBasePath(AppDomain.CurrentDomain.BaseDirectory); var z = y.AddXmlFile("Web.Config"); config.DependencyResolver = new AutofacWebApiDependencyResolver(container); } } 

I am using the latest version of Autofac, so I do not have the ConfigurationSettingsReader class.

Can anybody help me?

EDIT

I found an interesting save configuration on web.config, because this way I could “modify” web.config to fit my solution configuration (you know, classic web.debug.config, web.release.config, etc. ) ....

This could help register the correct modules, avoiding the use of directives (#if bla bla bla, ...) or just conditions ...

I already use modules, but I don’t think that the right solution adds a property inside the module to select the component for resolution according to the selected environment where I want to deploy the project. I just think of this solution reading this example (By the way, Flexibility to Override still applies to ConfigurationSettingsReader . Is this normal?)

0
autofac
source share
1 answer

In version 4.0 version, you do not store anything in web.config. All this in separate XML or JSON files. I would recommend JSON. The documentation describes this pretty well:

If you used the previously installed configuration app.config or web.config, you need to transfer the configuration to a new format and update the configuration method using the application container.

In fact, we spent a lot of time trying to document as much as possible, so although there is definitely a lot, try not to "TL; DR" it. If you skip, you may end up in the "pre 4.0" section, thinking that it will still work with 4.0 files. This is not true. It seems like your comment on this other question is that you may have missed a few things for the first time.

Spend some time in the quick start section. This section has C # and JSON code showing how everything works. Again, it's easy to skip this.

If there aren’t enough examples in the documents, check out the unit tests in the Autofac.Configuration repo , especially the folder full of test files that shows both the XML and the JSON-formatted examples that we use for testing.

Finally, three tips:

  • Configuration is not a function to replace functions for code. If you want to do awesome, crazy, logic-based things, then stick with modules , perhaps with some configuration for registering modules.
  • Get familiar with Autofac and DI terminology. If you are new to DI or Autofac, “components”, “services” and other terms will be confusing. The configuration uses these conditions, which means that you cannot get what you are looking for. Spend time with documents. At the top of the page is an introduction to some of the terms.
  • Learn about the new Microsoft configuration system. There is a separate document about this supported by Microsoft. Everything in their docs explains how to change the configuration based on the environment to create custom configuration providers. Autofac is on the shoulders of configuration giants - we no longer need to increase this flexibility because it comes free with the new configuration system.
+1
source share

All Articles