[英].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.