Application deployment
.NET Core Azure Azure App Services C# CICD Continuous Deployment Continuous Integration DevOps PaaS Swagger Top Visual Studio Web API

Deploying a .NET Core Web API to an Azure App Service

Welcome to today’s post.

I will discuss how to deploy changes to Web API applications from Visual Studio into Azure.

In previous posts I showed how to deploy ASP.NET .NET Core applications and Angular SPA applications to Azure App Service. In today’s post I will deploy a .NET Core Web API application to Azure using the Web publishing wizard within Visual Studio. Like normal web sites, web API applications are hosted and deployed to the same folder within a web server. The Azure App Service contains a subfolder folder \home\site\wwwroot where our application is deployed and hosted.

Once our application is deployed into the root folder of the Azure App Service, it keeps the same folder structure and configuration files that you have within the application’s project folder your local development environment. The only difference will be the settings within your configuration file that determine how your deployed application in the Azure App Service cloud environment integrates with other external services like cloud databases and other cloud services. These settings can be deployed with your project build configuration settings with a Production build context and the appSettings.production.json configuration file.

Following deployment, the API service will be accessible from an index.html landing page, and if we have configured Swagger for our API application, then the landing page will display the Swagger UI documentation displaying the application’s API service methods.

Let me give a quick review of what a Web API method is. In the example below we have a HTTP GET API method within our API controller.

[HttpGet("api/[controller]/AllBooks")]
public async Task<List<BookViewModel>> AllBooks()
{
   List<BookViewModel> bvm = new List<BookViewModel>();
   var books = await _db.Books.ToListAsync();
   return books;
}

After building and deploying our API to our localhost or even to IIS Express, we test it using POSTMAN:

The local IIS URL of our API HTTP GET method is:

http://localhost/BookLoan.Catalog.API/api/Book/AllBooks

Following this test, we proceed to configure our API project to include an additional publishing profile to Azure App Service.

In our project, right-click and select Publish..

After selecting New Profile.. the following screen will show where we select App Service as our publishing target:

Choose Select Existing.

Select Advanced ….

You are asked to enter your Azure credentials.

Sign-in to Azure.

Select (or search for) the App Service from your subscription and resource group.

The publish summary page now shows.

In the publish summary page select Configure.

Validate the SCM connection to Azure is valid.

In the publish summary page select Settings.

Enter the Azure SQL database connection that your Web API will be using.

To obtain the connection string open the Azure SQL resource in the portal and obtain the value as shown:

The Azure SQL Server database connection is of the form:

Server=tcp:[database server name].database.windows.net,1433;Initial Catalog=[database name]; Persist Security Info=False;User ID=[user id]; Password=[pwd]; MultipleActiveResultSets=False; Encrypt=True;TrustServerCertificate=False;Connection Timeout=30;

Note: The connection string will NOT include the password, which you will need to provide.

Next save the setting and publish.

After publishing completes the API will be shown within the Swagger UI with the API method we discussed earlier:

If our API controller or either of our API methods has the [Authorize] attribute then we will see a padlock security icon to its right. For more details on securing web API methods refer to one of my previous posts on JWT authentication.

For details on how to create an App Service for a Web API refer to my post Deploying Projects from GitHub Repo into Azure App Services.

As we have seen, there are a few different ways (and even more ways) to deploy our .NET Core Web API into Azure:

  1. Using the Web publishing wizard within Visual Studio
  2. Using deployment from a local Git repository
  3. Using a Docket image from a Docker Hub repository

That’s all for this post.

I hope you found this post useful and informative.

Social media & sharing icons powered by UltimatelySocial