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