Skip to content
Docs Try Aspire
Docs Try

Azure Functions runtime configuration

Azure Functions logo

This page explains how Azure Functions code reads the connection information that Aspire injects at run time. For the AppHost API surface — adding the Functions project, host storage, resource references, external HTTP endpoints, and Durable Task Scheduler — see Set up Azure Functions in the AppHost.

Azure Functions is compute, not a backing service

Section titled “Azure Functions is compute, not a backing service”

Most Aspire integrations follow a pattern where the AppHost provisions a backing service (a database, cache, or message broker) and consuming apps read its connection information. Azure Functions reverses this relationship: the Functions project is itself the consumer. Aspire injects connection strings and endpoints for the Azure resources you reference in the AppHost, and your function code reads those values from environment variables at run time.

When the AppHost calls WithReference on the Functions project resource, Aspire writes the referenced resource connection properties as environment variables into the Functions host process. The variable names follow the standard Aspire pattern: [RESOURCE]_[PROPERTY] (uppercased).

For example, if the AppHost references a blob storage container named blobs, Aspire injects:

Environment variableExample value
BLOBS_BLOBENDPOINThttps://127.0.0.1:10000/devstoreaccount1
BLOBS_QUEUEENDPOINThttps://127.0.0.1:10001/devstoreaccount1
BLOBS_TABLEENDPOINThttps://127.0.0.1:10002/devstoreaccount1

For a Service Bus namespace named servicebus, Aspire injects:

Environment variableExample value
SERVICEBUS_FULLYQUALIFIEDNAMESPACEsb://servicebus.servicebus.windows.net/

The exact set of properties depends on the resource type. See the binding-specific articles linked in the See also section for the properties exposed by each resource.

Pick the language your Functions project is written in.

The .NET isolated worker model starts the Functions host through a standard HostBuilder. You can read Aspire-injected values through IConfiguration (which maps environment variables automatically) or through Environment.GetEnvironmentVariable.

IConfiguration is available through dependency injection. Register a class that receives the configuration section you need:

C# — Program.cs
using Microsoft.Azure.Functions.Worker;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
var host = new HostBuilder()
.ConfigureFunctionsWebApplication()
.ConfigureServices(services =>
{
services.AddSingleton<MyBlobService>();
})
.Build();
await host.RunAsync();
C# — MyBlobService.cs
using Microsoft.Extensions.Configuration;
public class MyBlobService(IConfiguration config)
{
// Aspire injects BLOBS_BLOBENDPOINT for a resource named "blobs"
private readonly string _endpoint = config["BLOBS_BLOBENDPOINT"]!;
}

Read through Environment.GetEnvironmentVariable

Section titled “Read through Environment.GetEnvironmentVariable”

For one-off reads, use Environment.GetEnvironmentVariable directly in your function class:

C# — MyFunction.cs
using Microsoft.Azure.Functions.Worker;
using Microsoft.Azure.Functions.Worker.Http;
public class MyFunction
{
[Function("MyHttpFunction")]
public HttpResponseData Run(
[HttpTrigger(AuthorizationLevel.Function, "get")] HttpRequestData req)
{
// Read an Aspire-injected Service Bus namespace
var sbNamespace = Environment.GetEnvironmentVariable(
"SERVICEBUS_FULLYQUALIFIEDNAMESPACE");
var response = req.CreateResponse();
response.WriteString($"Service Bus namespace: {sbNamespace}");
return response;
}
}

Use connection name attributes for triggers

Section titled “Use connection name attributes for triggers”

Azure Functions trigger attributes accept a Connection parameter that maps to the environment variable prefix Aspire injects. For a Service Bus queue trigger using the servicebus resource:

C# — MyQueueFunction.cs
using Microsoft.Azure.Functions.Worker;
public class MyQueueFunction
{
[Function("MyQueueFunction")]
public void Run(
[ServiceBusTrigger("my-queue", Connection = "servicebus")]
string message)
{
// 'Connection = "servicebus"' tells the Functions runtime to look for
// SERVICEBUS_FULLYQUALIFIEDNAMESPACE (injected by Aspire)
Console.WriteLine($"Received: {message}");
}
}

local.settings.json for local development without Aspire

Section titled “local.settings.json for local development without Aspire”

When running your Azure Functions project outside of Aspire (for example, in isolation with func start), the Functions runtime reads connection information from local.settings.json. You can populate this file with the same environment variable names that Aspire injects:

JSON — local.settings.json
{
"IsEncrypted": false,
"Values": {
"AzureWebJobsStorage": "UseDevelopmentStorage=true",
"FUNCTIONS_WORKER_RUNTIME": "dotnet-isolated",
"BLOBS_BLOBENDPOINT": "https://127.0.0.1:10000/devstoreaccount1",
"SERVICEBUS_FULLYQUALIFIEDNAMESPACE": "sb://my-namespace.servicebus.windows.net/"
}
}

When Aspire runs your project, the values in local.settings.json are overridden by the Aspire-injected environment variables, so the same trigger and binding configuration works in both modes.