ASP. NET Core read configuration from GITLAB-CI environment variable

Source: Internet
Author: User

With the recent intensification of continuous integration, a scenario needs to pass the configuration to an ASP. NET Core application through the GITLAB-CI environment variable (Variables, CI/CD, Settings, Settings).

The configuration in Appsettings.json is like this

{"  ossclient": {    "Accesskeyid": "",    "Accesskeysecret": ""  }}

The question before you is how to pass the above configuration through an environment variable, without modifying the code that reads the configuration?

Services. Configure<osscredential> (configuration. GetSection ("ossclient"));

The ASP. NET Core enabled the read configuration from environment variable by default, and Config is included in Webhost.createdefaultbuilder (). Addenvironmentvariables (), as long as the configuration is passed in the correct format, it can be done.

The question remains one--what format provides ossclient Accesskeyid and Accesskeysecret configurations in environment variables?

Found a blog post on the Internet Read Connections Strings & Config Values from Environment Variables

Export connectionstrings__default= "server=database-server; Database=sample; Trusted_connection=true; "

It turns out that only 2 underscores are required to separate sectionname and key.

View Microsoft.Extensions.Configuration.EnvironmentVariables in Environmentvariablesconfigurationprovider implementation of the source code is also confirmed this point.

private static string Normalizekey (String key) {    return key. Replace ("__", Configurationpath.keydelimiter);}

Validation in the actual project using the following environment variable configuration is indeed valid.

Export Ossclient__accesskeyid=xxxexport ossclient__accesskeysecret=yyy

Success is also verified in Gitlab-ci.

Real-world experience with the powerful and flexible configuration capabilities of ASP.

ASP. NET Core read configuration from GITLAB-CI environment variable

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.