Search code examples
c#asp.net-coreasp.net-web-apiuser-secret

How come an upgraded ASP.net Core 6.0 to 8.0 App isnt automatically able to use the User Secrets Configuration Provider?


I have upgraded my ASP.NET Core app from version 6.0 to 8.0. I have upgraded all the nuget packages.

I have a Secrets.json file in Connected Services in VS 2022. I have the "ConnectionStrings.AppConfig" value in it.

When I start the application it doesn't use the user secrets.

However, when I add "builder.Configuration.AddUserSecrets();" to my Program.cs it works. I shouldn't have to do this.

When I create a brand new ASP.NET Core app everything works fine without me having to put in that line.

What do I need to do to make it work without adding that line of code?


Solution

  • Can you please run these checks to verify:

    using Microsoft.Extensions.Configuration.Json;
    using Microsoft.Extensions.Configuration.UserSecrets;
    
    var builder = WebApplication.CreateBuilder(args);
    
    var env = builder.Environment;
    if (!env.IsDevelopment())
    {
        throw new NotSupportedException("Not development env");
    }
    if (env.ApplicationName.Length == 0)
    {
        throw new NotSupportedException("ApplicationName not set");
    }
    
    var secretsPath = PathHelper.GetSecretsPathFromSecretsId("8fd96a16-931a-4460-ab39-b58c8acc992e");
    if (!File.Exists(secretsPath))
    {
        throw new NotSupportedException("secrets.json does not exist");
    }
    
    var configSource = builder.Configuration.Sources.OfType<JsonConfigurationSource>().FirstOrDefault(x => x.Path == "secrets.json");
    if (configSource is null)
    {
        throw new NotSupportedException("No secrets.json in config");
    }
    
    var appCongifConnectionString = builder.Configuration.GetConnectionString("AppConfig");
    if (string.IsNullOrEmpty(appCongifConnectionString))
    {
        throw new NotSupportedException("appCongifConnectionString is empty");
    }
    

    Which exception is thrown?