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

Viewed 4777

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?

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

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!

It doesn't work for me. I have a functionAPp in DotnetCore5, i run it with Ctrl+F5. I have the debug window, i select the run instance. The debug trigger the root file "program.cs" who contain:

#if DEBUG
            Debugger.Launch();
#endif

But not in this other file who contains:

       [Function(FunctionName)]
        public async Task Run([TimerTrigger("0 */1 * * * *"
#if DEBUG
                , RunOnStartup = true
#endif
            )]TimerInfo myTimer, FunctionContext executionContext)
        {
            // Application du log
            _log = executionContext.GetLogger(FunctionName);

And who is well running, i see it in console started (in my example below "GetRubixData")

I tried with VS 2019 & 2022 What i have to check ?

The console contains:

    Functions:
            PostWassaSalissureMesureData: [POST] http://localhost:7071/api/PostWassaSalissureMesureData
    
            GetRubixData: TimerTrigger
    
    For detailed output, run func with --verbose flag. 
[2021-12-22T07:38:06.596Z] Retrying to start listener for function 'Functions.GetRubixData' (Attempt 1) 
[2021-12-22T07:38:06.598Z] Listener successfully started for function 'Functions.GetRubixData' after 1 retries. [2021-12-22T07:38:09.430Z] Host lock lease acquired by instance ID '000000000000000000000000DA099322'. 
info: Microsoft.AspNetCore.Hosting.Diagnostics[1]
          Request starting HTTP/2 POST http://127.0.0.1:55251/AzureFunctionsRpcMessages.FunctionRpc/EventStream application/grpc 
info: Microsoft.AspNetCore.Routing.EndpointMiddleware[0]
          Executing endpoint 'gRPC - /AzureFunctionsRpcMessages.FunctionRpc/EventStream' 
[2021-12-22T07:38:59.183Z] { 
[2021-12-22T07:38:59.183Z]   "ProcessId": 3352, 
[2021-12-22T07:38:59.184Z]   "WorkerVersion": "1.3.1.0", 
[2021-12-22T07:38:59.185Z]   "ProductVersion": "1.3.1\u002B9d5d2f326090a62d4496dfb45611fb696b8960e9", 
[2021-12-22T07:38:59.185Z]   "FrameworkDescription": ".NET 5.0.13", 
[2021-12-22T07:38:59.186Z]   "OSDescription": "Microsoft Windows
    10.0.19044", 
[2021-12-22T07:38:59.186Z]   "OSArchitecture": "X64", 
[2021-12-22T07:38:59.187Z]   "RuntimeIdentifier": "win10-x64", 
[2021-12-22T07:38:59.187Z]   "CommandLine": "C:\\Sources\\Comin-API\\ByCN.ComIn\\src\\00-WEB\\02-FunctionApps\\ByCN.ComIn.FunctionApps.Data\\bin\\Debug\\net5.0\\ByCN.ComIn.FunctionApps.Data.dll
    --host 127.0.0.1 --port 55251 --workerId 66a7fb37-4a35-4e8c-99cf-7c294ca7aea7 --requestId 3be30260-aca2-4e29-b9e8-93a35f06566f --grpcMaxMessageLength 2147483647" 
[2021-12-22T07:38:59.188Z] } 
[2021-12-22T07:38:59.200Z] Worker process started and initialized.

and it stopped here, no one other line is writing after despite the script should loop each minute

Regards

Related