Welcome to today’s post.
In todays blog I will show you how to publish a .NET Core web application to Azure.
As part of the deployment, I will also show how to deploy a SQL database to Azure in the same deployment task.
What I will be doing is to deploy a .NET Core web application to an Azure App Service, which will be under an App Service Plan. I will also deploy a SQL database to an Azure SQL database.
The web application uses the data within the SQL database and establishes a connectivity through a connection string that is defined within the application settings file that is created with the web application in Visual Studio.
In the first section, I will show how to configure the application deployment parameters in Visual Studio Publish Wizard to publish the application to Azure App Services.
Azure App Service Deployment Parameters
After your .NET Core web application has reached a stage where it is ready for deployment, it is ready for publication to Azure.
Under your project in Visual Studio right-click and select Publish…
The first part of the publish wizard will display the initial parameters needed to deploy the web application to Azure. This includes the site URL and the resource group. The as initial page is shown below:
Before we can publish the application, the following resources in Azure will need to exist:
- A resource group (to be created or an existing one selected to contain the web application (App Service).
- An Azure SQL Server (to contain the SQL Server Database)
- An Azure SQL Database (SQL Server, My SQL, Cosmos DB etc.)
Next, click on the Configure link. The following dialog opens:
In the above Connection details, you will notice the following:
Your web site URL will be of the format:
http://[web app name].azurewebsites.net
Publish credentials will show in the user name and password parameter fields. Notice the password is totally obscured for security reasons.
To obtain the publish credentials, you will need to do the following:
Login to Azure.
Go to all resources.
Go to App Service.
Click on Get Publish Profile.
The publish profile in the format [Web App Name].PublishSettings will be downloaded to your local machine.
Open the file. The publishing credentials will be shown in the keys username and userPWD.
Note: Ensure when your source is checked into a repository (TFS, Git etc), that a git ignore includes extensions .pubxml.user. (Normally Visual Studio 2017 will include this file extension by default.)
Azure SQL Database Deployment Parameters
In the next part of the Settings tab, we will be able to configure the database connection settings.
When you click on the Settings tab, the following form with configuration parameters will show:
You will see Databases and an Entity Framework Migrations setting.
Expand the Databases and Entity Framework Migrations chevrons. You will see the following:
The first database connection string corresponds to the connection string during run time in Azure. It will initially contain a local host connection string. To obtain the connection string for the database in Azure, do the following.
Login to Azure.
Go to all resources.
Open the SQL Database.
The database overview shows:
Hover over the Connection Strings property.
Select Copy to Clipboard.
Return to the publish dialog settings in Visual Studio.
Paste in the connection string into the edit field for Databases.
The second database connection string corresponds to the connection string during run time for EF Migrations of your database in Azure. It will initially contain a local host connection string which will not work in the Azure App Services environment. Paste in the connection string to this edit field.
Note: The Azure database connection string will be of the format:
Server=tcp:[SQL Server Name].database.windows.net,1433;
Initial Catalog=[Your SQL Database Name];
Persist Security Info=False;
User ID=[sql server login name];
Password=[password];
MultipleActiveResultSets=False;
Encrypt=True;
TrustServerCertificate=False;
Connection Timeout=30;
Now save the settings.
In the publish screen hit the Publish button and watch your application deploy.
When deployed, the default launch settings will open your Azure web app . It will be of the form:
http://[appname].azurewebsites.net
where [web-application-name] is the name of the .NET Core web application in Visual Studio.
In the above overview, you have seen how to manually deploy a .NET Core web application with a SQL database to Azure using the Visual Studio Publish Wizard. We also saw how to obtain the credentials from Azure for both the application and database and set them within the publish wizard.
In future posts I will show how to publish web applications to Azure from Git source code repository and using an Azure DevOps pipeline. Stay tuned!
That’s all for todays post.
Thanks for reading this post and hope it has been helpful.
Andrew Halil is a blogger, author and software developer with expertise of many areas in the information technology industry including full-stack web and native cloud based development, test driven development and Devops.