Migrate ASP.NET Web App to Azure App Service

This post has been republished via RSS; it originally appeared at: New blog articles in Microsoft Community Hub.

In the previous post, we've discussed what Azure App Service is and why we need to move our existing ASP.NET applications to the cloud, especially Azure App Service. Throughout this post, I'm going to walk through how we migrate our existing ASP.NET application to the Azure App Service instance with minimal code changes.

 

What needs to consider for changes?

 

I'm more than confident to migrate our existing ASP.NET application to Azure App Service with no changes because that's what Azure App Service is designed for. However, there are a few things to change due to the cloud-native characteristics of Azure App Service. Then, what are they?

 

web.config to application settings

 

web.config is a file that contains a collection of configurations about how the ASP.NET web application runs in a given way. For example, a typical web.config file looks like the following:

 

 

<?xml version="1.0"?> <configuration> ... <appSettings> <add key="key1" value="value1"/> <add key="key2" value="value2"/> </appSettings> ... </configuration>

 

 

Both key1 and key2 under the appSettings node are the application settings values used in the ASP.NET application. We can still use this web.config file on the Azure App Service instance. However, relying on the web.config file results in losing huge opportunities to integrate with other Azure services such as monitoring, storage, security, etc. Therefore, for our ASP.NET application to become more cloud-native, we need to give up using the web.config file and move all the appSettings values to the application settings configuration on the Azure App Service instance.

 

Here's the screenshot of how both key1 and key2 are configured as application settings values.

 

deploying-aspnet-web-app-to-azure-app-service-01.png

 

File system to Blob Storage

 

If our ASP.NET application runs directly on our on-prem web server, there are high chances to use the local file system – read or write files for many reasons. But, when we migrate our application to Azure App Service, we can't do this any longer. Of course, the App Service instance has file storage, but it's not for our file transactions. Therefore, we SHOULD rewrite the existing file transaction logic to consider the Azure Storage service. Instead of accessing the local file system, send and receive the files to Azure Blob Storage.

 

Here's the sample code snippet to take a look at:

 

 

// existing file read logic from the local file system string filepath = @"C:\\temp\mydocument.txt"; string content = default; using (Stream file = System.IO.File.OpenRead(filepath)) using (StreamReader reader = new StreamReader(file)) { content = await reader.ReadToEndAsync(); } // new file download and read logic from Azure Blob Storage string connstring = "<blob_storage_connectionstring>"; string containerName = "containerName"; BlobServiceClient service = new BlobServiceClient(connstring); BlobContainerClient container = await service.CreateBlobContainerAsync(containerName); BlobClient blob = container.GetBlobClient("mydocument.txt"); using (Stream stream = new MemoryStream()) { await blob.DownloadToAsync(stream); using (StreamReader reader = new StreamReader(stream)) { content = await reader.ReadToEndAsync(); } }

 

 

Logging or monitoring to Application Insights

 

Capturing what has happened while handling requests and responses is important. There are many ways to capture the log data, but storing the data in a plain text file or database is the most common approach. However, if we deploy our web application to Azure App Service, we SHOULD avoid the local file system due to the reason stated above. Logging and monitoring can be incredibly easy because it's already integrated with the Azure App Service. Azure Application Insights takes care of this logging and monitoring. All we need is the instrumentation key or connection string of the Azure Application Insights instance, and we store it in the application settings configuration.

 

Here's the screenshot of Azure Application Insights integrated with Azure App Service:

 

deploying-aspnet-web-app-to-azure-app-service-02.png

 

These three points are the most common ones we must consider for changes during the application migration process.

 

How to deploy ASP.NET applications directly from Visual Studio?

 

Once we complete adjusting the codebase to be cloud-friendly, it's time for deployment! Visual Studio offers a very convenient way to publish ASP.NET applications to Azure App Service. After clicking a mouse a few times, Visual Studio initiates our ASP.NET application deployment process. Let's take a look at how we can publish the application with only a few mouse clicks.

 

  1. Right-click the web application project

     

    deploying-aspnet-web-app-to-azure-app-service-03.png

     

  2. Choose the deployment target for Azure

     

    deploying-aspnet-web-app-to-azure-app-service-04.png

     

  3. Choose Azure App Service as a specific target

     

    deploying-aspnet-web-app-to-azure-app-service-05.png

     

  4. If necessary, you can directly provision Azure App Service instance within Visual Studio

     

    deploying-aspnet-web-app-to-azure-app-service-06.png

     

  5. Choose the Azure App Service instance

     

    deploying-aspnet-web-app-to-azure-app-service-07.png

     

  6. ASP.NET application has now been published

     

    deploying-aspnet-web-app-to-azure-app-service-08.png

     

 

So far, we've walked through 1) what to consider for migration and 2) how Visual Studio improves the overall application deployment process and velocities. With minimal code changes, our existing ASP.NET application will easily be migrated to Azure App Service.

 

It's your turn!

 

Once we come to this section, you are ready for migration! Please take a look at the following links for further reading as your action items. If you have any questions around this, please don't hesitate to reach out to us at @justinchronicle or @codemillmatt.

 

 

Now it's your turn! Show us what you complete!

Leave a Reply

Your email address will not be published. Required fields are marked *

*

This site uses Akismet to reduce spam. Learn how your comment data is processed.