我有一个 .NET 6 Web API,我使用 GitHub 操作将其部署到 Azure 应用服务。
我的 Web API 包含 3 个应用程序设置文件:
appsettings.json
appsettings.development.json
appsettings.生产.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();
我尝试更改 appsettings.production.json 中的值,并且在部署时已将其考虑在内。这让我更加困惑,因为我仍然在 Azure 门户上看不到任何应用程序设置,但应用程序正在找到它。
我尝试在 GitHub 和我的应用程序服务上搜索任何告诉我的应用程序读取这些 JSON 文件的配置,但没有任何运气。
我希望我的应用程序找不到这些应用程序设置。
任何解释将不胜感激。
即使在部署时,您的应用程序也会读取您的
appsettings.json
文件。如果您右键单击并为您的 Properties
文件选择 appsettings.{env}.json
,您将看到它们被设置为:
Build Action: Content
Copy to Output Directory: If Newer
这意味着文件将与您的部署一起发送。
Azure 应用服务实际上并不通过这些设置填充
Configuration
视图,您必须自行设置它们。 (或者使用部署操作通过 API 设置它们)。
无论如何,如果您在 Azure AppService 中使用正确的名称手动设置新设置,这将覆盖您的
appsettings.json
文件中包含的所有内容。
确保在 Azure 中创建设置时,小心遵循正确的命名约定:
__
作为分隔符,因此,例如:Credentials__APIKey
:
作为分隔符,因此,例如:Credentials:APIKey
JSON
视图通过单击
Advanced Edit
对 Azure 门户内的配置值进行批量修改