|
| 1 | +# Aspire.Hosting.Azure.AppService library |
| 2 | + |
| 3 | +Provides extension methods and resource definitions for a .NET Aspire AppHost to configure Azure App Service for the compute resources (like project). |
| 4 | + |
| 5 | +## Getting started |
| 6 | + |
| 7 | +### Prerequisites |
| 8 | + |
| 9 | +- Azure subscription (requires Owner access to the target subscription for role assignments) |
| 10 | + |
| 11 | +### Install the package |
| 12 | + |
| 13 | +In your AppHost project, install the .NET Aspire Azure App Service Hosting library with [NuGet](https://www.nuget.org): |
| 14 | + |
| 15 | +```dotnetcli |
| 16 | +dotnet add package Aspire.Hosting.Azure.AppService |
| 17 | +``` |
| 18 | + |
| 19 | +## Usage example |
| 20 | + |
| 21 | +In the _AppHost.cs_ file of `AppHost`, add an Azure App Service Environment and publish your project as an Azure App Service Web App: |
| 22 | + |
| 23 | +```csharp |
| 24 | +var builder = DistributedApplication.CreateBuilder(args); |
| 25 | + |
| 26 | +var appServiceEnvironment = builder.AddAzureAppServiceEnvironment("env"); |
| 27 | + |
| 28 | +builder.AddProject<Projects.MyWebApp>("webapp") |
| 29 | + .WithExternalHttpEndpoints() |
| 30 | + .PublishAsAzureAppServiceWebsite((infrastructure, website) => |
| 31 | + { |
| 32 | + // Customize the App Service Web App appsettings |
| 33 | + website.SiteConfig.IsWebSocketsEnabled = true; |
| 34 | + }); |
| 35 | +``` |
| 36 | + |
| 37 | +## Azure App Service constraints |
| 38 | + |
| 39 | +When deploying to Azure App Service, the following constraints apply: |
| 40 | + |
| 41 | +- **External endpoints only**: App Service only supports external endpoints. All endpoints must be configured using `WithExternalHttpEndpoints()`. |
| 42 | +- **HTTP/HTTPS only**: Only HTTP and HTTPS endpoints are supported. Other protocols are not supported. |
| 43 | +- **Single endpoint**: App Service supports only a single target port. Resources with multiple external endpoints with different target ports are not supported. The default target port is 8000, which can be overridden using the `WithHttpEndpoint` extension method. |
| 44 | + |
| 45 | +### Publishing compute resources to Azure App Service |
| 46 | + |
| 47 | +The `PublishAsAzureAppServiceWebsite` extension method is used to configure a compute resource (such as a project) to be published as an Azure App Service Web App when deploying to Azure. This method allows you to customize the App Service Web App configuration using the Azure Provisioning SDK. |
| 48 | + |
| 49 | +```csharp |
| 50 | +builder.AddProject<Projects.MyApi>("api") |
| 51 | + .WithHttpEndpoint(targetPort: 8080) |
| 52 | + .WithExternalHttpEndpoints() |
| 53 | + .WithHealthProbe("/health") |
| 54 | + .WithArgs("--environment", "Production") |
| 55 | + .PublishAsAzureAppServiceWebsite((infrastructure, website) => |
| 56 | + { |
| 57 | + // Customize the App Service Web App appsettings |
| 58 | + website.SiteConfig.IsWebSocketsEnabled = true; |
| 59 | + website.SiteConfig.MinTlsVersion = SupportedTlsVersions.Tls1_2; |
| 60 | + }); |
| 61 | +``` |
| 62 | + |
| 63 | +### Adding an Azure App Service Environment |
| 64 | + |
| 65 | +The Azure App Service Environment resource creates the underlying infrastructure needed to host your applications, including: |
| 66 | + |
| 67 | +- An Azure App Service Plan (default SKU: P0v3) |
| 68 | +- An Azure Container Registry for storing container images |
| 69 | +- A managed identity for accessing the container registry |
| 70 | +- Optionally, the Aspire Dashboard as an Azure App Service Web App |
| 71 | +- Optionally, Application Insights for monitoring and telemetry |
| 72 | + |
| 73 | +```csharp |
| 74 | +var appServiceEnvironment = builder.AddAzureAppServiceEnvironment("env"); |
| 75 | +``` |
| 76 | + |
| 77 | +By default, the Aspire Dashboard is included in the App Service Environment. To disable the dashboard, use the `WithDashboard` extension method: |
| 78 | + |
| 79 | +```csharp |
| 80 | +var appServiceEnvironment = builder.AddAzureAppServiceEnvironment("env") |
| 81 | + .WithDashboard(enable: false); |
| 82 | +``` |
| 83 | + |
| 84 | +### Enabling Application Insights |
| 85 | + |
| 86 | +Application Insights can be enabled for the App Service Environment using the `WithAzureApplicationInsights` extension method. A different location can be specified for Application Insights using the optional location parameter: |
| 87 | + |
| 88 | +```csharp |
| 89 | +var appServiceEnvironment = builder.AddAzureAppServiceEnvironment("env") |
| 90 | + .WithAzureApplicationInsights(); |
| 91 | +``` |
| 92 | + |
| 93 | +### Customizing the App Service Plan SKU |
| 94 | + |
| 95 | +The default SKU for the App Service Plan is P0v3. This can be changed using the `ConfigureInfrastructure` extension method: |
| 96 | + |
| 97 | +```csharp |
| 98 | +var appServiceEnvironment = builder.AddAzureAppServiceEnvironment("env") |
| 99 | + .ConfigureInfrastructure((infra) => |
| 100 | + { |
| 101 | + var plan = infra.GetProvisionableResources().OfType<AppServicePlan>().Single(); |
| 102 | + plan.Sku = new AppServiceSkuDescription |
| 103 | + { |
| 104 | + Name = "P2V3", |
| 105 | + Tier = "Premium" |
| 106 | + }; |
| 107 | + }); |
| 108 | +``` |
| 109 | + |
| 110 | +## Additional documentation |
| 111 | + |
| 112 | +* https://learn.microsoft.com/azure/app-service/ |
| 113 | +* https://github.com/dotnet/aspire/tree/main/src/Components/README.md |
| 114 | + |
| 115 | +## Feedback & contributing |
| 116 | + |
| 117 | +https://github.com/dotnet/aspire |
0 commit comments