Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I debug Azure Functions using .NET 5 (isolated process) in Visual Studio?

I've recently migrated from .NET Core 3.1 to .NET 5.0 (using isolated/out-of-process runtime) for an Azure Function project in C#. Everything is working as expected. However, whenever I debug, none of my breakpoints hits. Why can't I debug my Azure Function app now, but I used to be able to?

like image 697
GLJ Avatar asked May 13 '21 21:05

GLJ


People also ask

Do Azure functions support .NET 5?

As of March 2021, Microsoft announced that Azure Functions are supported running on . NET 5.

How do I debug a .NET application in Visual Studio?

As the following image shows, Visual Studio indicates the line on which the breakpoint is set by highlighting it and displaying a red dot in the left margin. Press F5 to run the program in Debug mode. Another way to start debugging is by choosing Debug > Start Debugging from the menu.


Video Answer


2 Answers

If you are using Visual Studio Version 16.10 or later, debugging in Visual Studio is straightforward.

The updated steps from Microsoft are as follows:

Visual Studio integrates with Azure Functions Core Tools so that you can test your functions locally using the full Azure Functions runtime.

  • To run your function, press F5 in Visual Studio. You might need to enable a firewall exception so that the tools can handle HTTP requests. Authorization levels are never enforced when you run a function locally.

  • Copy the URL of your function from the Azure Functions runtime output and run the request. A welcome to Functions message is displayed when the function runs successfully and logs are written to the runtime output.

  • To stop debugging, press Shift+F5 in Visual Studio.

After you've verified that the function runs correctly on your local computer, it's time to publish the project to Azure.


This below section only applies if you are using Visual Studio Version 16.9 or earlier. I highly recommend upgrading Visual Studio instead of using this "PITA" method.

(Please see the answer from Andrii for an alternative solution)

After doing a deal of research online, I've learned that the isolated process used by .NET 5 for Azure Functions doesn't support debugging by default. To do so in Visual Studio, you need to follow these steps (link used to be valid, but has since been updated).

  1. Open your solution in Visual Studio
  2. Open PowerShell within Visual Studio (View -> Terminal, or Ctrl-`)
  3. Navigate to your project cd MyProject
  4. Start the function with debugging enabled func start –-dotnet-isolated-debug At this point, you should see a PID printed in the terminal output that looks similar to the following
 Functions:
     HttpExample: [GET,POST] http://localhost:7071/api/HttpExample
 For detailed output, run func with --verbose flag.
 [2021-03-09T08:41:41.904Z] Azure Functions .NET Worker (PID: 81720) initialized in debug mode. Waiting for debugger to attach...
  1. Open the Attach Process (Open -> Debug) window, and select the PID found in the console output At this point, the breakpoints are now valid and will be hit.
like image 184
GLJ Avatar answered Oct 18 '22 20:10

GLJ


No longer required as the native Visual Studio support already added

There is an easy way!

public static class Program
{
    public static void Main()
    {
        Debugger.Launch(); // The trick! <<=======================================

        using var host = new HostBuilder()
...

Then just launch as usual without debugging (Ctrl+F5) and attach the same Visual Studio instance to dotnet process Attach debugger

And voila, you debug your function!

like image 31
Andrii Avatar answered Oct 18 '22 20:10

Andrii