简体   繁体   中英

.NET Core 2.2 - Unable to get app configuration from Azure AppConfig

I've created an Azure AppConfiguration store and get the Access Keys (EndPoint, Id, Secret, ConnectionString).

Now from my .NET Core WebAPI app, I want to read the configurations from Azure. I have tried many approaches that I found online but none of them works.

I already added references to the required Nuget libraries. My appsettings.json :

{
    "ConnectionString:AppConfig": "Endpoint=https://somewhere.azconfig.io;Id=Hidden;Secret=Hidden",
    "EndPoint": "https://somewhere.azconfig.io"
}

In my Program.cs I have:

public static IWebHostBuilder CreateWebHostBuilder(string[] args) =>
    WebHost.CreateDefaultBuilder(args)
    .ConfigureAppConfiguration((hostingContext, config) =>
    {
        var settings = config.Build();

        config.AddAzureAppConfiguration(options =>
        {
            options.Connect(settings["ConnectionStrings:AppConfig"]);
        });
    })
    .UseStartup<Startup>();

Then in my Startup.cs constructor:

public Startup(
    IConfiguration configuration, ---> checked this but no Azure config data
    IHostingEnvironment hostingEnv
)
    {
        var env = Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT");

        //this.Config = configuration;
        var builder = new ConfigurationBuilder()
                    .SetBasePath(hostingEnv.ContentRootPath)
                    .AddJsonFile(...)
                    .AddEnvironmentVariables();

        Configuration = builder.Build();
    }

And this is where I read the configurations, but all the values are null

public void ConfigureServices(IServiceCollection services)
    {
        var dbConnection = Configuration.GetSection("MyApp:Key1:Key2").Value; ---> get null
        services.AddDbContext<MyDbContext>(options => options.UseSqlServer(dbConnection));

        _jwtLifespan = Configuration.GetValue<int>("MyApp:Key3"); ----> get null
        _jwtSecretKey = Configuration.GetSection("MyApp:Key4").Value; ----> get null

        .....
    }

Anyone please check what I'm missing here and suggest me a working solution. Many thanks!

Suppose it's your connection setting problem. From your code you are using settings["ConnectionStrings:AppConfig"] to get the connection string,however your are using appsettings.json .

From the tutorial: Connect to an app configuration store , if you want to use settings["ConnectionStrings:AppConfig"] , use dotnet user-secrets set ConnectionStrings:AppConfig <your_connection_string> to add your connection string to Secret Manager.

Note: Secret manager is only for local test, When the app is deployed to Azure App Service, use an application setting Connection Strings in App Service.

And if you want to use appsettings.json , you could try the below code.

public static IWebHostBuilder CreateWebHostBuilder(string[] args) =>
WebHost.CreateDefaultBuilder(args)
    .ConfigureAppConfiguration((hostingContext, config) =>
    {

        var builder = new ConfigurationBuilder()

            .SetBasePath(Environment.CurrentDirectory)

            .AddJsonFile("appsettings.json", optional: false, reloadOnChange: true)

            .AddEnvironmentVariables();

        var Configuration = builder.Build();
        var settings = config.Build();
        config.AddAzureAppConfiguration(Configuration.GetSection("ConnectionStrings:AppConfig").Value);
    })
    .UseStartup<Startup>();

And then I use the tutorial example to show the value in the AppConfiguration.

在此处输入图像描述

在此处输入图像描述

Hope this could help you, if you still have other problem please feel free to let me know.

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