skip to Main Content

We want to create an azure function in c# that retrieve the list of azure web app contained in the subscription (basically we want to call dynamically, for each webapp, the same API endpoint changing the subdomain of the api).

It’s possible with c# retrieve the list of the web app contained in the same azure function subscriptions?

Usually we connect to the master database, we query the sys.databases to collect the dbname and understand the webapp names. But we are searching for a smartest way.

2

Answers


  1. Yes, one easy way is to use HttpClient and send a request to Azure Rest API:

    GET https://management.azure.com/subscriptions/{subscriptionId}/providers/Microsoft.Web/sites?api-version=2022-03-01
    

    https://learn.microsoft.com/en-us/rest/api/appservice/web-apps/list

    PS: you first need to acquire an authentication token.

    https://www.youtube.com/watch?v=6b1J03fDnOg&t=329s

    Login or Signup to reply.
  2. If you’re in C# land, I’d look at using the ArmClient class to retrieve what you’re looking for.

    Install these (I’ve got a few others installed but start with that and see how you go, there may be a couple of others needed) Nuget packages …

    Azure.Identity;
    Azure.ResourceManager;
    Azure.ResourceManager.AppService
    

    … and from there, using the DefaultCredential approach (if you’ve never used it, read up on it here -> https://github.com/Azure/azure-sdk-for-net/blob/main/sdk/identity/Azure.Identity/README.md) you can query your subscriptions webApps …

    using Azure.Identity;
    using Azure.ResourceManager;
    using Azure.ResourceManager.AppService;
    using System;
    using System.Threading.Tasks;
    
    namespace AzureManagement
    {
        internal class Program
        {
            static void Main(string[] args)
            {
                GetAzureResources().Wait();
            }
    
            static async Task GetAzureResources()
            {
                var credential = new DefaultAzureCredential();
                var armClient = new ArmClient(credential);
                var subscription = await armClient.GetDefaultSubscriptionAsync();
    
                var webSitesEnumerator = subscription.GetWebSitesAsync().GetAsyncEnumerator();
    
                try
                {
                    while (await webSitesEnumerator.MoveNextAsync())
                    {
                        var webSite = webSitesEnumerator.Current;
                        
                        Console.WriteLine($"Web App Name ........ {webSite.Data.Name}");
                        Console.WriteLine($"Default Host Name ... {webSite.Data.DefaultHostName}n");
                    }
                }
                finally
                {
                    await webSitesEnumerator.DisposeAsync();
                }
    
                Console.ReadLine();
            }
        }
    }
    

    The above is obviously not a function app but the core code will still work for you and can be ported as need be.

    Note: I could be telling you how to suck eggs, but, once deployed to Azure, you’ll need to do the necessary work to ensure that the function app has the required access to retrieve all of the resource information you’re looking for.
    If you’re unfamiliar with that, read up on the managed identity concept. It’s very easy to setup -> https://learn.microsoft.com/en-us/azure/app-service/overview-managed-identity

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search