Set up Azure Functions in the AppHost
This article is the reference for the Aspire Azure Functions Hosting integration. It enumerates the AppHost APIs — with examples for AppHost.cs, apphost.mts, and apphost.go — that you use to model an Azure Functions project resource in your AppHost project.
If you are new to the Azure Functions integration, start with the Get started with the Azure Functions integration guide. For how your Functions code reads the connection information this page sets up, see Azure Functions runtime configuration.
Project constraints
Section titled “Project constraints”The Aspire Azure Functions integration has the following project constraints for .NET Functions projects:
- You must target .NET 8.0 or later.
- You must use a .NET 9 SDK or later.
- It currently only supports .NET workers with the isolated worker model.
- Requires the following NuGet packages:
- 📦 Microsoft.Azure.Functions.Worker: Use the
FunctionsApplicationBuilder. - 📦 Microsoft.Azure.Functions.Worker.Sdk: Adds support for
aspire runandaspire deploy. - 📦 Microsoft.Azure.Functions.Worker.Extensions.Http.AspNetCore: Adds HTTP trigger-supporting APIs.
- 📦 Microsoft.Azure.Functions.Worker: Use the
Installation
Section titled “Installation”To start building an Aspire app that uses Azure Functions, install the 📦 Aspire.Hosting.Azure.Functions NuGet package:
aspire add azure-functionsLearn more about aspire add in the command reference.
This updates your aspire.config.json with the Azure Functions hosting integration package:
{ "packages": { "Aspire.Hosting.Azure.Functions": "17.0.0" }}aspire add azure-functionsLearn more about aspire add in the command reference.
Or, choose a manual installation approach:
#:package Aspire.Hosting.Azure.Functions@*<PackageReference Include="Aspire.Hosting.Azure.Functions" Version="*" />aspire add azure-functionsLearn more about aspire add in the command reference.
This updates your aspire.config.json with the Azure Functions hosting integration package:
{ "packages": { "Aspire.Hosting.Azure.Functions": "17.0.0" }}Add Azure Functions resource
Section titled “Add Azure Functions resource”The Azure Functions hosting integration models Azure Functions projects as the AzureFunctionsProjectResource type. Call AddAzureFunctionsProjectaddAzureFunctionsProjectAddAzureFunctionsProject on the builder instance to add an Azure Functions project resource to the app model.
There are two ways to add an Azure Functions project in C#, depending on whether the Functions project is referenced by the AppHost project. The TypeScript AppHost always uses the file path approach.
Add a referenced Functions project (C# only)
Section titled “Add a referenced Functions project (C# only)”If the Azure Functions project is referenced in your C# AppHost project, use the generic overload of AddAzureFunctionsProject:
var builder = DistributedApplication.CreateBuilder(args);
var functions = builder.AddAzureFunctionsProject<Projects.ExampleFunctions>("functions") .WithExternalHttpEndpoints();
builder.AddDotnetProject("exampleproject", "../ExampleProject/ExampleProject.csproj") .WithReference(functions) .WaitFor(functions);
// After adding all resources, run the app...builder.Build().Run();Add a Functions project by file path
Section titled “Add a Functions project by file path”If the Azure Functions project is not referenced in your AppHost project (or you are using a TypeScript AppHost), specify the path to the project file:
import { createBuilder } from './.aspire/modules/aspire.mjs';
const builder = await createBuilder();
const functions = await builder.addAzureFunctionsProject( "functions", "../MyFunctions/MyFunctions.csproj");await functions.withExternalHttpEndpoints();
await builder.addNodeApp("api", "./api", "index.js") .withReference(functions) .waitFor(functions);
// After adding all resources, run the app...var builder = DistributedApplication.CreateBuilder(args);
var functions = builder.AddAzureFunctionsProject("functions", "../MyFunctions/MyFunctions.csproj") .WithExternalHttpEndpoints();
builder.AddDotnetProject("exampleproject", "../ExampleProject/ExampleProject.csproj") .WithReference(functions) .WaitFor(functions);
// After adding all resources, run the app...builder.Build().Run();builder, err := aspire.CreateBuilder()if err != nil { log.Fatal(aspire.FormatError(err))}
functions := builder.AddAzureFunctionsProject("functions", "../MyFunctions/MyFunctions.csproj"). WithExternalHttpEndpoints()
api := builder.AddGoApp("api", "./api"). WithReference(functions). WaitFor(functions)
// After adding all resources, run the app...-
The path to the Functions project file is relative to the AppHost project directory. If the path is not absolute, it is resolved relative to the AppHost directory.
-
The
functionsresource can be referenced by other project resources. TheWithReferencewithReferenceWithReferencemethod configures a connection named"functions"in the consuming project. -
If the Functions project exposes an HTTP trigger and you call
WithExternalHttpEndpointswithExternalHttpEndpointsWithExternalHttpEndpoints, the trigger endpoint is publicly accessible when deployed.
Add Azure Functions resource with host storage
Section titled “Add Azure Functions resource with host storage”Azure Functions requires an Azure Storage account as host storage for checkpoints, leases, and other internal state. By default, Aspire creates an implicit host storage resource automatically. To use a specific storage account, call WithHostStorage(IResourceBuilder<AzureStorageResource> storage)withHostStorage(storage: AzureStorageResource)WithHostStorage on the Azure Functions resource:
import { createBuilder } from './.aspire/modules/aspire.mjs';
const builder = await createBuilder();
const storage = await builder.addAzureStorage("storage");await storage.runAsEmulator();
const functions = await builder.addAzureFunctionsProject( "functions", "../MyFunctions/MyFunctions.csproj");await functions.withHostStorage(storage);
// After adding all resources, run the app...var builder = DistributedApplication.CreateBuilder(args);
var storage = builder.AddAzureStorage("storage") .RunAsEmulator();
var functions = builder.AddAzureFunctionsProject<Projects.ExampleFunctions>("functions") .WithHostStorage(storage);
builder.AddDotnetProject("exampleproject", "../ExampleProject/ExampleProject.csproj") .WithReference(functions) .WaitFor(functions);
// After adding all resources, run the app...builder.Build().Run();builder, err := aspire.CreateBuilder()if err != nil { log.Fatal(aspire.FormatError(err))}
storage := builder.AddAzureStorage("storage"). RunAsEmulator()
functions := builder.AddAzureFunctionsProject("functions", "../MyFunctions/MyFunctions.csproj"). WithHostStorage(storage)
// After adding all resources, run the app...The preceding code relies on the 📦 Aspire.Hosting.Azure.Storage NuGet package to add an Azure Storage resource running as an emulator. The storage resource is passed to WithHostStorage, setting the host storage to the emulated account.
Reference resources in Azure Functions
Section titled “Reference resources in Azure Functions”To connect Azure Functions triggers and bindings to other Azure resources, chain WithReference (or withReference) on the Azure Functions project resource:
import { createBuilder } from './.aspire/modules/aspire.mjs';
const builder = await createBuilder();
const storage = await builder.addAzureStorage("storage");await storage.runAsEmulator();const blobs = await storage.addBlobs("blobs");
const functions = await builder.addAzureFunctionsProject( "functions", "../MyFunctions/MyFunctions.csproj");await functions.withHostStorage(storage);await functions.withReference(blobs);
// After adding all resources, run the app...var builder = DistributedApplication.CreateBuilder(args);
var storage = builder.AddAzureStorage("storage").RunAsEmulator();var blobs = storage.AddBlobs("blobs");
builder.AddAzureFunctionsProject<Projects.ExampleFunctions>("functions") .WithHostStorage(storage) .WithReference(blobs);
builder.Build().Run();builder, err := aspire.CreateBuilder()if err != nil { log.Fatal(aspire.FormatError(err))}
storage := builder.AddAzureStorage("storage"). RunAsEmulator()blobs := storage.AddBlobs("blobs")
functions := builder.AddAzureFunctionsProject("functions", "../MyFunctions/MyFunctions.csproj"). WithHostStorage(storage). WithReference(blobs)
// After adding all resources, run the app...The connection information required to connect to the blobs resource is automatically injected into the Azure Functions project, enabling the project to define a BlobTrigger that uses the blobs resource. For how your function code reads the injected connection information, see Azure Functions runtime configuration.
Add external HTTP endpoints
Section titled “Add external HTTP endpoints”To make HTTP triggers publicly accessible when deployed, call WithExternalHttpEndpoints (or withExternalHttpEndpoints) on the Azure Functions resource:
import { createBuilder } from './.aspire/modules/aspire.mjs';
const builder = await createBuilder();
const functions = await builder.addAzureFunctionsProject( "functions", "../MyFunctions/MyFunctions.csproj");await functions.withExternalHttpEndpoints();
// After adding all resources, run the app...var builder = DistributedApplication.CreateBuilder(args);
var functions = builder.AddAzureFunctionsProject<Projects.ExampleFunctions>("functions") .WithExternalHttpEndpoints();
builder.Build().Run();builder, err := aspire.CreateBuilder()if err != nil { log.Fatal(aspire.FormatError(err))}
functions := builder.AddAzureFunctionsProject("functions", "../MyFunctions/MyFunctions.csproj"). WithExternalHttpEndpoints()
// After adding all resources, run the app...TypeScript and JavaScript Functions apps
Section titled “TypeScript and JavaScript Functions apps”The directory-based Functions app APIs — AddAzureFunctionsApp, AzureFunctionsAppResource, AzureFunctionsLanguage, and the app-specific WithHostStorage and WithReference overloads — are experimental pending API review and may change or be removed. In a C# AppHost, suppress diagnostic ASPIREAZUREFUNCTIONS001 to opt in, for example with #pragma warning disable ASPIREAZUREFUNCTIONS001. For suppression options, see ASPIREAZUREFUNCTIONS001. The existing .NET project APIs, such as AddAzureFunctionsProject, are unaffected.
Use AddAzureFunctionsApp (or addAzureFunctionsApp) to model a TypeScript or JavaScript Azure Functions app that does not have a .NET project file. Directory-based apps run on the Azure Functions Node language worker — Aspire sets FUNCTIONS_WORKER_RUNTIME to node for you:
import { AzureFunctionsLanguage, createBuilder } from './.aspire/modules/aspire.mjs';
const builder = await createBuilder();
const storage = await builder.addAzureStorage("storage");await storage.runAsEmulator();const blob = await storage.addBlobs("blob");
const functions = await builder.addAzureFunctionsApp( "functions", "../functions", AzureFunctionsLanguage.TypeScript);await functions.withReference(blob);
// After adding all resources, run the app...using Aspire.Hosting.Azure;
#pragma warning disable ASPIREAZUREFUNCTIONS001
var builder = DistributedApplication.CreateBuilder(args);
var storage = builder.AddAzureStorage("storage").RunAsEmulator();var blob = storage.AddBlobs("blob");
builder.AddAzureFunctionsApp("functions", "../functions", AzureFunctionsLanguage.TypeScript) .WithReference(blob);
builder.Build().Run();
#pragma warning restore ASPIREAZUREFUNCTIONS001package main
import ( "log"
"apphost/modules/aspire")
func main() { builder, err := aspire.CreateBuilder() if err != nil { log.Fatal(aspire.FormatError(err)) }
storage := builder.AddAzureStorage("storage").RunAsEmulator() blob := storage.AddBlobs("blob") builder.AddAzureFunctionsApp("functions", "../functions", aspire.AzureFunctionsLanguageTypeScript). WithReference(blob)
app, err := builder.Build() if err != nil { log.Fatal(aspire.FormatError(err)) } if err := app.Run(); err != nil { log.Fatal(aspire.FormatError(err)) }}-
The path to the Functions app directory is relative to the AppHost project directory.
-
AzureFunctionsLanguagehas two values:TypeScriptandJavaScript. In Go, useAzureFunctionsLanguageTypeScriptorAzureFunctionsLanguageJavaScript. Both run on the Node language worker. -
TypeScript apps are launched through
npm run start -- --port <port>, so the standard Azure Functions Core Tools-generatedprestartscript can build the app before the host starts. Thestartscript should resolve Azure Functions Core Tools from the app’s local npm dependencies, for example by depending on theazure-functions-core-toolspackage indevDependencies. JavaScript apps are launched directly withfunc host start --port <port>and requirefuncto be available onPATH. -
As with .NET Functions projects, Aspire provisions an implicit Azure Storage account for host storage by default. Call
WithHostStorage(orwithHostStorage) to use a specific storage account instead — see Add Azure Functions resource with host storage.
A directory-based Functions app’s package.json should include scripts similar to the standard Azure Functions TypeScript template:
{ "scripts": { "build": "tsc", "prestart": "npm run build", "start": "func start" }, "devDependencies": { "azure-functions-core-tools": "^4.0.0", "typescript": "^5.0.0" }}Debug TypeScript and JavaScript Functions apps
Section titled “Debug TypeScript and JavaScript Functions apps”Use the Aspire extension for VS Code to start your AppHost with debugging. Set breakpoints in your JavaScript or TypeScript source and invoke the function to stop at those breakpoints. Aspire attaches the VS Code JavaScript debugger to the Functions Node worker.
For TypeScript, enable source maps and build your app before invoking the function. By default, the debugger looks for compiled JavaScript under dist/**/*.js in the Functions app directory. If your build uses another directory, set outFiles under debuggers.azure-functions-node in your Aspire launch configuration. For example, add the following property to the type: "aspire" configuration in .vscode/launch.json, adjusting the path for your app:
{ "debuggers": { "azure-functions-node": { "outFiles": ["${workspaceFolder}/functions/build/**/*.js"] } }}Durable Task Scheduler integration
Section titled “Durable Task Scheduler integration”The Durable Task scheduler and task hub APIs are experimental in this release. They are marked with the [Experimental("ASPIREDURABLETASK001")] attribute and may change before becoming generally available (GA).
To use these APIs without compiler warnings, suppress the ASPIREDURABLETASK001 diagnostic. For suppression options, see ASPIREDURABLETASK001.
The Aspire.Hosting.Azure.Functions package includes support for Azure Durable Task Scheduler, which provides a managed backend for Durable Functions orchestrations. Use AddDurableTaskScheduleraddDurableTaskSchedulerAddDurableTaskScheduler to add a scheduler resource and AddTaskHubaddTaskHubAddTaskHub to associate a task hub with it.
Add a Durable Task scheduler and task hub
Section titled “Add a Durable Task scheduler and task hub”Call AddDurableTaskScheduler on the builder instance to add a scheduler resource, then call AddTaskHub to create a task hub and pass its reference to the Azure Functions project:
import { createBuilder } from './.aspire/modules/aspire.mjs';
const builder = await createBuilder();
const storage = await builder.addAzureStorage("storage");await storage.runAsEmulator();
const scheduler = await builder.addDurableTaskScheduler("scheduler");await scheduler.runAsEmulator();
const taskHub = await scheduler.addTaskHub("taskhub");
const functions = await builder.addAzureFunctionsProject( "functions", "../MyFunctions/MyFunctions.csproj");await functions.withHostStorage(storage);await functions.withReference(taskHub);
// After adding all resources, run the app...#pragma warning disable ASPIREDURABLETASK001
var builder = DistributedApplication.CreateBuilder(args);
var storage = builder.AddAzureStorage("storage") .RunAsEmulator();
var scheduler = builder.AddDurableTaskScheduler("scheduler") .RunAsEmulator();
var taskHub = scheduler.AddTaskHub("taskhub");
builder.AddAzureFunctionsProject<Projects.ExampleFunctions>("functions") .WithHostStorage(storage) .WithReference(taskHub);
builder.Build().Run();
#pragma warning restore ASPIREDURABLETASK001builder, err := aspire.CreateBuilder()if err != nil { log.Fatal(aspire.FormatError(err))}
storage := builder.AddAzureStorage("storage"). RunAsEmulator()
scheduler := builder.AddDurableTaskScheduler("scheduler"). RunAsEmulator()
taskHub := scheduler.AddTaskHub("taskhub")
functions := builder.AddAzureFunctionsProject("functions", "../MyFunctions/MyFunctions.csproj"). WithHostStorage(storage). WithReference(taskHub)
// After adding all resources, run the app...In the preceding example:
AddDurableTaskScheduler(oraddDurableTaskScheduler) adds aDurableTaskSchedulerResourceto the app model.RunAsEmulatorrunAsEmulatorRunAsEmulatorconfigures the scheduler to use the local Durable Task emulator for development and testing.AddTaskHub(oraddTaskHub) creates aDurableTaskHubResourceassociated with the scheduler.WithReference(taskHub)(orwithReference(taskHub)) injects the task hub connection information into the Azure Functions project.
When using RunAsEmulator(), Aspire starts a local container running the DTS emulator and automatically provides:
- A Scheduler Dashboard URL on the scheduler resource.
- A Task Hub Dashboard URL on each task hub resource.
- A
DTS_TASK_HUB_NAMESenvironment variable on the emulator container listing the associated task hub names.
Durable Task Scheduler emulator image
- Durable Task Scheduler Emulator container imageMicrosoft Artifact Registrymcr.microsoft.com/dts/dts-emulator:latest
Tags reflect the latest defaults on the microsoft/aspire main branch, and may be newer than the version pinned by the package you install.
Use an existing scheduler
Section titled “Use an existing scheduler”To connect to an already-deployed Durable Task Scheduler instance instead of provisioning a new one, use RunAsExistingrunAsExistingRunAsExisting. The overload accepts either a plain string connection string or an IResourceBuilder<ParameterResource> for securely supplying the value at runtime:
#pragma warning disable ASPIREDURABLETASK001
var builder = DistributedApplication.CreateBuilder(args);
var connectionString = builder.AddParameter("dts-connection-string", secret: true);
var scheduler = builder.AddDurableTaskScheduler("scheduler") .RunAsExisting(connectionString);
var taskHubNameParam = builder.AddParameter("taskhub-name");var taskHub = scheduler.AddTaskHub("taskhub").WithTaskHubName(taskHubNameParam);
builder.AddAzureFunctionsProject<Projects.ExampleFunctions>("functions") .WithReference(taskHub);
builder.Build().Run();
#pragma warning restore ASPIREDURABLETASK001Durable Task Scheduler deployment support (manifest and deployment) is not yet included. Only local development with the emulator or an existing scheduler instance is currently supported.
Deployment
Section titled “Deployment”Deployment to Azure Container Apps (ACA) is supported using the SDK container publish function in Microsoft.Azure.Functions.Worker.Sdk. When deploying to Azure Container Apps, KEDA-based auto-scaling rules are automatically configured for your functions.
For more information, see Azure Functions and Aspire integration.
Deploying TypeScript and JavaScript Functions apps
Section titled “Deploying TypeScript and JavaScript Functions apps”When a directory-based Functions app doesn’t already contain a Dockerfile, aspire publish generates one that uses the official Azure Functions Node image, copies the app to /home/site/wwwroot, installs npm dependencies, and (for TypeScript apps) runs npm run build. After the build step, Aspire runs npm prune --omit=dev so the deployed container keeps only production dependencies.
If the Functions app directory already contains a Dockerfile, Aspire uses it as-is instead of generating one — use this to select a different Azure Functions Node image, pin a Node version, or add project-specific build steps.
When deployed to Azure Container Apps, directory-based Functions resources are published with the container app kind set to functionapp and the container’s HTTP port set to 80, matching the shape of the Azure Functions Node host image.
If your subscription restricts storage network access, configure the Container Apps environment and storage with private networking. Some policies also require disabling anonymous blob access. See the Azure Virtual Network integration and customizing Azure resources.
Connection properties
Section titled “Connection properties”For the full reference of how consuming apps and Functions code read the Aspire-injected connection information, see Azure Functions runtime configuration.