I am trying to load my appsettings.test.json file on a test API and I am having problems reading environment variables. Works fine locally but when I push it to the Azure App Service I keep getting pointed to my dev appsettings.json file.
I will attach my settings below and the API call results I used to verify this issue. Every other custom value works aside ASPNETCORE_ENVIRONMENT.
Azure App Service Application Settings
App Service Endpoint for testing
[HttpGet("env")]
public IActionResult GetEnvVariable()
{
var test = Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT");
var test1 = Environment.GetEnvironmentVariable("DOTNET_ENVIRONMENT");
return Ok($"ASPNETCORE: {test}nDOTNET: {test1}");
}
App Service Endpoint response
Program.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>();
}
Program.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>objReleasenet6.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.BusinessTest.Api.Business.csproj" />
</ItemGroup>
<ItemGroup>
<Folder Include="wwwroot" />
</ItemGroup>
</Project>
Additional Information
This app service is using the F1 free plan, could that be why?
2
Answers
FYI, if anyone encounters this issue. My project was originally a .NET CORE 3.1 project, even after upgrading to 6 there were obviously still lingering files which wouldn't be cleared just because I targeted a newer framework.
The culprit was my
web.config
file, it was overriding myASPNETCORE_ENVIRONMENT
env variable.If you’re using the Azure NuGet packages (
Azure.ResourceManager.Resources
,Azure.ResourceManager.AppService
) to update an App Setting, you can do something like this: