Publish ASP.NET Core 2.0 Application On Azure With SQL Services

This article will demonstrate how we can deploy or publish an ASP.NET CORE 2.0 application on Azure. As we all know, once we create an application we have to deploy it somewhere --  it can be IIS, the Cloud or somewhere else. Publishing or deploying an application is a bit difficult and we get so many issues when we don’t know the exact way to deploy the application.

So I  will let you know each step to publish an ASP.NET CORE 2.0 on Azure. We have already created an application using Razor Pages, we will use the same application to deploy here.

To know more about Razor pages and its fundamentals, you can check 

  1. Razor page in Asp.Net Core
  2. CRUD operations with Asp.Net Core Razor Pages [We will use this application for deployment]

So, let's jump to the practical implementation part. We have a project ready for deployment. To publish an Asp.Net Core 2 application, just right click on the project from Solution Explorer and click to “Publish” option.  We can see clearly that we are using the “RazorPagesExample” application for publishing what we have created in the previous article and this article is using SQL server database and tables for CRUD operations. 

ASP.NET Core

Once we choose “Publish” option it will open a window where we have different options available to publish an app on Azure or another host like IIS, folder etc. Here we have to choose “Microsoft Azure App Service”  with “Create New” option and without doing anything else just click to “Publish” Option.

ASP.NET Core

The next window will show the different options to setup a hosting environment and its services.  Under the “Hosting” section, we can define our application name “App Name”, this name will be used to access the application from the browser, define our subscription plan under which we would like to publish an application and resource group for SQL server.

Note

Should we have Microsoft Subscription Plan with Azure Credits for deployment.?

Note

If we have a database as a part of the application then move to Service Part to create SQL server and database.

ASP.NET Core

As we are publishing an application which is performing a CRUD operation, so for that we are required to set up the SQL database as well. So, we will directly move to Services option just below Hosting to create SQL related services like SQL server, SQL database, and connection string etc.

As we can see, with below screen inside the Services option we have “SQL Database” with Plus [+] sign inside the Resource Type. To create new SQL service, click on PLUS [+] sign.

ASP.NET Core

Once we click on PLUS sign button, it will open a popup window which is responsible for setting up SQL server and database.  So, first, we will set up a new SQL Server on Azure. To create new SQL Server on Azure, click on New button as the following screen shown.

ASP.NET Core

From the “Configure SQL Server” window, we have to define SQL Server name, its username and password to access it from SQL Server Management Studio or to use it with Connection String.

ASP.NET Core

After setting up a new SQL server click OK. It will go back to “Configure SQL Database” screen where we can now set up our SQL database. So here we can define our database name as well as connection string name as we have done with the following screen and click to OK.

Note

Previously it was taking the default database and connection string name. Here we have changed,

ASP.NET Core

So, now we have configured three things, first the plan under which our application will publish on Azure. The second one is the SQL server where we have configured our SQL database and tables and the last one is a SQL database under the same SQL server as the following screen shows.

ASP.NET Core

Now, it's time to deploy our application with the above configuration, so to do that just click to “CREATE” button and it will start deploying our application on Azure as following screen shows.

ASP.NET Core

Here we can see that “Azure Successfully Configured” and Visual Studio is going to publish the application on Azure. 

ASP.NET Core

It will take few minutes or seconds as per the network connection and speed to deploy an application on Azure and it gives us a message as follows which says that the app has deployed successfully on the following location.

ASP.NET Core

As we all know, this application is using SQL database and its tables to perform CRUD operations. We have already created a database at the time of configuring SQL services but tables are pending to create. To create required SQL tables under “TestDB” database, we have to access SQL server and its database and manually run the tables scripts.

Now, we have to open “Microsoft SQL Server Management Studio” from your system and pass our credentials as we have configured previously for SQL server. Be sure, when entering the server name, that the SQL server name should be in the following format.

Servername.database.secure.windows.net,1433

ASP.NET Core

Oh, we have a new error, which means we are not able to access defined SQL server due to a firewall rule with this IP address. 

ASP.NET Core

To resolve this error, we have to add this IP address under firewall rules.  So, open Microsoft Azure portal and select SQL server which we have configured previously.

ASP.NET Core

As the following screen shows, under the SQL Server, we have an option “Firewall/Virtual Networks”. Here, we can see the following screen that we don’t have any IP address defined right now. You have to click to “Add Client IP” to add client IP address under firewall rules and click to save it.

ASP.NET Core

After adding “Client IP Address”, it will come under the Firewall rules. Now, we can access this server from SQL Server Management Studio.

ASP.NET Core

If we will try once more to log in with SQL Server Management Studio we are able to log in. Now select “TestDB” database which we have configured with this publish and right click and open a new query window. Here, we have to run our SQL scripts like create a new table, insert new record etc.

ASP.NET Core

So, finally, we have configured everything including database as well. Now its time to access our deployed application. If we access our application with the below URL [this is only for my case, yours can be defined your own name] we will see the application as below. 

https://razorpagesexample.azurewebsites.net/Product

ASP.NET Core

Conclusion

So, today we have learned how to publish an application created in Asp.Net Core 2.0 on Azure.

I hope this post will help you. Please put your feedback using the comments which helps me to improve myself for next post. If you have any doubts please ask your doubts or query in the comment section and if you like this post, please share it with your friends. Thanks...

G
M
T
 
Text-to-speech function is limited to 200 characters