Events
17 Mar, 23 - 21 Mar, 23
Join the meetup series to build scalable AI solutions based on real-world use cases with fellow developers and experts.
Register nowThis browser is no longer supported.
Upgrade to Microsoft Edge to take advantage of the latest features, security updates, and technical support.
In this tutorial, you learn to configure an ASP.NET Core app with a SQL Server Database for deployment to Azure. .NET Aspire provides multiple SQL Server integration configurations that provision different database services in Azure. You'll learn how to:
Note
This document focuses specifically on .NET Aspire configurations to provision and deploy SQL Server resources in Azure. Visit the Azure Container Apps deployment tutorial to learn more about the full .NET Aspire deployment process.
To work with .NET Aspire, you need the following installed locally:
For more information, see .NET Aspire setup and tooling, and .NET Aspire SDK.
Visual Studio creates a new ASP.NET Core solution that is structured to use .NET Aspire. The solution consists of the following projects:
.NET Aspire provides two built-in configuration options to streamline SQL Server deployment on Azure:
Add the appropriate .NET Aspire integration to the AspireSQL.AppHost project for your desired hosting service.
Add the 📦 Aspire.Hosting.Azure.Sql NuGet package to the AspireSQL.AppHost project:
dotnet add package Aspire.Hosting.Azure.Sql
Configure the AspireSQL.AppHost project for your desired SQL database service.
Replace the contents of the Program.cs file in the AspireSQL.AppHost project with the following code:
var builder = DistributedApplication.CreateBuilder(args);
var apiService = builder.AddProject<Projects.AspireSql_ApiService>("apiservice");
// Provisions an Azure SQL Database when published
var sqlServer = builder.AddAzureSqlServer("sqlserver")
.AddDatabase("sqldb");
builder.AddProject<Projects.AspireSql_Web>("webfrontend")
.WithExternalHttpEndpoints()
.WithReference(apiService)
.WaitFor(apiService);
builder.Build().Run();
The preceding code adds a SQL Server Container resource to your app and configures a connection to a database called sqldata
. The PublishAsAzureSqlDatabase
method ensures that tools such as the Azure Developer CLI or Visual Studio create an Azure SQL Database resource during the deployment process.
Tools such as the Azure Developer CLI (azd
) support .NET Aspire SQL Server integration configurations to streamline deployments. azd
consumes these settings and provisions properly configured resources for you.
Open a new terminal window and cd
into the directory of your .NET Aspire solution.
Execute the azd init
command to initialize your project with azd
, which will inspect the local directory structure and determine the type of app.
azd init
For more information on the azd init
command, see azd init.
Select Use code in the current directory when azd
prompts you with two app initialization options.
? How do you want to initialize your app? [Use arrows to move, type to filter]
> Use code in the current directory
Select a template
After scanning the directory, azd
prompts you to confirm that it found the correct .NET Aspire AppHost project. Select the Confirm and continue initializing my app option.
Detected services:
.NET (Aspire)
Detected in: D:\source\repos\AspireSample\AspireSample.AppHost\AspireSample.AppHost.csproj
azd will generate the files necessary to host your app on Azure using Azure Container Apps.
? Select an option [Use arrows to move, type to filter]
> Confirm and continue initializing my app
Cancel and exit
Enter an environment name, which is used to name provisioned resources in Azure and managing different environments such as dev
and prod
.
Generating files to run your app on Azure:
(✓) Done: Generating ./azure.yaml
(✓) Done: Generating ./next-steps.md
SUCCESS: Your app is ready for the cloud!
You can provision and deploy your app to Azure by running the azd up command in this directory. For more information on configuring your app, see ./next-steps.md
azd
generates a number of files and places them into the working directory. These files are:
azd
what the current active environment is.Once an azd
template is initialized, the provisioning and deployment process can be executed as a single command from the AppHost project directory using azd up:
azd up
Select the subscription you'd like to deploy to from the list of available options:
Select an Azure Subscription to use: [Use arrows to move, type to filter]
1. SampleSubscription01 (xxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxx)
2. SamepleSubscription02 (xxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxx)
Select the desired Azure location to use from the list of available options:
Select an Azure location to use: [Use arrows to move, type to filter]
42. (US) Central US (centralus)
43. (US) East US (eastus)
> 44. (US) East US 2 (eastus2)
46. (US) North Central US (northcentralus)
47. (US) South Central US (southcentralus)
After you make your selections, azd
executes the provisioning and deployment process.
By default, a service can only be reached from inside the Azure Container Apps environment it is running in. Selecting a service here will also allow it to be reached from the Internet.
? Select which services to expose to the Internet webfrontend
? Select an Azure Subscription to use: 1. <YOUR SUBSCRIPTION>
? Select an Azure location to use: 1. <YOUR LOCATION>
Packaging services (azd package)
Provisioning Azure resources (azd provision)
Provisioning Azure resources can take some time.
Subscription: <YOUR SUBSCRIPTION>
Location: <YOUR LOCATION>
You can view detailed progress in the Azure Portal:
<LINK TO DEPLOYMENT>
(✓) Done: Resource group: <YOUR RESOURCE GROUP>
(✓) Done: Container Registry: <ID>
(✓) Done: Log Analytics workspace: <ID>
(✓) Done: Container Apps Environment: <ID>
SUCCESS: Your application was provisioned in Azure in 1 minute 13 seconds.
You can view the resources created under the resource group <YOUR RESOURCE GROUP> in Azure Portal:
<LINK TO RESOURCE GROUP OVERVIEW>
Deploying services (azd deploy)
(✓) Done: Deploying service apiservice
- Endpoint: <YOUR UNIQUE apiservice APP>.azurecontainerapps.io/
(✓) Done: Deploying service webfrontend
- Endpoint: <YOUR UNIQUE webfrontend APP>.azurecontainerapps.io/
Aspire Dashboard: <LINK TO DEPLOYED .NET ASPIRE DASHBOARD>
SUCCESS: Your up workflow to provision and deploy to Azure completed in 3 minutes 50 seconds.
The azd up
command acts as wrapper for the following individual azd
commands to provision and deploy your resources in a single step:
azd package
: The app projects and their dependencies are packaged into containers.azd provision
: The Azure resources the app will need are provisioned.azd deploy
: The projects are pushed as containers into an Azure Container Registry instance, and then used to create new revisions of Azure Container Apps in which the code will be hosted.When the azd up
stages complete, your app will be available on Azure, and you can open the Azure portal to explore the resources. azd
also outputs URLs to access the deployed apps directly.
The deployment process provisioned an Azure SQL Database resource due to the .AppHost configuration you provided.
Run the following Azure CLI command to delete the resource group when you no longer need the Azure resources you created. Deleting the resource group also deletes the resources contained inside of it.
az group delete --name <your-resource-group-name>
For more information, see Clean up resources in Azure.
.NET Aspire feedback
.NET Aspire is an open source project. Select a link to provide feedback:
Events
17 Mar, 23 - 21 Mar, 23
Join the meetup series to build scalable AI solutions based on real-world use cases with fellow developers and experts.
Register nowTraining
Module
Customize a .NET Aspire app to use existing Azure resources - Training
In this module, you'll learn how to move backing services for your Azure-hosted .NET Aspire app from containers into native Azure services.
Certification
Microsoft Certified: Azure Database Administrator Associate - Certifications
Administer an SQL Server database infrastructure for cloud, on-premises and hybrid relational databases using the Microsoft PaaS relational database offerings.
Documentation
Deploy a .NET Aspire project using the Azure Developer CLI - .NET Aspire
Learn how to use `azd` to deploy .NET Aspire projects.
Deploy .NET Aspire projects to Azure Container Apps using Visual Studio - .NET Aspire
Learn how to use Bicep, the Azure CLI, and Azure Developer CLI to deploy .NET Aspire projects to Azure using Visual Studio.
.NET Aspire manifest format for deployment tool builders - .NET Aspire
Learn about the .NET Aspire manifest format in this comprehensive deployment tool builder guide.