Search code examples
c#visual-studio.net-coreazure-functions.net-5

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?


Solution

  • 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.