在.NET 7中通过appsetting文件设置变量的问题

huangapple go评论85阅读模式
英文:

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:

{
  &quot;MyVariable1&quot;: &quot;Hello&quot;,
  &quot;MyVariable2&quot;: &quot;World!&quot;,
  &quot;Logging&quot;: {
    &quot;LogLevel&quot;: {
      &quot;Default&quot;: &quot;Debug&quot;,
      &quot;Microsoft.AspNetCore&quot;: &quot;Debug&quot;
    }
  }
}

在.NET 7中通过appsetting文件设置变量的问题

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($&quot;appsettings.json&quot;, optional: false, reloadOnChange: true)
    .AddJsonFile($&quot;appsettings{environment}.json&quot;, optional: true, reloadOnChange: true)    
    .AddEnvironmentVariables();

// Attempt #2 - Not working
string? baseDirectory = Directory.GetCurrentDirectory();
builder.Configuration
    .SetBasePath(baseDirectory)
    .AddJsonFile(path: Path.Combine(baseDirectory, $&quot;appsettings.json&quot;), optional: false, reloadOnChange: true)
    .AddJsonFile(path: Path.Combine(baseDirectory, $&quot;appsettings{environment}.json&quot;), optional: true, reloadOnChange: true)
    .AddEnvironmentVariables();

// Attempt #3 - Not working
string? baseDirectory = AppContext.BaseDirectory;
builder.Configuration
    .SetBasePath(baseDirectory)
    .AddJsonFile(path: Path.Combine(baseDirectory, $&quot;appsettings.json&quot;), optional: false, reloadOnChange: true)
    .AddJsonFile(path: Path.Combine(baseDirectory, $&quot;appsettings{environment}.json&quot;), 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, $&quot;appsettings.json&quot;), optional: false, reloadOnChange: true)
    .AddJsonFile(path: Path.Combine(baseDirectory, $&quot;appsettings{environment}.json&quot;), optional: true, reloadOnChange: true)
    .AddEnvironmentVariables()

BTW, I have already set "Copy Always" option for every single appsettings file. Anyways, it does not work.

&lt;ItemGroup&gt;
    &lt;None Include=&quot;..\.editorconfig&quot; Link=&quot;.editorconfig&quot; /&gt;
    &lt;None Include=&quot;appsettings.Development.json&quot;&gt;
      &lt;CopyToOutputDirectory&gt;Always&lt;/CopyToOutputDirectory&gt;
    &lt;/None&gt;
    &lt;None Include=&quot;appsettings.json&quot;&gt;
      &lt;CopyToOutputDirectory&gt;Always&lt;/CopyToOutputDirectory&gt;
    &lt;/None&gt;
    &lt;None Include=&quot;appsettings.production.json&quot;&gt;
      &lt;CopyToOutputDirectory&gt;Always&lt;/CopyToOutputDirectory&gt;
    &lt;/None&gt;
    &lt;None Include=&quot;appsettings.Staging.json&quot;&gt;
      &lt;CopyToOutputDirectory&gt;Always&lt;/CopyToOutputDirectory&gt;
    &lt;/None&gt;
  &lt;/ItemGroup&gt;

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 ConsoleApp Settings中。

配置:
在.NET 7中通过appsetting文件设置变量的问题

KUDU控制台
在.NET 7中通过appsetting文件设置变量的问题

  • 您可以看到appsettings.json也已经部署了。
    路径:
    https://YourAppName.scm.azurewebsites.net/DebugConsole

转到KUDU控制台,从Debug Console中选择CMD => site => wwwroot

在.NET 7中通过appsetting文件设置变量的问题

当我尝试从appsettings.json中检索这些值时,您可以看到这些值被显示出来,没有任何问题。

在.NET 7中通过appsetting文件设置变量的问题

.cshtml

@using Microsoft.Extensions.Configuration
@inject IConfiguration myconfig
<div class="text-center">
<h2>@myconfig["MyVariable1"]</h2>  <h2> @myconfig["MyVariable2"]</h2> 
</div>
  • 当您想要更改appsettings.json文件中设置的变量(MyVariable1MyVariable2)的值时,您需要在Azure App ServiceConfiguration Section中添加键值对。

  • 键名必须与appsettings.json文件中提到的一样。

应用程序设置:

在.NET 7中通过appsetting文件设置变量的问题

  • 上述值现在在KUDU App Settings中可用。

在.NET 7中通过appsetting文件设置变量的问题

从App Service更新的值:

在.NET 7中通过appsetting文件设置变量的问题

英文:

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:
在.NET 7中通过appsetting文件设置变量的问题

KUDU Console
在.NET 7中通过appsetting文件设置变量的问题

  • 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

在.NET 7中通过appsetting文件设置变量的问题

When I tried to retrieve the values from appsettings.json, you can see the values are displayed without any issue.

在.NET 7中通过appsetting文件设置变量的问题

.cshtml

@using Microsoft.Extensions.Configuration
@inject IConfiguration myconfig
&lt;div class=&quot;text-center&quot;&gt;
&lt;h2&gt;@myconfig[&quot;MyVariable1&quot;]&lt;/h2&gt;  &lt;h2&gt; @myconfig[&quot;MyVariable2&quot;]&lt;/h2&gt; 
&lt;/div&gt;
  • When you want to change the values of the Variables (MyVariable1 and
    MyVariable2) which you have set in appsettings.json file, then you need to add the key-values in the Configuration Section of an Azure App Service.

  • The key name must be same as mentioned in the appsettings.json file.

Application Settings:

在.NET 7中通过appsetting文件设置变量的问题

  • The above values are now available in KUDU App Settings.

在.NET 7中通过appsetting文件设置变量的问题

Updated values from App Service:

在.NET 7中通过appsetting文件设置变量的问题

huangapple
  • 本文由 发表于 2023年7月13日 11:12:27
  • 转载请务必保留本文链接:https://go.coder-hub.com/76675654.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定