目前,我们正在将 .NET Framework WEB API 迁移到 .NET 8。与此同时,我们正在从服务结构迁移到 Azure 容器应用程序。 我们希望在代码中使用 AppSettings.json 文件中的应用程序设置,但我们无法找到在 Azure 容器应用程序中实现/使用它的步骤。
到目前为止,我们已按照以下步骤使用 AppSettings.json:
From mcr.microsoft.com/dotnet/sdk:8.0 AS build ARG BUILD_CONFIGURATION=Release WORKDIR /src COPY ["DummyWebApi/DummyWebApi.csproj", "DummyWebApi/"] COPY ["DummyWebApi/appsettings.json", "/appsettings.json"]
更改代码以使用应用程序设置
private readonly ILogger<WeatherForecastController> _logger;
private readonly IConfiguration _configuration;
public WeatherForecastController(ILogger<WeatherForecastController> logger, IConfiguration configuration)
{
_logger = logger;
_configuration = configuration;
}
[HttpGet(Name = "GetWeatherForecast")]
public string Get()
{
string? appSettingValue = _configuration["appSettingKey"];
return $"dummy response + {appSettingValue}";
}
部署后,我收到以下错误
激活失败 --> TargetPort 8080 与监听不匹配 端口 5000
在容器应用程序中配置设置与在普通 Web 应用程序中配置设置非常相似且简单。
appsettings.json
文件部署到应用程序根目录。我的
.csproj
文件:
<Project Sdk="Microsoft.NET.Sdk.Web">
<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
<Nullable>enable</Nullable>
<ImplicitUsings>enable</ImplicitUsings>
<UserSecretsId>****</UserSecretsId>
<DockerDefaultTargetOS>Linux</DockerDefaultTargetOS>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.VisualStudio.Azure.Containers.Tools.Targets" Version="1.21.0" />
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.4.0" />
</ItemGroup>
</Project>
我的
Docker file
:
FROM mcr.microsoft.com/dotnet/aspnet:8.0 AS base
USER app
WORKDIR /app
EXPOSE 8080
EXPOSE 8081
FROM mcr.microsoft.com/dotnet/sdk:8.0 AS build
ARG BUILD_CONFIGURATION=Release
WORKDIR /src
COPY ["ContainerAppSetting/ContainerAppSetting.csproj", "ContainerAppSetting/"]
RUN dotnet restore "./ContainerAppSetting/ContainerAppSetting.csproj"
COPY . .
WORKDIR "/src/ContainerAppSetting"
RUN dotnet build "./ContainerAppSetting.csproj" -c $BUILD_CONFIGURATION -o /app/build
FROM build AS publish
ARG BUILD_CONFIGURATION=Release
RUN dotnet publish "./ContainerAppSetting.csproj" -c $BUILD_CONFIGURATION -o /app/publish /p:UseAppHost=false
FROM base AS final
WORKDIR /app
COPY --from=publish /app/publish .
ENTRYPOINT ["dotnet", "ContainerAppSetting.dll"]
字符串? appSettingValue = _configuration["appSettingKey"];
输出(
appsettings.json
文件中设置的值:
appsettings.json
=> Container App
=> Containers
部分覆盖 Environment variables
文件中设置的值。覆盖值: