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

断了今生、忘了曾经 提交于 2020-01-06 06:51:35

问题


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?)


回答1:


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.


来源:https://stackoverflow.com/questions/47454684/read-modules-from-web-config-in-autofac-to-change-container-configuration-accord

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!