简体   繁体   English

.NET Core 2.2 - 无法从 Azure AppConfig 获取应用程序配置

[英].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).我创建了一个 Azure AppConfiguration 存储并获取访问密钥(EndPoint、Id、Secret、ConnectionString)。

Now from my .NET Core WebAPI app, I want to read the configurations from Azure.现在从我的 .NET Core WebAPI 应用程序中,我想从 Azure 中读取配置。 I have tried many approaches that I found online but none of them works.我尝试了很多我在网上找到的方法,但没有一个有效。

I already added references to the required Nuget libraries.我已经添加了对所需 Nuget 库的引用。 My appsettings.json :我的appsettings.json

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

In my Program.cs I have:在我的Program.cs中,我有:

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:然后在我的Startup.cs构造函数中:

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这是我读取配置的地方,但所有值都是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 .从您的代码中,您使用settings["ConnectionStrings:AppConfig"]获取连接字符串,但是您使用的是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.从教程: 连接到应用程序配置商店,如果您想使用settings["ConnectionStrings:AppConfig"] ,请使用dotnet user-secrets set ConnectionStrings:AppConfig <your_connection_string>将您的连接字符串添加到 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.注意: Secret manager 仅用于本地测试,当应用部署到 Azure 应用服务时,请使用应用服务中的应用程序设置连接字符串。

And if you want to use appsettings.json , you could try the below code.如果你想使用appsettings.json ,你可以试试下面的代码。

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.然后我使用教程示例来显示 AppConfiguration 中的值。

在此处输入图像描述

在此处输入图像描述

Hope this could help you, if you still have other problem please feel free to let me know.希望这可以帮助你,如果你还有其他问题,请随时让我知道。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何让 IOptionsMonitor<T> 从托管在 Azure Windows Server VM 上的正在运行的 .NET Core 2.2 应用程序获取最新配置值? - How to let IOptionsMonitor<T> get the latest configuration value from a running .NET Core 2.2 app hosted on an Azure Windows Server VM? 使用 .NET Core 2.2 从 Azure 存储中获取所有 Blob - Get all Blobs From Azure Storage using .NET Core 2.2 从 Azure 应用配置动态更新 .net 核心配置 - Dynamically update .net core config from Azure App Configuration .NET Core 2.2:如何在 Azure 应用服务中从 Azure AD 自动填充用户列表? - .NET Core 2.2 : How to auto-populate list of users from Azure AD in Azure app service.? Serilog无法使用asp.net core 2.2 API中的配置 - Serilog not working from configuration in asp.net core 2.2 API 无法通过UserManager更新用户 <TUser> 在asp.net core 2.2应用程序中使用ef core 2.2 - unable to update user via UserManager<TUser> using ef core 2.2 in asp.net core 2.2 app 获取社交登录提供商返回的流量,以在我的.net core 2.2 Web应用程序上创建本地帐户 - Get traffic returned from social login providers to create a local account on my .net core 2.2 web app 无法将文件从Angular 8上传到Asp.net Core 2.2 - Unable to File Upload From Angular 8 to Asp.net Core 2.2 .NET Core 2.2中的DDD实体从属性中获取 - DDD entities in .NET Core 2.2 Get from properties 无法使用 dinktopdf 为 .net core2.2 azure functionapp 加载 dll libwkhtmltox - Unable to load the dll libwkhtmltox using dinktopdf for a .net core2.2 azure functionapp
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM