我有一个使用GitHub操作部署到Azure应用服务的.NET6Web API.

我的Web API包含3个应用程序设置文件:

  • Appsettings.json

  • Appsettings.development.json

  • Appsettings.production.json

据我所知,这些文件通常在本地用于开发和测试目的.

当部署Web应用程序时,它会正确运行并读取应用程序设置中的所有值(以某种方式).然而,配置设置中Azure门户中的"应用程序设置"为空.这让我感到困惑,因为我找不到这些应用程序设置是从哪里读取的.

这是我的GitHub行动YAML:

name: API CI/CD

env:
  AZURE_WEBAPP_NAME: test    # set this to the name of your Azure Web App
  AZURE_WEBAPP_PACKAGE_PATH: '.'      # set this to the path to your web app project, defaults to the repository root
  DOTNET_VERSION: '6.0'                 # set this to the .NET Core version to use

on:
  push:
    branches: [ "main" ]
  workflow_dispatch:

permissions:
  contents: read

jobs:
  build:
    runs-on: ubuntu-latest

    steps:
      - uses: actions/checkout@v3

      - name: Set up .NET Core
        uses: actions/setup-dotnet@v2
        with:
          dotnet-version: ${{ env.DOTNET_VERSION }}

      - name: Set up dependency caching for faster builds
        uses: actions/cache@v3
        with:
          path: ~/.nuget/packages
          key: ${{ runner.os }}-nuget-${{ hashFiles('**/packages.lock.json') }}
          restore-keys: |
            ${{ runner.os }}-nuget-

      - name: Build with dotnet
        run: dotnet build --configuration Release

      - name: dotnet publish
        run: dotnet publish  Test.Api -c Release -o ${{env.DOTNET_ROOT}}/myapp

      - name: Upload artifact for deployment job
        uses: actions/upload-artifact@v3
        with:
          name: .net-app
          path: ${{env.DOTNET_ROOT}}/myapp

  deploy:
    permissions:
      contents: none
    runs-on: ubuntu-latest
    needs: build
    environment:
      name: 'production'
      url: ${{ steps.deploy-to-webapp.outputs.webapp-url }}

    steps:
      - name: Download artifact from build job
        uses: actions/download-artifact@v3
        with:
          name: .net-app

      - name: Deploy to Azure Web App
        id: deploy-to-webapp
        uses: azure/webapps-deploy@v2
        with:
          app-name: ${{ env.AZURE_WEBAPP_NAME }}
          publish-profile: ${{ secrets.AZURE_WEBAPP_PUBLISH_PROFILE }}
          package: ${{ env.AZURE_WEBAPP_PACKAGE_PATH }}

这是我的程序.cs:

var builder        = WebApplication.CreateBuilder(args);

// Add services to the container.
builder.Services.RegisterApiVersioning();
builder.Services.AddControllers();
builder.Services.AddEndpointsApiExplorer();
builder.Services.RegisterOptions(builder.Configuration);
builder.Services.RegisterServices(builder.Configuration);
builder.Services.RegisterSwagger();

var app = builder.Build();

app.UseCors(maksCorsPolicy);
app.ConfigureSwagger();
app.UseHttpsRedirection();
app.UseAuthentication();
app.UseAuthorization();
app.MapControllers();
app.UseMiddleware<ExceptionMiddleware>();

app.Run();

I tried to change a value in my Appsettings.production.json, and it was taken into account when I deployed. This confused me even more since I still can't see any app setting on the Azure Portal, but the application is finding it.

我试着在GitHub和我的应用程序服务上搜索是否有任何配置告诉我的应用程序读取这些JSON文件没有任何运气.

我希望我的应用程序找不到这些应用程序设置.

任何解释都将不胜感激.

推荐答案

您的应用程序正在读取您的appsettings.json个文件,即使在部署时也是如此.如果您右键单击并为您的appsettings.{env}.json个文件 Select Properties,您将看到它们被设置为:

Build Action: Content
Copy to Output Directory: If Newer

这意味着这些文件将随您的展开一起发送.

Azure应用程序服务实际上不会根据这些设置填充Configuration视图,你必须自己设置它们.(或使用部署操作通过API设置它们).

在任何情况下,如果您在Azure AppService中使用正确的名称手动设置新设置,这将覆盖您的appsettings.json个文件中包含的任何内容.

确保在Azure中创建设置时小心遵循正确的命名约定:

  • Linux应用程序服务需要__作为分隔符,例如:Credentials__APIKey
  • Windows应用服务将使用:作为分隔符,例如:Credentials:APIKey

你可以阅读关于命名here.的更多信息

请注意,您还可以使用JSON视图,通过单击Advanced Edit来对Azure门户中的配置值执行批量修改

.net相关问答推荐

为什么DotNet新的webapi;命令会为我生成不同的文件夹

仅在有换行符时捕获分隔符之间的所有文本

在 C# 中,如何使用泛型的基类将泛型接口的所有实例注入到单个构造函数中?

.NET - WindowStyle = hidden 与 CreateNoWindow = true?

是否可以像 WebView 一样在 Windows 窗体中嵌入 Gecko 或 Webkit?

来自奥尔森时区的 .NET TimeZoneInfo

将笔画应用于 WPF 中的文本块

.NET 4.0 中有内置的二叉搜索树吗?

ReaderWriterLock 与锁{}

在 C# 中转义命令行参数

日期时间是什么意思?在 C# 中是什么意思?

在安全处理异常时避免首次机会异常消息

C# 的浮点比较函数

mscorlib 代表什么?

Visual Studio 2017 和 2019 突出显示滚动条中所选单词的出现

在 C#/.NET 中组合路径和文件名的最佳方法是什么?

如何防止任务的同步延续?

何时何地使用 GetType() 或 typeof()?

String.Join 与 StringBuilder:哪个更快?

如何从 webclient 获取状态码?