Read modules from web.config in autofac to change container configuration according to solution configurations
Asked Answered
T

1

0

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

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

Now I have to build my container. But the autofac documentation is not clear to me. I do not understand what I have to do to read my modules and build the 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 latest version of Autofac so I do not have the ConfigurationSettingsReader class available.

Can anyone help me please?

EDIT

I had found interesting saving configuration on web.config because in this way I could "change" web.config according to my solution configuration (you know, the classic web.debug.config, web.release.config, etc)...

That could me help to register the correct modules avoiding the use of directives (#if bla bla bla, ...) or simply conditions...

I am already using modules, but I do not think the correct solution is adding a property inside the module to choose the component to resolve according 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 refers to ConfigurationSettingsReader. Is it ok?)

Triplex answered 23/11, 2017 at 11:41 Comment(0)
K
2

In the 4.0 version of configuration you don't store anything in web.config. It's all in separate XML or JSON files. I'd recommend JSON. The documentation outlines that pretty well:

If you were using the app.config or web.config based configuration available before, you will need to migrate your configuration to the new format and update the way you set configuration with your application container.

We actually spent a lot of time trying to document as much as possible, so while there's definitely a lot there try not to "TL;DR" it. If you skip around, you're liable to end up in the "pre 4.0" section thinking that will still work with the 4.0 stuff. It won't. It sounds like from your comment on this other question that you may have missed a few things the first time through.

Spend some time in the quick start section. That section has both C# and JSON code showing how things work. Again, it's easy to skip past that.

If the docs don't show enough examples, look at the unit tests in the Autofac.Configuration repo, especially the folder full of test files that shows both XML and JSON formatted examples we use in testing.

Finally... three tips:

  1. Configuration is not a feature-for-feature replacement for code. If you're looking to do amazing, crazy, logic-based stuff then stick to modules, possibly with some configuration to register the modules.
  2. Be familiar with Autofac and DI terminology. If you're new to DI or Autofac, "components," "services," and other terms will be confusing. The configuration uses these terms, which means you may not get what you're looking at. Spend time with the docs. The getting started page includes an intro to some of the terminology.
  3. Learn about the new Microsoft config system. There is separate doc about that maintained by Microsoft. Their docs explain everything from how to change config based on environment to creating custom config providers. Autofac is standing on the shoulders of config giants - we don't have to build in that flexibility anymore because it comes for free from the new config system.
Kurtiskurtosis answered 24/11, 2017 at 3:32 Comment(3)
Thank you for your answer.. I edited the post because my answer is too long... I assure you I am not doing "TL;DR". In my last week I have stopped working and I have started reading and trying... I do not want to do amazing, crazy, logic-based stuff, I just want to do the most correct thing I can do... I do not want a code that "just works"... I already have it, but I am not satisfied in what I have done...Triplex
Sounds like you weren't asking the question you meant to ask - "How do I change config based on environment?" I added a third tip - read up on the Microsoft config system. We built on top of that because it has all the extensibility points and handling built in. Their doc shows how to work with multiple environments.Kurtiskurtosis
yes... you are right... I understood that my question was wrong just after you answered. Your answer let me understood. Thank you very much.. now everything come back clear again!Triplex

© 2022 - 2024 — McMap. All rights reserved.