Deploying a .NET 5 Web API and a SQL Database in Azure

Azure provide us with the possibility to deploy an application in a cloud environment in an easy and safe way. In this article, I explain how to deploy a Web API using .NET 5 and an MS SQL Server Database in a cloud environment in Azure. For that, I’m using the BookStore solution which is a project that was created for a series about how to create a web application from scratch (you can find the first article about that by clicking here).

Creating an Azure Account

To deploy the application in Azure, first, we need to have an Azure account and an Azure subscription. You can create a free Azure account on the Azure website, click here to access the page (note that it’s necessary to inform a valid credit card).

In the Azure Portal we will make use of three Azure services:

  • App Service — to deploy the Web API
  • SQL Server
  • SQL Database

Creating a Resource Group

“A resource group is a container that holds related resources for an Azure solution. The resource group can include all the resources for the solution, or only those resources that you want to manage as a group. You decide how you want to allocate resources to resource groups based on what makes the most sense for your organization. Generally, add resources that share the same lifecycle to the same resource group so you can easily deploy, update, and delete them as a group.” (Microsoft Documentation)

Once your account is created, you need to create a resource group. When you access the Resource groups page in the Azure portal, you will notice that it’s necessary to create a subscription, you can select the free trial:

Now that you have an Azure account and a subscription, let’s create a Resource Group:

If this option is not visible for you, it’s also possible to search for that using the top bar:

Inform your subscription, a name for the resource group, and select a region, and click in “Review + create”:

TIP: If you want to have lower latency, select the region that is near to your region. In this example, I selected “(US) East US 2” because it’s the cheapest one at this moment. If you want to know which region is the cheapest, there is a nice website from the community where you can check this information, click here to access the page (note that this is not an official Microsoft website).

After that click in “Create”, and you will be redirected to the Resource groups page, and you can see your resource group in the list.

Creating an APP Service

Now we need to create an “App Services” to deploy the Web API. With this service we don’t need to worry about the infrastructure where the application will run, Azure will take care of that for us. You can click on the “App Services” option or search for it in the search bar in the Azure portal:

“Azure App Service lets you create apps faster with a one-of-a kind cloud service to quickly and easily create enterprise-ready web and mobile apps for any platform or device and deploy them on a scalable and reliable cloud infrastructure.” (Azure Microsoft)

Then click in “Create”:

Now it’s necessary to configure your app service, by selecting your subscription, your resource group, informing a name for the service, the tech stack, operating system, region and the plan.

Note that I’m using the plan “Free F1”, because this is a demo. If you want to deploy your application at a production level, I recommend you to use a “Production” plan. You can change that by clicking on this option:

Then click in “Review + create”, confirm the information and click in “Create”. Can take some time until your service is created, you can check the status in the notifications (the bell icon in the top bar):

Once the service is deployed you will see the notification:

Creating the SQL Server

This project uses a SQL Server database, so for that, we must use create a SQL Sever and a Database. If you are using another database, for example, MySQL, then instead of the “SQL Server” service, you need the “Azure Database for MySQL servers”.

First search for SQL Servers:

Then click on “Create” and create a new SQL Server selecting the resource group and adding the server name, user name and password:

Now that we have the SQL Server in Azure, we can also create the Database. Go to “SQL Servers”, and click in “Create database”:

Informe the name of your database, select a “Compute + storage” and click in “Review + create”, review the info and click in Create:

Now the SQL Server and the Database were deployed:

Configuring the firewall

In the SQL Server, go to firewall configurations:

Add your client IP or the IP’s that you want to allow to connect in the SQL Server:

Also, make sure to select this option (you don’t want to forget to do it — believe me):

Now go to the database and get the connection string by clicking in the option “Show database connection strings”:

Copy the connection string that will be displayed, and save it. We will need to add it to our API.

Configuring the Database Connection

Open your solution in Visual Studio, and go to the “appsettings.json” file, and add the connection string with the Azure information and add your database password in the connection string:

Now we can run the migrations from Visual Studio, with the command “update-database”:

Note that you can also the database connection string in Azure Portal. To do that, go to the API’s App Services, and search for the “Configuration” option:

Add a new connection string:

Add the connection string name, the value, and the type and click in ok:

The click in Save:

Querying data in Azure Portal

It’s also possible to run SQL queries in Azure Portal. To do that, go to the SQL Server, select your database, click on the option “SQL databases”:

Select your database and then search by “Query editor”:

Add the database user name and password and click in “Ok”:

Now you can see your database in Azure Portal and also run SQL queries:

Deploying the WEB API

Let’s now deploy the API. To do that, right-click in the API project and click in “Publish”:

Select the “Azure” option and click in Next:

Select “Azure app Service (Windows)” and click in Next:

If you are not logged in yet in your Azure account in Visual Studio, you need to log in by clicking here:

Select your app service and click in next:

We can skip the API Management step for now (you can create later also in Azure portal):

Here is a brief explanation about what is an API Management:

“API Management (APIM) is a way to create consistent and modern API gateways for existing back-end services.

API Management helps organizations publish APIs to external, partner, and internal developers to unlock the potential of their data and services. Businesses everywhere are looking to extend their operations as a digital platform, creating new channels, finding new customers and driving deeper engagement with existing ones. API Management provides the core competencies to ensure a successful API program through developer engagement, business insights, analytics, security, and protection. You can use Azure API Management to take any backend and launch a full-fledged API program based on it.” (Microsoft Documentation)

Select the option “Publish (generates pubxml file) and click in “Finish”:

Click in “Publish” to deploy the API:

If the application is deployed with success, a new page will be opened in your browser. You can also get the URL in the Azure portal in “App Services” > select your app service > URL:

This application is using Swagger, so we can access it like this:

And now you must see the Web API:

Let’s now add a Category:

And also let’s make a request to get all the categories:

As we can see, the Web API is running in Azure, and also the database. We can also check the data by running an SQL query in Azure Portal:

Conclusion

The Azure portal has a great interface where we can easily find the services that we want, and as we saw in this article, we can deploy a Web API and a SQL Server Database in a few minutes, and we can do that without spending much time with configuration related to infrastructure. Of course, it’s necessary to spend time studying Azure and the services that Azure provides, and how we can configure the services, firewall, network, and so on. But thinking in a long term, having an application running in Azure will bring many advantages and will save a lot of time and save a significant amount of money.

With Azure, you can easily configure your application to scale when necessary, you have access to monitoring services, disaster recovery, security, and many other services. We can also make use of a “Pay-as-you-go” subscription where as the name implies, we only need to pay for what is used.

You can check the code of this project here:

Thanks for reading!

.NET Full-Stack Developer | C# | .NET | .NET CORE | ASP.NET MVC | Unit Test | Angular