In a Function App that we develop locally in Visual Studio, we have two environments to run against: Test and Development.
Now, we have a local.settings.json
and two respective environment-specific local.settings.<environment>.json
which works totally fine – with one exception: The values used in the function trigger annotation.
They seem to be resolved before the actual process is even started and from local.settings.json
, only.
It’s dotnet 6, v4, isolated process. The project is set up to read from local.settings.json
and local.settings.<environment>.json
.
The relevant files look like this:
lauchSettings.json
{
"profiles": {
"My.Project.Functions": {
...,
"environmentVariables": {
"AZURE_FUNCTIONS_ENVIRONMENT": "Test"
}
}
}
}
local.settings.Test.json
{
"myTopicName": "some-topic-for-test",
"myServiceBusConnection": "Endpoint=sb://MyTestBusURI"
}
MyFunction.cs
public class MyFunction
{
[Function(nameof(MyFunctionAsync))]
public async Task MyFunctionAsync(
[ServiceBusTrigger("%myTopicName%", Connection = "myServiceBusConnection")] // <-- error here
MyMessageEntity myMessageEntity)
{
// resolving environment-specific values _here_ will work perfectly fine.
await _someIrrelevantService.DoSomethingAsync(myMessageEntity);
}
}
If we do not have myTopicName
and myServiceBusConnection
in local.settings.json
but only in the environment specific settings, the app won’t start, complaining that the respective settings are not found.
We really would like to avoid having to copy around settings, making this tedious and error-prone.
I have been through a lot of Microsoft documentation, which unfortunately never speaks about environment specific settings files in local development. So, I am dreading this may not even be possible but:
Does anyone have an idea, how I can make the tooling respect the environment specific settings in the annotation? (“You cannot” would be a valid answer, so I can continue looking for workarounds without a bad conscience 🙂 )
As requested: We load configs like so:
return hostBuilder
.ConfigureAppConfiguration((context, builder) =>
{
builder.SetBasePath(Directory.GetCurrentDirectory())
.AddJsonFile("local.settings.json", optional: true, reloadOnChange: true)
.AddJsonFile($"local.settings.{context.HostingEnvironment.EnvironmentName}.json", optional: true, reloadOnChange: true)
.AddEnvironmentVariables();
})
Which is working fine but is irrelevant because it happens, after the annotation’s references are resolved.
{
“IsEncrypted”: false,
“Values”: {
“FUNCTIONS_WORKER_RUNTIME”: “<language worker>”,
“AzureWebJobsStorage”: “<connection-string>”,
“MyBindingConnection”: “<binding-connection-string>”,
“AzureWebJobs.HttpExample.Disabled”: “true”
},
“Host”: {
“LocalHttpPort”: 7071,
“CORS”: “*”,
“CORSCredentials”: false
},
“ConnectionStrings”: {
“SQLConnectionString”: “<sqlclient-connection-string>”
}
}