Deploy .NET API To Azure App Service Through Azure DevOps

In this article, we are going to learn how to implement continuous integration and continuous deployment to a .NET API through Azure DevOps.

This is part of a series of videos,

  1. Implementing Clean Architecture On .NET
  2. Implementing Unit And Integration Tests On .NET With xUnit
  3. Deploy .NET API to Azure App Service through Azure DevOps


1. Create Azure SQL Database

Click on the sidebar icon and go to Resource groups.

Click on Create.

Type a name for the resource group, then click on Review + create / Create.

For this example, I used Demo-StoreCleanArchitecture for the resource group name.

Now, from the sidebar, go to SQL databases.

Click on Create.

Type the database name and create a server if you haven't one.

Let's create a server. Follow these steps:

  • Type a unique server name
  • From Authentication method, select Use SQL authentication
  • Type Server admin login and password, then click on Ok

From Compute + storage, click on Configure database.

As you can see, the default service tier has a lot of estimated cost per month (231.28 USD). Let's select other service tier cheaper.

Select Basic, this is for less demanding workloads. And now the cost is too low (4.99 USD).

Then, click on Review + create.

You can see a summary of all the features you are going to implement in this resource. Click on Create.

Let's wait until the database is created. Then click on Set server firewall.

Let's add a rule to allow connections to this database from any IP address (just for testing, we can restrict to connect only from our IP address). Then click on Save.

To test if we are allowed to connect to this database. Open SQL Server Management Studio. From Object Explorer, click on Connect Database Engine.

Enter the credentials and click on Connect.

As you can see, the connection was successful.

2. Create Azure App Service

From the sidebar, select App Services.

Click on Create.

Follow these steps:

  • Select the Resource group
  • Name: specify a unique name
  • Runtime stack: .NET 6 (LTS)
  • Operating System: Linux

If it's a new App Service Plan, we can change the Sku and size, clicking on Change size.

Select F1 plan from Dev/Test and click on Apply.

Then, click on Review + create.

You can see a summary of all the features of our Web App. Click on Create.

After the Web App is created. Go to the resource, in Settings section, click on Configuration.

Go to Connection strings, click on New connection string.

In the dialog, enter DefaultConnection in Name, select SQLAzure in Type and add the connection string in Value. Then click on Ok.

Server=<server_name>;Database=<database_name>;User ID=<username>;Password=<password>

As you can see, the connection string was added. Click on Save.

3. Create a project on Azure DevOps and create Build and Release pipelines

3.1. Update .NET application

Before going to Azure DevOps Services, first, we need to do a change in the .NET application. In the Infrastructure project, in DependencyInjection.cs file, add the following below services.AddScoped<IProductRepository, ProductRepository>(),

var serviceProvider = services.BuildServiceProvider();
try {
    var dbContext = serviceProvider.GetRequiredService < StoreContext > ();
} catch {}

This will apply all the pending migrations to the database.
Commit the changes.

3.2. Create Build Pipeline

Go to Azure DevOps Services. At the right, click on New project.

Specify a project name. Select the visibility you want. Click on Create.

Go to Pipelines, click on Pipelines.

Click on Create Pipeline.

Click on Use the classic editor.

Select GitHub. Type a connection name, then click on Authorize using OAuth.

Sign in with the credentials from the account you have the repository.

Once you are authorized to connect to your GitHub account. You can link your repository.

Search the repository and click on Select.

Once the repository and branch are selected, click on Continue.

Select the ASP.NET Core template and click on Apply.

Set the pipeline name as CI and the agent specification in windows-2022.

You can change the name for the agent job as well. Click on the plus icon from Agent Job.

Select Use .NET Core and click on Add.

This will add a new task to the final. Move it to the top and set version as 6.x.

Click on the Publish task. Uncheck Publish web projects, click on Path to project(s) / Unlink.

Type **/src/Store.WebApi/*.csproj in Path to project(s).

Click on Save & queue.

You can specify a save comment, then click on Save and run.

Now, the job is running. Click on the job.

Let's wait until the job is finalized. As you can see it was successful.

Go to the list of pipelines, click on the three dots to the right. edit.

Go to Triggers section. Enable continuous integration and click on Save.

3.3. Create Release Pipeline

From Pipelines section, click on Releases.

Click on New pipeline.

Select Azure App Service deployment template and click on Apply.

Follow these steps,

  1. Change pipeline name to Deploy to Azure
  2. Change stage name to Production
  3. Click on Add an artifact

It should be selected Build as Source type. In Source, select CI and click on Add.

Once the Artifact is added, click on the button with the lightning icon.

Enable Continuous deployment trigger.

Go to the Production stage and click on 1 job, 1 task.

Select one of your Azure subscriptions and click on Authorize.

Then, select Web App on Linux in App type. Select the app service name you created in section 2 and in startup command type dotnet Store.WebApi.dll. Click on Run on Agent.

Select windows-2022 in Agent Specification.

Scroll down and in Artifact download you can see it's selected _CI artifact and there is a folder named drop with file. Click on Save.

Click on Ok.

Finally. Lest's create our first release. Click on Create release.

Click on Create.

Let's wait until the pipeline finalizes.

As you can see the Production stage was succeeded.

Let's test the API. Since we used the F1 App Service plan. We have to wait some seconds for the first request of the day.

3.4. Create a dashboard

We are going to create a dashboard to display the statistics for build and release pipelines.
Go to Overview section and click on Dashboards.

Click on Add a widget.

Select Deployment status and drop it to the dashboard.

Configure the widget.

Select the build pipeline, release pipeline, and last column. Click on Save.

Add Build Health Details widget.

Configure the widget.

In Definition, select the build pipeline (CI).

Add Build History widget.

Configure the widget.

Select the build pipeline (CI).

Add Build Health Overview widget.

In the configuration, select the build pipeline (CI).

Once you added the four widgets, click on Done Editing at the top of the dashboard.

This is the final result. You can add all the widgets you want, but for this example, these ones are enough.

You can find the source code here.

Thanks for reading

Thank you very much for reading, I hope you found this article interesting and may be useful in the future. If you have any questions or ideas that you need to discuss, it will be a pleasure to be able to collaborate and exchange knowledge together.