英文:
Issues setting variables through appsetting files in .NET 7
问题
我无法在我的Azure App Service配置中看到我在appSettings.json
文件中设置的变量(或通过Kudus访问),示例.NET 7项目的appsettings.json
如下所示:
{
"MyVariable1": "Hello",
"MyVariable2": "World!",
"Logging": {
"LogLevel": {
"Default": "Debug",
"Microsoft.AspNetCore": "Debug"
}
}
}
在AppSettings
中无法看到"MyVariable1"和"MyVariable2"。我正在.NET 7.0的Web应用程序上工作,然后在program.cs
文件中像这样读取appsettings.json
文件:
// Program.cs
var builder = WebApplication.CreateBuilder(args);
// 尝试1 - 不起作用
builder.Configuration
.AddJsonFile("appsettings.json", optional: false, reloadOnChange: true)
.AddJsonFile($"appsettings{environment}.json", optional: true, reloadOnChange: true)
.AddEnvironmentVariables();
// 尝试2 - 不起作用
string? baseDirectory = Directory.GetCurrentDirectory();
builder.Configuration
.SetBasePath(baseDirectory)
.AddJsonFile(Path.Combine(baseDirectory, "appsettings.json"), optional: false, reloadOnChange: true)
.AddJsonFile(Path.Combine(baseDirectory, $"appsettings{environment}.json"), optional: true, reloadOnChange: true)
.AddEnvironmentVariables();
// 尝试3 - 不起作用
string? baseDirectory = AppContext.BaseDirectory;
builder.Configuration
.SetBasePath(baseDirectory)
.AddJsonFile(Path.Combine(baseDirectory, "appsettings.json"), optional: false, reloadOnChange: true)
.AddJsonFile(Path.Combine(baseDirectory, $"appsettings{environment}.json"), optional: true, reloadOnChange: true)
.AddEnvironmentVariables();
// 尝试4 - 不起作用
string? baseDirectory = Path.GetDirectoryName(System.Reflection.Assembly.GetEntryAssembly()?.Location);
builder.Configuration
.SetBasePath(baseDirectory)
.AddJsonFile(Path.Combine(baseDirectory, "appsettings.json"), optional: false, reloadOnChange: true)
.AddJsonFile(Path.Combine(baseDirectory, $"appsettings{environment}.json"), optional: true, reloadOnChange: true)
.AddEnvironmentVariables();
顺便说一下,我已经为每个appsettings
文件设置了"Copy Always"选项,但无论如何,它都不起作用。
<ItemGroup>
<None Include="..\editorconfig" Link=".editorconfig" />
<None Include="appsettings.Development.json">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
<None Include="appsettings.json">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
<None Include="appsettings.production.json">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
<None Include="appsettings.Staging.json">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
</ItemGroup>
有人知道我错过了什么吗?
英文:
I am not able to see on my Azure App Service Configuration (or accessing by Kudus), variables I set in appSettings.json
files in the program file.
Example of appsettings.json
on the .NET 7 project:
{
"MyVariable1": "Hello",
"MyVariable2": "World!",
"Logging": {
"LogLevel": {
"Default": "Debug",
"Microsoft.AspNetCore": "Debug"
}
}
}
I'm not able to see "MyVariable1", "MyVariable2" in AppSettings.
I'm working on a web application on .NET 7.0, then in the program.cs
file I'm reading the appsettings.json
file like this:
// Program.cs
var builder = WebApplication.CreateBuilder(args);
// Attempt #1 - Not working
builder.Configuration
.AddJsonFile($"appsettings.json", optional: false, reloadOnChange: true)
.AddJsonFile($"appsettings{environment}.json", optional: true, reloadOnChange: true)
.AddEnvironmentVariables();
// Attempt #2 - Not working
string? baseDirectory = Directory.GetCurrentDirectory();
builder.Configuration
.SetBasePath(baseDirectory)
.AddJsonFile(path: Path.Combine(baseDirectory, $"appsettings.json"), optional: false, reloadOnChange: true)
.AddJsonFile(path: Path.Combine(baseDirectory, $"appsettings{environment}.json"), optional: true, reloadOnChange: true)
.AddEnvironmentVariables();
// Attempt #3 - Not working
string? baseDirectory = AppContext.BaseDirectory;
builder.Configuration
.SetBasePath(baseDirectory)
.AddJsonFile(path: Path.Combine(baseDirectory, $"appsettings.json"), optional: false, reloadOnChange: true)
.AddJsonFile(path: Path.Combine(baseDirectory, $"appsettings{environment}.json"), optional: true, reloadOnChange: true)
.AddEnvironmentVariables();
// Attempt #4 - Not working
string? baseDirectory = Path.GetDirectoryName(System.Reflection.Assembly.GetEntryAssembly()?.Location);
builder.Configuration
.SetBasePath(baseDirectory)
.AddJsonFile(path: Path.Combine(baseDirectory, $"appsettings.json"), optional: false, reloadOnChange: true)
.AddJsonFile(path: Path.Combine(baseDirectory, $"appsettings{environment}.json"), optional: true, reloadOnChange: true)
.AddEnvironmentVariables()
BTW, I have already set "Copy Always" option for every single appsettings
file. Anyways, it does not work.
<ItemGroup>
<None Include="..\.editorconfig" Link=".editorconfig" />
<None Include="appsettings.Development.json">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
<None Include="appsettings.json">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
<None Include="appsettings.production.json">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
<None Include="appsettings.Staging.json">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
</ItemGroup>
Does anybody knows what I'm missing about this?
答案1
得分: 1
以下是已翻译的内容:
这是正常的。
与您在尝试一中添加了3个配置源一样,可以添加许多配置源,而appsettings.json
只是其中之一。
您直接在应用程序服务配置中配置的值是另一个配置源。在线编辑器(以及您正在使用的Kudu)只关注这些配置源,因此只显示它们,但所有添加的配置源都可用于您运行的代码。
请注意,WebApplication.CreateBuilder(args)
已经默认执行了您在问题中尝试的各种操作,因此在大多数情况下不需要该代码。
有关更多详细信息,请参阅此文档。
英文:
That's normal.
Much like you have added 3 sources of configuration in your attempt one, many sources can be added and appsettings.json
is just one of them.
The values you configure directly in the App Service Configuration are another. The online editor (and Kudu as you are using it) is only concerned with these and hence only showing them, but all sources added are available to your running code.
Note that WebApplication.CreateBuilder(args)
is already doing what you are trying to do with the various attempts in your question, by default, so that code is not needed in most scenarios.
See the docs on this for more details.
答案2
得分: 0
我已经在appsettings.json
文件中使用了您提供的相同配置,并将应用程序部署到Azure App Service。
您可以看到这些值既不在Configuration Section
中,也不在KUDU Console
的App Settings
中。
配置:
KUDU控制台
- 您可以看到
appsettings.json
也已经部署了。
路径:
https://YourAppName.scm.azurewebsites.net/DebugConsole
转到KUDU控制台,从Debug Console
中选择CMD
=> site
=> wwwroot
当我尝试从appsettings.json
中检索这些值时,您可以看到这些值被显示出来,没有任何问题。
.cshtml
@using Microsoft.Extensions.Configuration
@inject IConfiguration myconfig
<div class="text-center">
<h2>@myconfig["MyVariable1"]</h2> <h2> @myconfig["MyVariable2"]</h2>
</div>
-
当您想要更改
appsettings.json
文件中设置的变量(MyVariable1
和MyVariable2
)的值时,您需要在Azure App Service
的Configuration Section
中添加键值对。 -
键名必须与
appsettings.json
文件中提到的一样。
应用程序设置:
- 上述值现在在KUDU App Settings中可用。
从App Service更新的值:
英文:
I have used the same configurations inappsettings.json
file which you have provided and deployed the App to Azure App Service.
You can see the values are neither available in the Configuration Section
nor in the KUDU Console
- App Settings
.
Configuration:
KUDU Console
- You can see the
appsettings.json
is also deployed.
Path :
https://YourAppName.scm.azurewebsites.net/DebugConsole
Navigate to the KUDU Console, select CMD
from Debug Console
=> site
=> wwwroot
When I tried to retrieve the values from appsettings.json
, you can see the values are displayed without any issue.
.cshtml
@using Microsoft.Extensions.Configuration
@inject IConfiguration myconfig
<div class="text-center">
<h2>@myconfig["MyVariable1"]</h2> <h2> @myconfig["MyVariable2"]</h2>
</div>
-
When you want to change the values of the Variables (
MyVariable1
and
MyVariable2
) which you have set inappsettings.json
file, then you need to add the key-values in theConfiguration Section
of anAzure App Service
. -
The key name must be same as mentioned in the
appsettings.json
file.
Application Settings:
- The above values are now available in KUDU App Settings.
Updated values from App Service:
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论