7

How to Detect If Your Application is Running in Azure App Service

 9 months ago
source link: https://edi.wang/post/2023/12/14/how-to-detect-if-your-application-is-running-in-azure-app-service
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

How to Detect If Your Application is Running in Azure App Service

Original

Edi Wang

12/14/2023, 8:34:41 AM

Problem


An application needs to know if itself is running in Azure App Service, so that it can apply special logic and optimization for Azure. 

Solution


When deploying an application to Azure App Service, the platform automatically assigns a set of environment variables that can be accessed by the application code.

To view the complete list of environment variables applicable to your application at runtime, you can navigate to the "Advanced Tools" blade within your App Service Instance.

img-f98c0164-08c7-44e9-8621-43db4cc98e40.png

Then, go to "Environment" page. You will see the entire list here. Among them, are a few environment variables that are suitable for identify if the application is running on Azure App Service.

  • WEBSITE_SKU
  • WEBSITE_SITE_NAME
  • WEBSITE_HOSTNAME
img-47b044e9-8456-4072-b22c-63f256fed3df.png

Now, in our code, we can try to get the values of those environment variables, example code for ASP.NET Core Razor Page is like:

<tr>
    <td>@SharedLocalizer["Azure App Service"]</td>
    <td class="text-muted">
        WEBSITE_SKU : @Environment.GetEnvironmentVariable("WEBSITE_SKU") <br />
        WEBSITE_SITE_NAME: @Environment.GetEnvironmentVariable("WEBSITE_SITE_NAME") <br />
        WEBSITE_HOSTNAME : @Environment.GetEnvironmentVariable("WEBSITE_HOSTNAME") <br />
        REGION_NAME  : @Environment.GetEnvironmentVariable("REGION_NAME") <br />
    </td>
</tr>

Example result:

img-6655443e-a6f3-43b3-a7cb-8abab61ea1db.png

This work is licensed under a Creative Commons Attribution-ShareAlike 4.0 International License



Recommend

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK