Dotnetcore how cross-platform ~ configuration file and Configuration code coexist

Source: Internet
Author: User
Tags dotnet

Back to Catalog

The Ancients Cloud "one mountain does not allow two Tigers", and after the dotnet core era, we can see some of the official demo, it will be the data connection string and other configuration items are directly hardcoded in the code, that is, in the startup definition, ask you in the production environment how compatible! Of course, you would say, can be configured in the corresponding appsettings, said it is the corresponding appsettings, because the dotnet core configuration file has an environmental distinction, generally use the following name to represent different environments:

    1. Development environment, development
    2. Pre-release environment, Staging
    3. Production environment, Production

For both, how to choose the configuration file and hard-coded configuration, and if both are set, then who should be? The uncle thinks, if both are set, that is the configuration file, when the configuration file is not defined, and then the hard-coded configuration, which is their priority, because of the following points:

    1. Hard coding facilitates debugging in the development environment
    2. After you specify a running environment, the configuration file chooses a different configuration depending on the environment
    3. Optimization level, configuration file is better than hard coding

The following is the warehousing service at the time of registration, select the configuration of the policy, of course, you can make this logic a decoration, feel better.

 Public classefoptionsextension:ilindoptionsextension {Private ReadOnlyAction<repositoryoptions>_configure;  PublicEfoptionsextension (action<repositoryoptions>Configure) {_configure=Configure; }         Public voidaddservices (iservicecollection services) {varOptions =Newefoptions (); _configure?. Invoke (options);//Decoration            if(oconfigfilehelper.get<efoptions>(). ConnString!=NULL)//configuration file first hard-coded{options. ConnString= configfilehelper.get<efoptions>().            connstring; }            if(configfilehelper.get<efoptions>(). DbType!=dbtype.none) {options. DbType= configfilehelper.get<efoptions>().            DbType; } services. Addsingleton<ilogger, filelogger> ();//LogServices. Addsingleton (options);//EF ConfigurationServices. AddTransient (typeof(DbContext), options. Dbcontexttype);//Registering data contexts, instance patternsServices. AddTransient (typeof(irepository<>),typeof(efrepository<>));//Register Data Warehousing        }    }

After we have published, the general release of Dotnet core to Linux or directly in the Docker container to run, just set the corresponding environment variables can be very convenient!

ENV aspnetcore_environment="Production"

Once setup is complete, dotnet Core chooses the corresponding appsettings. Production.json file to load!

Thank you for reading!

Back to Catalog

How Dotnetcore Cross-platform ~ configuration files and configuration code coexist

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.