简体   繁体   中英

ASP.NET Core 2.0 - Configuration[“TestSecret”] always return null

I am starting to work on an ASP.NET Core 2.0 Web Api . I added 2 secrets to the secrets.json file and am trying to read them in through the Configuration property in my Startup file. Each time I called I try to get the value from the Configuration variable, it returns null. An example of how I am reading this from the secrets.json is shown below.

public void ConfigureServices(IServiceCollection services)
{
    var secret = Configuration["TestSecret"];

My secrets.json file looks like:

{
    "TestSecret": "SecretValue"
}

I have also tried to retrieve the value by using:

public void ConfigureServices(IServiceCollection services)
{
    IConfigurationSection secret = Configuration.GetSection("TestSecret");

    var value = secret.Value;

This returns a section that corresponds to the TestSecret section, but the value in the IConfigurationSection is also null.

I have tried to install the Microsoft.Extensions.Configuration.UserSecrets NuGet package, but this hasn't helped. Am I missing a package I need to install or is there an alternate way to retrieve this value?

If I need to provide any more information to help solve this issue, please ask in the comments. I will try to provide as much information as possible to help solve this issue.

In general you use a file called "appSettings.json" file for storing all json values like that. This file doesn't need to be manually added in 2.0. Unless there is a specific reason for having multiple json files I would recommend doing this as it allows you to have all your application specific settings in the one place

it is possible to manually add a .json file though. for asp.net Core 2.0 in your Program.cs in the BuildWebHost method

you add in the following

.ConfigureAppConfiguration((WebHostBuilderContext, ConfigurationBuilder) =>
        {
            ConfigurationBuilder
                    .AddJsonFile("Secrets.json", optional: true);
            ConfigurationBuilder.AddEnvironmentVariables();
        })

depending on the setup the entire method should look like

public static IWebHost BuildWebHost(string[] args) =>
    WebHost.CreateDefaultBuilder(args)
        .ConfigureAppConfiguration((WebHostBuilderContext, ConfigurationBuilder) =>
        {
            ConfigurationBuilder
                    .AddJsonFile("Secrets.json", optional: true);
            ConfigurationBuilder.AddEnvironmentVariables();
        })
        .UseKestrel()
            .UseContentRoot(Directory.GetCurrentDirectory())
            .UseIISIntegration()
            .UseStartup<Startup>()
            .UseApplicationInsights()
        .Build();

To get a value from in your ConfigureServices method you use

var testSecret = Configuration.GetSection("Secrets")["TestSecret"];

this is how the Secrets.Json file should look

{
  "Secrets": 
  {
    "TestSecret": "SecretValue"
  }
}

Did you configure to use secrets in the Startup method?

public Startup(IHostingEnvironment env)
{
    var builder = new ConfigurationBuilder();

    if (env.IsDevelopment())
    {
        builder.AddUserSecrets<Startup>();
    }

    Configuration = builder.Build();
}

Read more here

For me it was null because I was playing around with other ASPNETCORE_ENVIRONMENT values and the default logic is apparently dependent on this being "Development"... which makes basic sense given that secrets.json is entirely local dev scenario.

As a related aside, for us there's mild heartburn that "Development" could mean either cloud runtime dev or raw local dev so we're forced to create yet another key/value to represent local dev because ASPNETCORE_ENVIRONMENT = Development drives specific logic we don't want to lose.

在此处输入图片说明

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM