Friday, January 3, 2020

Get the secrets.json file working in a .NET Core 3 application.

In the ConfigureServices method at Startup.cs have something like so:

var builder = new ConfigurationBuilder()
      .SetBasePath(AppDomain.CurrentDomain.BaseDirectory)
      .AddJsonFile($"appsettings.json", optional: false, reloadOnChange: true)
      .AddJsonFile("secrets.json", optional: true, reloadOnChange: true)
      .AddEnvironmentVariables();
Configuration = builder.Build();

 
 

Grab something out on the very next line like so:

var includeSecretService = Configuration.GetSection("Foo").Exists();

 
 

Configuration the variable looks like this:

namespace MyMagic
{
   public class Startup
   {
      public Startup(IConfiguration configuration)
      {
         Configuration = configuration;
      }

No comments:

Post a Comment