Skip to content
Docs Try Aspire
Docs Try

Set up Azure Functions in the AppHost

Azure Functions logo

This article is the reference for the Aspire Azure Functions Hosting integration. It enumerates the AppHost APIs — with examples for both AppHost.cs and apphost.ts — 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.

The Aspire Azure Functions integration has the following project constraints for .NET Functions projects:

To start building an Aspire app that uses Azure Functions, install the 📦 Aspire.Hosting.Azure.Functions NuGet package:

Terminal
aspire add azure-functions

Learn more about aspire add in the command reference.

Or, choose a manual installation approach:

C# — AppHost.cs
#:package Aspire.Hosting.Azure.Functions@*
XML — AppHost.csproj
<PackageReference Include="Aspire.Hosting.Azure.Functions" Version="*" />

The Azure Functions hosting integration models Azure Functions projects as the AzureFunctionsProjectResource type. Call addAzureFunctionsProject 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:

C# — AppHost.cs
var builder = DistributedApplication.CreateBuilder(args);
var functions = builder.AddAzureFunctionsProject<Projects.ExampleFunctions>("functions")
.WithExternalHttpEndpoints();
builder.AddProject<Projects.ExampleProject>()
.WithReference(functions)
.WaitFor(functions);
// After adding all resources, run the app...
builder.Build().Run();

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:

C# — AppHost.cs
var builder = DistributedApplication.CreateBuilder(args);
var functions = builder.AddAzureFunctionsProject("functions", "../MyFunctions/MyFunctions.csproj")
.WithExternalHttpEndpoints();
builder.AddProject<Projects.ExampleProject>()
.WithReference(functions)
.WaitFor(functions);
// After adding all resources, run the app...
builder.Build().Run();
  1. 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.

  2. The functions resource can be referenced by other project resources. The WithReference (or withReference) method configures a connection named "functions" in the consuming project.

  3. If the Functions project exposes an HTTP trigger and you call WithExternalHttpEndpoints (or withExternalHttpEndpoints), 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 (or withHostStorage) on the Azure Functions resource:

C# — AppHost.cs
var builder = DistributedApplication.CreateBuilder(args);
var storage = builder.AddAzureStorage("storage")
.RunAsEmulator();
var functions = builder.AddAzureFunctionsProject<Projects.ExampleFunctions>("functions")
.WithHostStorage(storage);
builder.AddProject<Projects.ExampleProject>()
.WithReference(functions)
.WaitFor(functions);
// After adding all resources, run the app...
builder.Build().Run();

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.

To connect Azure Functions triggers and bindings to other Azure resources, chain WithReference (or withReference) on the Azure Functions project resource:

C# — AppHost.cs
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();

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.

To make HTTP triggers publicly accessible when deployed, call WithExternalHttpEndpoints (or withExternalHttpEndpoints) on the Azure Functions resource:

C# — AppHost.cs
var builder = DistributedApplication.CreateBuilder(args);
var functions = builder.AddAzureFunctionsProject<Projects.ExampleFunctions>("functions")
.WithExternalHttpEndpoints();
builder.Build().Run();

The Aspire.Hosting.Azure.Functions package includes support for Azure Durable Task Scheduler, which provides a managed backend for Durable Functions orchestrations. Use AddDurableTaskScheduler to add a scheduler resource and AddTaskHub to associate a task hub with it.

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:

C# — AppHost.cs
#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 ASPIREDURABLETASK001

In the preceding example:

  • AddDurableTaskScheduler (or addDurableTaskScheduler) adds a DurableTaskSchedulerResource to the app model.
  • RunAsEmulator (or runAsEmulator) configures the scheduler to use the local Durable Task emulator for development and testing.
  • AddTaskHub (or addTaskHub) creates a DurableTaskHubResource associated with the scheduler.
  • WithReference(taskHub) (or withReference(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_NAMES environment variable on the emulator container listing the associated task hub names.

To connect to an already-deployed Durable Task Scheduler instance instead of provisioning a new one, use RunAsExisting. The overload accepts either a plain string connection string or an IResourceBuilder<ParameterResource> for securely supplying the value at runtime:

C# — AppHost.cs
#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 ASPIREDURABLETASK001

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.

For the full reference of how consuming apps and Functions code read the Aspire-injected connection information, see Azure Functions runtime configuration.