gpt4 book ai didi

c# - Azure 应用服务应用程序设置值未更新

转载 作者:行者123 更新时间:2023-12-03 05:15:36 28 4
gpt4 key购买 nike

我正在尝试在测试 API 上加载 appsettings.test.json 文件,但在读取环境变量时遇到问题。在本地工作正常,但当我将其推送到 Azure 应用服务时,我不断指向我的 dev appsettings.json 文件。

我将在下面附上我的设置以及我用来验证此问题的 API 调用结果。除了 ASPNETCORE_ENVIRONMENT 之外,所有其他自定义值均有效。

Azure 应用服务应用程序设置

enter image description here

用于测试的应用服务端点

[HttpGet("env")]
public IActionResult GetEnvVariable()
{
var test = Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT");
var test1 = Environment.GetEnvironmentVariable("DOTNET_ENVIRONMENT");
return Ok($"ASPNETCORE: {test}\nDOTNET: {test1}");
}

应用服务端点响应

enter image description here

程序.cs

public class Program
{
public static void Main(string[] args)
{
CreateWebHostBuilder(args).Build().Run();
}

public static IWebHostBuilder CreateWebHostBuilder(string[] args) =>
WebHost.CreateDefaultBuilder(args).UseStartup<Startup>();
}

程序.cs

public class Startup
{
public Startup(IConfiguration configuration, IWebHostEnvironment env)
{
var builder = new ConfigurationBuilder()
.SetBasePath(env.ContentRootPath)
.AddJsonFile("appsettings.json", optional: false, reloadOnChange: true)
.AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true)
.AddEnvironmentVariables();

Configuration = builder.Build();
}

public IConfiguration Configuration { get; }

public void ConfigureServices(IServiceCollection services)
{
//register bugsnag, swagger, jwt auth, auto mapper, service classes, db context, controllers
}

public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
app.UseSwagger();

if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();

app.UseSwaggerUI(options =>
{
options.SwaggerEndpoint("/swagger/v1/swagger.json", "Test API V1");
});
}

app.UseAuthentication();

app.UseHttpsRedirection();

app.UseRouting();

app.UseAuthorization();

app.UseMiddleware<GlobalErrorHandlingMiddleware>();

app.UseEndpoints(endpoints =>
{
endpoints.MapControllers();
});
}
}

CSPROJ

<Project Sdk="Microsoft.NET.Sdk.Web">

<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<AssemblyName>$(MSBuildProjectName)</AssemblyName>
<RootNamespace>$(MSBuildProjectName.Replace(" ", "_"))</RootNamespace>
<Configurations>Release;Debug;Test</Configurations>
<Nullable>enable</Nullable>
<ImplicitUsings>enable</ImplicitUsings>
</PropertyGroup>

<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|AnyCPU'">
<Optimize>True</Optimize>
<DefineConstants>$(DefineConstants)</DefineConstants>
</PropertyGroup>

<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Test|AnyCPU' ">
<Optimize>True</Optimize>
<DefineConstants>$(DefineConstants)</DefineConstants>
<IntermediateOutputPath>obj\Release\net6.0</IntermediateOutputPath>
</PropertyGroup>

<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|AnyCPU'">
<DefineConstants>$(DefineConstants)</DefineConstants>
<Optimize>True</Optimize>
</PropertyGroup>
<ItemGroup>
<Content Remove="appsettings.Development.json" />
<Content Remove="appsettings.json" />
<Content Remove="appsettings.Production.json" />
<Content Remove="appsettings.Test.json" />
</ItemGroup>

<ItemGroup>
<None Include="appsettings.Development.json">
<CopyToOutputDirectory>Never</CopyToOutputDirectory>
</None>
<None Include="appsettings.json">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
<None Include="appsettings.Production.json">
<CopyToOutputDirectory>Never</CopyToOutputDirectory>
</None>
<None Include="appsettings.Test.json">
<CopyToOutputDirectory>Never</CopyToOutputDirectory>
</None>
</ItemGroup>

<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.Authentication.JwtBearer" Version="6.0.1" />
<PackageReference Include="Microsoft.AspNetCore.Mvc.NewtonsoftJson" Version="6.0.1" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="6.0.1">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="Serilog.AspNetCore" Version="6.0.1" />
<PackageReference Include="Swashbuckle.AspNetCore" Version="5.0.0" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\Test.Api.Business\Test.Api.Business.csproj" />
</ItemGroup>

<ItemGroup>
<Folder Include="wwwroot\" />
</ItemGroup>
</Project>

附加信息此应用服务正在使用 F1 免费套餐,这可能是原因吗?

最佳答案

仅供引用,如果有人遇到此问题。我的项目最初是一个 .NET CORE 3.1 项目,即使升级到 6 后,显然仍然存在挥之不去的文件,这些文件不会仅仅因为我针对的是较新的框架而被清除。

罪魁祸首是我的 web.config 文件,它覆盖了我的 ASPNETCORE_ENVIRONMENT 环境变量。

<environmentVariables>
<environmentVariable name="ASPNETCORE_ENVIRONMENT" value="Development" />
<environmentVariable name="ASPNETCORE_HTTPS_PORT" value="44364" />
<environmentVariable name="COMPLUS_ForceENC" value="1" />
</environmentVariables>

关于c# - Azure 应用服务应用程序设置值未更新,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/75037837/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com