After upgrading a web job to .NET 6.0, it stopped recognizing the app settings and only works if we specify the settings in the app.config
.
To provide some context, this web job runs every 5 minutes, performs some logic, and then calls an Azure function.
Here’s a detailed rundown of what I’ve done:
I upgraded the project from .NET Framework 4.7.2 to .NET 6.0 and updated the dependent packages to their latest versions.
I noticed that JobHostConfiguration
is no longer available and that I have to use HostBuilder
from now on. I adjusted the code accordingly:
The web job initially started detecting my Azure function, but after deployment, it no longer recognizes the app settings.
Below is a snippet of my Azure function that fails to pick up the app settings:
using System.Configuration;
using System.Linq;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Threading.Tasks;
namespace CDP.Portal.Azure.WJCMTFileUpload
{
public class Functions
{
/// <summary>
/// Convert the customerlists into notes grouped by POS
/// </summary>
/// <returns></returns>
[NoAutomaticTrigger]
public static async Task RunFunction()
{
TelemetryClient appInsights = new TelemetryClient
{
InstrumentationKey = ConfigurationManager.AppSettings["AppInsights"]
};
try
{
var clientId = ConfigurationManager.AppSettings["ClientId"];
if (clientId != null && !string.IsNullOrEmpty(clientId))
{
//Common.Common.Log("ClientId", ConfigurationManager.AppSettings["ClientId"]);
//Common.Common.Log("ResourceUri", ConfigurationManager.AppSettings["ResourceUri"]);
//Common.Common.Log("KeyVaultUri", ConfigurationManager.AppSettings["KeyVaultUri"]);
//Common.Common.Log("KeyVaultSecretName", ConfigurationManager.AppSettings["KeyVaultSecretName"]);
//Common.Common.Log("ResourceApiUri", ConfigurationManager.AppSettings["ResourceApiUri"]);
string token = await CDP.Portal.Azure.Common.Common.AccessTokenGeneratorSecrets(ConfigurationManager.AppSettings["ClientId"],
ConfigurationManager.AppSettings["ResourceUri"],
ConfigurationManager.AppSettings["KeyVaultSecretName"]);
//Common.Common.Log("token", token);
CrmHelper._client = new HttpClient()
{
BaseAddress = new Uri(ConfigurationManager.AppSettings["ResourceApiUri"]),
DefaultRequestHeaders = { Authorization = new AuthenticationHeaderValue("Bearer", token) }
};
}
List<CustomerList> noteLists;
string targetFilename = ConfigurationManager.AppSettings["targetFilename"];
string nonoteRow = ConfigurationManager.AppSettings["nonoteRow"];
if (string.IsNullOrEmpty(targetFilename))
{
targetFilename = "CMT_";
}
try
{
noteLists = await CrmHelper.GetNotesToProcess(appInsights, targetFilename, nonoteRow);
}
The line:
var clientId = ConfigurationManager.AppSettings["ClientId"];
seems to return null even though clientId
is specified in the app settings.
I have already tried different things such as:
-
Using
CloudConfigurationManager.GetSetting
instead -
configSource=
in AppSettings
And here’s my .csproj file:
<Project Sdk="Microsoft.NET.Sdk;Microsoft.NET.Sdk.Publish">
<PropertyGroup Label="Globals">
<SccProjectName>SAK</SccProjectName>
<SccProvider>SAK</SccProvider>
<SccAuxPath>SAK</SccAuxPath>
<SccLocalPath>SAK</SccLocalPath>
</PropertyGroup>
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net6.0</TargetFramework>
</PropertyGroup>
<ItemGroup>
<Compile Remove="HelpersCrmHelper.cs" />
<Compile Remove="HelpersCsvHelper.cs" />
<Compile Remove="ModelsCustomerList.cs" />
<Compile Remove="ModelsNote.cs" />
<Compile Remove="ModelsOdataCollectionWrapper.cs" />
<Compile Remove="ModelsPos.cs" />
<Compile Remove="ModelsTargetList.cs" />
<Compile Remove="ModelsTranslation.cs" />
</ItemGroup>
<ItemGroup>
<None Remove="HelpersCsvHelper.cs~RF130716c7.TMP" />
</ItemGroup>
<ItemGroup>
<PackageReference Include="ClosedXML" Version="0.102.1" />
<PackageReference Include="Microsoft.ApplicationInsights" Version="2.22.0" />
<PackageReference Include="Microsoft.Azure.WebJobs.Extensions" Version="5.0.0" />
<PackageReference Include="Microsoft.Azure.WebJobs.Host.Storage" Version="5.0.0" />
<PackageReference Include="Microsoft.IdentityModel.Clients.ActiveDirectory" Version="5.3.0" />
<PackageReference Include="RestSharp" Version="110.2.0" />
<PackageReference Include="System.Spatial" Version="5.8.5" />
<PackageReference Include="Microsoft.Data.Edm" Version="5.8.5" />
<PackageReference Include="Microsoft.Data.OData" Version="5.8.5" />
<PackageReference Include="Microsoft.Data.Services.Client" Version="5.8.5" />
<PackageReference Include="Microsoft.WindowsAzure.ConfigurationManager" Version="3.2.3" />
<PackageReference Include="WindowsAzure.Storage" Version="9.3.3" />
<PackageReference Include="Microsoft.Azure.WebJobs.Core" Version="3.0.39" />
<PackageReference Include="Microsoft.Azure.WebJobs" Version="3.0.39" />
<PackageReference Include="Microsoft.Azure.KeyVault.Core" Version="3.0.5" />
<PackageReference Include="Newtonsoft.Json" Version="13.0.3" />
<PackageReference Include="System.Configuration.ConfigurationManager" Version="8.0.0" />
</ItemGroup>
<ItemGroup>
<None Update="settings.job">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
</ItemGroup>
<ItemGroup>
<Folder Include="Helpers" />
<Folder Include="Models" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..CDP.Portal.Azure.CommonCDP.Portal.Azure.Common.csproj" />
</ItemGroup>
</Project>
2
Answers
Thanks for your feedback.
In the meantime, I tried Environment.GetEnvironmentVariable("ClientId") and, surprisingly, it worked as expected. I've adapted all my code to it.
I still don't know why, because I know they're both supposed to do the same thing.
You have set the Environment to Development in
Program.cs
file. Initially even I got the value as null.Remove the below line from
Program.cs
file.As a workaround, I have used your WebJob Function with my sample code to retrieve config values.
Functions.cs
file, instead ofConfigurationManager
, retrieve the values usingIConfiguration
.My
Functions.cs
file:Environment Variables
.Output: