Ninetiger blog

-- my reminder

Read appsettings.json (*.json) from .net Core library project

Currently there is not existing lib to read a .json file from a .net core library project. To write our own lib:

1. Import 2 nuGet libs:

  • Microsoft.Extensions.Configuration
  • Microsoft.Extensions.Configuration.Json

2. Create a class:

    public static class AppConfiguration
    {
        public static readonly IConfigurationSection AppSettings;
        static AppConfiguration()
        {
            var configurationBuilder = new ConfigurationBuilder();
            var path = Path.Combine(Directory.GetCurrentDirectory(), "appsettings.json");
            configurationBuilder.AddJsonFile(path, true);

            var configurationRoot = configurationBuilder.Build();
            AppSettings = configurationRoot.GetSection("appSettings");
        }
    }

3. To use it, call 

AppConfiguration.AppSettings["function_host"] 

4. Json example:

{
  "bindingCulture": {
    "language": "en-us"
  },
  "language": {
    "feature": "en-us"
  },
  "appSettings": {
    "function_host": "http://localhost:7071",
    "Environment": "abc"
  }
}

Comments:
by dobsonz, 2020年10月18日 1:38:15 PM jVofZy http://pills2sale.com/ levitra nizagara
by dobsonz, 2020年10月18日 1:38:13 PM jVofZy http://pills2sale.com/ levitra nizagara

Back to top