Software Architect / Microsoft MVP (AI) and Technical Author

Azure, Azure Functions, C#, Developer Life, General Development, Process Improvement

How To: Remote Debug a Deployed Azure Function using Visual Studio 2022

I have an Azure Function that can be invoked using an HTTP Trigger.

 

This function lets me execute processes periodically for the free journal and mood tracking micro-SaaS https://dailytracker.co I recently shipped.

 

I had an issues with the function after deploying it to Azure that I couldn’t resolve.

 

The function worked locally but always returned 500 (server error) on Azure.

 

Remote debugging the function was the only way to figure out what was going on.

 

In this blog you will learn how to remotely debug an Azure Function directly from Visual Studio.

~

The HTTP Trigger Function

For reference, the function that can be invoked over HTTP:

       [FunctionName("H_GenerateAnalytics")]

        public static async Task<IActionResult> GenerateFromRequest(

            [HttpTrigger(AuthorizationLevel.Function, "get", "post", Route = null)] HttpRequest req,

            ILogger log, ExecutionContext context)

        {

            var config = new ConfigurationBuilder()

            .SetBasePath(context.FunctionAppDirectory)

            .AddJsonFile("appsettings.json", optional: false)

            .AddEnvironmentVariables()

            .Build();

            var cfgValues = config.GetSection("values");

            log.LogInformation("C# HTTP trigger function processed a request.");

            TextAnalyticsService analyticsService =

                new TextAnalyticsService(cfgValues["TextAnalyticsEndpoint"], cfgValues["TextAnalyticsAPIKey"]);




            analyticsService.{Some-Logic};

            string responseMessage ="This HTTP triggered function executed”             

            return new OkObjectResult(responseMessage);

        }

~

Deployed Function URL in the Azure Portal

First, fetch the deployed function URL from the Azure Portal:

~

Deployed Function App Key

Next, take note of the default app key.  This will be needed to construct the HTTP request and is supplied as a parameter.

~

Check You Have Remote Debugging Enabled

This wont work if you haven’t enabled this feature in the Azure Portal for the Azure Function.  It’s normally disabled by default.

 

To enable this feature, go to Settings->Configuration->General Settings:

 

Enable Remote Debugging:

~

Remotely Attaching the Visual Debugger to the Function Hosted in Azure

You need to find the function in Azure from Visual Studio and attach a debugger to the remote process.

 

To do this, click on Debug->Attach to Process:

 

Change the Connection Type to Microsoft Azure App Service:

 

Click Find:

 

Select the function and click ok:

 

The dialog attempts to connect to the remote process. After a few seconds, information is fetched:

 

Click Attach and Visual Studio launches, and the debug session is enabled:

 

The Azure Function can now be tested by sending an HTTP request in Postman.

~

Testing with Postman

To debug/test the Azure Function using the HTTP Trigger, some values are needed from the earlier steps.

  • the function URL
  • app key
  • function method name

 

We take each of these to construct a URL.  For example:

https://dailytrackertextanalyticsfunction.azurewebsites.net/api/H_GenerateAnalytics?code={app-key}

 

With visual studio still running in debug mode, and attached to the remote Azure Function in Azure, it can be tested by creating a POST request:

 

Click Send and the debugger in Visual Studio hits a breakpoint:

 

When the function completes, the success status code returns in Postman:

 

Success!

~

Summary

Remote debugging helped me understand why my DBContext class couldn’t find a database connection string when deployed to Azure.

 

You can read more about how I resolved that in this blog post.

 

Happy remote debugging.

JOIN MY EXCLUSIVE EMAIL LIST
Get the latest content and code from the blog posts!
I respect your privacy. No spam. Ever.

Leave a Reply