.NET Azure Function error - A connection string was not found

Surprisingly, a freshly created .NET Azure Function project isn't running for me without making a small, but critical configuration change.

.NET

Azure Functions

macOS

I’m trying out running .NET Azure Functions on my new laptop, and thought I’d start from scratch by creating a brand new .NET Azure Function project.

Azure Function logo

I installed the Azure Function Core Tools using Homebrew. (Note that official instructions missed the ‘trust’ command):

brew tap azure/functions
brew trust azure/functions
brew install azure-functions-core-tools@4

I then created a new Function project using the CLI, following these instructions.

func init --worker-runtime dotnet-isolated --language csharp

And then attempt to run it:

dotnet run

And was greeted by this error:

Failed to start language worker process for runtime: dotnet-isolated. workerId:80487f5d-6d73-4858-a55a-2e56d0853367
Language Worker Process exited. Pid=4680.
Microsoft.Azure.WebJobs.Script.Grpc: dotnet exited with code 134 (0x86). Unhandled exception. System.InvalidOperationException: A connection string was not found. Please set your connection string.
Removing errored webhost language worker channel for runtime: dotnet-isolated workerId:80487f5d-6d73-4858-a55a-2e56d0853367
Microsoft.Azure.WebJobs.Script.Grpc: dotnet exited with code 134 (0x86). Unhandled exception. System.InvalidOperationException: A connection string was not found. Please set your connection string.
A host error has occurred during startup operation '67dfffee-7a1e-4970-9014-209232677f0f'.
Microsoft.Azure.WebJobs.Script.Grpc: dotnet exited with code 134 (0x86). Unhandled exception. System.InvalidOperationException: A connection string was not found. Please set your connection string.
Failed to stop host instance 'a4c866ff-2d8e-4423-b022-308f56a22348'.
Microsoft.Azure.WebJobs.Host: The host has not yet started.
meter 'provider')
Host startup operation has been canceled

That’s a weird error. My Function app is pretty much empty - there’s the Program.cs file but I haven’t even added any actual Functions yet!

A bit of searching and throwing the error at Copilot turned up the suggestion that it’s the UseAzureMonitorExporter() method call that is added by default in Program.cs that is the culprit:

builder.Services.AddOpenTelemetry()
    .UseFunctionsWorkerDefaults()
    .UseAzureMonitorExporter();

It is throwing an error because APPLICATIONINSIGHTS_CONNECTION_STRING environment variable has not been set. It’s a pity the error message was not more descriptive.

The solution is to either change the code so that UseAzureMonitorExporter() is only called if that environment variable has been set, or alternatively make sure that it is set.

For local development you can add it to your local.settings.json file. If you don’t have a real App Insights resource to point to, you can set it to InstrumentationKey=00000000-0000-0000-0000-000000000000 which is recognised as a “null” connection string. With that value it no longer fails, but all traces and telemetry are just ignored.

{
  "IsEncrypted": false,
  "Values": {
    "AzureWebJobsStorage": "UseDevelopmentStorage=true",
    "FUNCTIONS_WORKER_RUNTIME": "dotnet-isolated",
    "APPLICATIONINSIGHTS_CONNECTION_STRING": "InstrumentationKey=00000000-0000-0000-0000-000000000000"
  }
}

If you deployed this Function App to Azure, you would ideally have a real Application Insights resource, and had set the APPLICATIONINSIGHTS_CONNECTION_STRING to point to it.

See Connection strings in Application Insights for more info about what options there are for connection strings.

Not an ideal experience. Hopefully they’ll update the generated code so that it includes that so things work right out of the box.