AUTOFAC v4.0+ registering components with a configuration file

Source: Internet
Author: User

Recently in the Li Yubao/openauth.net Project code, the novice said that do not understand. So, regardless of 3,721, imitation is the best learning, so I decided to create a project, the people's Code 1.1 copy over, savor.

In the course of the study, I discovered that the great gods used AUTOFAC to configure the file by the way. The autofac.configuration version is V3.3, and then I created the project with the V4.0.1. Originally wanted to register components with code, but to see the great God is registered through the configuration file, so, regardless of 3,721, I set a small goal, I want to use the v4.0.1 version of the configuration file to complete the way to register the component.

No more nonsense. Official website Documents in http://autofac.readthedocs.io/en/latest/configuration/xml.html#configuring-with-microsoft-configuration-4-0

There is a demo, such as

But when you copy the code to the project, you will find that the Addjsonfile method is an error, such as

Solution: How can I complete config. Addjsonfile this method.

In the degree of Niang after the discovery of basic all the pages are used addjsonfile this method

The reason is that Microsoft.Extensions.Configuration has 3 versions of this DLL.

Finally really helpless me, downloaded the source code of Microsoft.Extensions.Configuration components,: Https://codeload.github.com/aspnet/Configuration/zip/dev

After the study, it was found that the implementation classes of Iconfigurationsource (all abstract classes) have

Memoryconfigurationsource
Commandlineconfigurationsource
Environmentvariablesconfigurationsource
Fileconfigurationsource
Azurekeyvaultconfigurationsource

According to the Addjsonfile method above, I guess I need the fileconfigurationsource, and then I went to find the implementation class of the method class, found the approximate

Iniconfigurationsource
Jsonconfigurationsource
Xmlconfigurationsource

Similarly, according to the Addjsonfile method, I guess what I need is the Jsonconfigurationsource class.

With the above speculation, I can start.

In the VS Project reference right-click, find NuGet, search for Jsonconfigurationprovider, and then install

Then, again addjsonfile this sentence, I chose to use JSON, rather than XML to configure, the configuration file content was appended.

Finally, attach the main code (I am the ASP. NET MVC Project)

1. Create a containerbuilder.
var builder = new Containerbuilder ();
Iconfigurationbuilder config = new Configurationbuilder ();

Iconfigurationsource Autofacjsonconfigsource = new Jsonconfigurationsource ()
{
Path = "/config/autofac.json",
Optional = False,//boolean, default is False, can not write
Reloadonchange = false,//Ibid.
};

Config. ADD (Autofacjsonconfigsource);

Register the Configurationmodule with AUTOFAC.
var module = new Configurationmodule (config. Build ());
Builder. Registermodule (module);

Register your MVC controllers.
Builder. Registercontrollers (typeof (Mvcapplication). Assembly);


3.Build the container and store it for later use.
var container = Builder. Build ();

4. Implement Di (Dependencyresolver mode)
Dependencyresolver.setresolver (new Autofacdependencyresolver (container));

Finally, a description of the parameters of the configuration file, Link: https://github.com/autofac/Documentation/blob/master/docs/configuration/xml.rst

Attached: Contents of my Autofac.json file

AUTOFAC v4.0+ registering components with a configuration file

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.