DotNetCore cross-platform ~ How the configuration file and configuration code coexist, dotnetcore

Source: Internet
Author: User

DotNetCore cross-platform ~ How the configuration file and configuration code coexist, dotnetcore

Back to directory

We can see some official demos like this after the ancient cloud "one mountain cannot be two tigers" and the dotnet core era, it hard-coded the data connection string and other configuration items in the code, that is, the definition in startup. How can you ensure compatibility in the production environment! Of course, you will say that you can configure it in the corresponding appsettings, and that it is the corresponding appsettings, because the configuration files under dotnet core have different environments, generally, the following names are used to indicate different environments:

For the two, how should I select the configuration file and hard encoding configuration? if both are set, who should be the correct one? Uncle believes that if both are set, the configuration file prevails. When the configuration file is not defined, the hardware encoding configuration prevails. This is their priority, for the following reasons:

The following is the Configuration Policy selected when the warehousing service is registered. Of course, you can make this logic a decoration and feel better.

Public class EFOptionsExtension: Detail {private readonly Action <RepositoryOptions> _ configure; public EFOptionsExtension (Action <RepositoryOptions> configure) {_ configure = configure;} public void AddServices (IServiceCollection services) {var options = new EFOptions (); _ configure ?. Invoke (options); // decoration if (oConfigFileHelper. Get <EFOptions> (). ConnString! = Null) // the configuration file is hardcoded {options. ConnString = ConfigFileHelper. Get <EFOptions> (). ConnString;} if (ConfigFileHelper. Get <EFOptions> (). DbType! = DbType. none) {options. dbType = ConfigFileHelper. get <EFOptions> (). dbType;} services. addSingleton <ILogger, FileLogger> (); // log services. addSingleton (options); // ef Configuration services. addTransient (typeof (DbContext), options. dbContextType); // register the data context, instance mode services. addTransient (typeof (IRepository <>), typeof (EFRepository <>); // register data warehouse }}

After the release, we usually release the dotnet core to linux or directly run it in a docker container. In this case, you only need to set the corresponding environment variable, which is very convenient!

ENV ASPNETCORE_ENVIRONMENT="Production"

After the configuration is complete, dotnet core selects the corresponding etettings. Production. json file for loading!

Thank you for reading this article!

Back to directory

 

Related Article

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.