Salta ai contenuti

Azure Functions integration

Questi contenuti non sono ancora disponibili nella tua lingua.

Choose a development environment
Azure Functions logo 🧪 Preview

Azure Functions is a serverless compute service that lets you run event-triggered code without having to explicitly provision or manage infrastructure. The Aspire Azure Functions integration enables you to model Azure Functions projects as part of your distributed application.

It’s expected that you’ve installed the required Azure tooling:

The Aspire Azure Functions integration has several key supported scenarios. This section outlines the scenarios and provides details related to the implementation of each approach.

The following table lists the supported triggers for Azure Functions in the Aspire integration:

TriggerAttributeDetails
Azure Event Hubs triggerEventHubTrigger📦 Aspire.Hosting.Azure.EventHubs
Azure Service Bus triggerServiceBusTrigger📦 Aspire.Hosting.Azure.ServiceBus
Azure Storage Blobs triggerBlobTrigger📦 Aspire.Hosting.Azure.Storage
Azure Storage Queues triggerQueueTrigger📦 Aspire.Hosting.Azure.Storage
Azure CosmosDB triggerCosmosDbTrigger📦 Aspire.Hosting.Azure.CosmosDB
HTTP triggerHttpTriggerSupported without any additional resource dependencies.
Timer triggerTimerTriggerSupported without any additional resource dependencies—relies on implicit host storage.

Currently, deployment is supported only to containers on Azure Container Apps (ACA) using the SDK container publish function in Microsoft.Azure.Functions.Worker.Sdk. This deployment methodology doesn’t currently support KEDA-based autoscaling.

To make HTTP triggers publicly accessible, call the WithExternalHttpEndpoints API on the AzureFunctionsProjectResource. For more information, see Add Azure Functions resource.

The Aspire Azure Functions integration has the following project constraints:

If you encounter issues with the Azure Functions project, such as:

There is no Functions runtime available that matches the version specified in the project

In Visual Studio, try checking for an update on the Azure Functions tooling. Open the Options dialog, navigate to Projects and Solutions, and then select Azure Functions. Select the Check for updates button to ensure you have the latest version of the Azure Functions tooling:

Visual Studio: Options / Projects and Solutions / Azure Functions.

The Aspire Azure Functions hosting integration models Azure Functions projects as the AzureFunctionsProjectResource type. To access this type and APIs for expressing them within your AppHost project, install the 📦 Aspire.Hosting.Azure.Functions NuGet package:

Aspire CLI — Aggiungi pacchetto Aspire.Hosting.Azure.Functions
aspire add azure-functions

La CLI Aspire è interattiva; seleziona il risultato corretto quando richiesto:

Aspire CLI — Output di esempio
Select an integration to add:
> azure-functions (Aspire.Hosting.Azure.Functions)
> Other results listed as selectable options...

In your AppHost project, call AddAzureFunctionsProject on the builder instance to add an Azure Functions resource:

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...

When Aspire adds an Azure Functions project resource the AppHost, as shown in the preceding example, the functions resource can be referenced by other project resources. The WithReference method configures a connection in the ExampleProject named "functions". If the Azure Resource was deployed and it exposed an HTTP trigger, its endpoint would be external due to the call to WithExternalHttpEndpoints.

Add Azure Functions resource with host storage

Section titled “Add Azure Functions resource with host storage”

If you want to modify the default host storage account that the Azure Functions host uses, call the WithHostStorage method on the Azure Functions project 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...

The preceding code relies on the 📦 Aspire.Hosting.Azure.Storage NuGet package to add an Azure Storage resource that runs as an emulator. The storage resource is then passed to the WithHostStorage API, explicitly setting the host storage to the emulated resource.

To reference other Azure resources in an Azure Functions project, chain a call to WithReference on the Azure Functions project resource and provide the resource to reference:

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 preceding code adds an Azure Storage resource to the AppHost and references it in the Azure Functions project. The blobs resource is added to the storage resource and then referenced by the functions resource. The connection information required to connect to the blobs resource is automatically injected into the Azure Functions project and enables the project to define a BlobTrigger that relies on blobs resource.

Domande & RisposteCollaboraCommunityDiscuteGuarda