Deploy and configure Azure App Service on Linux

By Mirek on (tags: AppService, asp.net Core, azure, categories: azure, architecture, code, web)

Today I’ll show you how easy it is to deploy and configure an ASP.NET Core web application on Azure AppService with linux os.

Let’s start by creating simple ASP.NET Core web application using a default template from Visual Studio. So just choose Create new project then select Web and ASP.NET Core Web Application as the project template. All defaults, no fancy stuff.

Now Let’s assume our application has some custom settings. Let’s open the appsettings.json file and add them there

{
   "Logging": {
     "LogLevel": {
       "Default": "Information",
       "Microsoft.AspNetCore": "Warning"
     }
   },
   "AllowedHosts": "*",
   "AppSpecialCredentials": {
     "UserName": "",
     "Password": ""
   } }

AppSpecialCredentials are the settings that we later on want to configure and override in our running Azure App Service app . So to check if the settings are properly set lets fetch and display them on home page. Of course it’s not recommended to display app credentials on a page, but here we just want to demonstrate that settings are properly configured and retreived on Azure App Service.

Let’s go to Index page and open Index.chtml.cs code behind. Modify the IndexModel class as follows

public class IndexModel : PageModel
{
     private readonly ILogger<IndexModel> _logger;
     private readonly IConfiguration _configuration;

     public string? UserName { get; private set; }
     public string? Password { get; private set; }

     public IndexModel(ILogger<IndexModel> logger, IConfiguration configuration)
     {
         _logger = logger;
         _configuration = configuration;
     }

     public void OnGet()
     {
         var section = _configuration.GetSection("AppSpecialCredentials");
         UserName = section["UserName"];
         Password = section["Password"];
     } }

First in the constructor we get the configuration service injected, then in the OnGet method we read the app setting section and fetch the credentials. Then lets modify the Index.cshtml page and display the credentials

@page
@model IndexModel
@{
     ViewData["Title"] = "Home page"; } <div>
     <h1>This is a test Azure Linux app.</h1>
     <p>User settings:</p>
     <p>User: @Model.UserName</p>
     <p>Password: @Model.Password</p> </div>

That’s it. Our web app will display what it finds in the configuration.

Now let’s deploy our app to the Azure.

I use Azure CLI and the guideline from here. Nothing more simple that that.

Assuming you have active Azure subscription and the Azure CLI installed on your machine follow these steps:

  1. Open Command Prompt or Powershell
  2. Navigate to your project root folder (the project file location)
  3. Login to you Azure account by running:
    az login
  4. Then create new webapp running command:
    az webapp up --sku F1 --name my-azure-test-app --os-type linux --location westeurope
    That will create resource group and app service in Free tier and with linux as operating system.
    When the process is done you should get the summary json like this
    {
           "URL": "http://my-azure-test-app.azurewebsites.net",
           "appserviceplan": "your.name_plan",
           "location": "westeurope",
           "name": "my-azure-test-app",
           "os": "Linux",
           "resourcegroup": "your.name_resourcegroup",
           "runtime_version": "dotnetcore|6.0",
           "runtime_version_detected": "6.0",
           "sku": "FREE",
           "src_path": "C:\\temp\\my-azure-test-app"
    }
  5. Now you can run the app by navigating web browser to
    https://my-azure-test-app.azurewebsites.net

You should see home page:

AzureAppServiceLinux_01

Meaning our app has no settings configured. Default empty are not overriden.

Ok, now let’s see how to configure our App Service settings. You can go straight to the Azure portal and do this in the App Service Configuration editor, but I’ll show you how to do it with Azure CLI.

Having the Powershell still open and running, execute following command replacing your app name and your plan name with proper values:

az webapp config appsettings set --resource-group your.name_resourcegroup --name my-azure-test-app --settings AppSpecialCredentials__UserName="user999" AppSpecialCredentials__Password="12345"

That will set the user name to user999 and password to 12345. Note how the appsettings.json tree structure is being flatten here with use of ‘__’ (double underscore). This delimiter is specific for Linux based deployment as for Windows based deployments you will use ‘:’ (colon). For more information about application settings in App Service refer to the documentation.

Now we can test our app and check if the application settings were properly overriden

AzureAppServiceLinux_02

After all remember to clean up and delete the app service and all its related resources if you dont need it anymore. To do this you must delete the whole resource group by calling:

az group delete --name your.name_resourcegroup

Thanks!