DEV Community

loading...

Debug .NET 5 Function with Visual Studio/Visual Studio Code

Kenichiro Nakamura
Updated on ・2 min read

As we start developing Azure Function with .NET 5, we start wondering how to properly debug the application. Even though we cannot start with F5 as usual, func cli provides --dotnet-isolated-debug option.

Create Simple Function

1. Create a folder and create new Function with dotnet-isolated runtime.

mkdir debugmyfunction
cd debugmyfunction
func new -n debugtest --worker-runtime dotnet-isolated
Enter fullscreen mode Exit fullscreen mode

2. Select any trigger as you want. I selected HttpTrigger.
image

3. Run the func for test.

func start
Enter fullscreen mode Exit fullscreen mode

image

4. Call the function for test.

image

Debug the startup with Visual Studio

We can attach to the process after

func start

, however if we want to debug startup logic, then we can do like this.

1. Run the func by adding --dotnet-isolated-debug option.

func start --dotnet-isolated-debug
Enter fullscreen mode Exit fullscreen mode

2. Go to Visual Studio and set break point on Program.cs.

image

3. From Debug menu, click Attach to Process...

image

4. Find dotnet.exe and attach to it. Don't attach to func,exe!

image

5. Now we can do startup debug.

image

Debug the startup with Visual Studio Code

We can do F5 with Visual Studio Code or use same process as Visual Studio.

Use F5

To make F5 debug works, we simply need launch.json in .vscode folder. If you need to debug the startup, you need tasks.json as well to specify --dotnet-isolated-debug

launch.json

{
    "version": "0.2.0",
    "configurations": [
        {
            "name": "Attach to .NET Functions",
            "type": "coreclr",
            "request": "attach",
            "processId": "${command:azureFunctions.pickProcess}"
        }
    ]
}
Enter fullscreen mode Exit fullscreen mode

tasks.json
Optional.

{
    "version": "2.0.0",
    "tasks": [
        {
            "type": "func",
            "options": {
                "cwd": "${workspaceFolder}/bin/Debug/net5.0"
            },
            "command": "host start --dotnet-isolated-debug",
            "isBackground": true,
            "problemMatcher": "$func-dotnet-watch"
        }
    ]
}
Enter fullscreen mode Exit fullscreen mode

Use --dotnet-isolated-debug

We can do the same as Visual Studio, too.

1. Run the func by adding --dotnet-isolated-debug option.

func start --dotnet-isolated-debug
Enter fullscreen mode Exit fullscreen mode

2. Go to Visual Studio Code and set break point.

image

3. Go to debug menu and click create a launch.json file.

image

4. Select .NET Core.

image

5. From dropdown, select .NET Core Attach.

image

6. Press F5 key and select the dotnet.exe

image

7. Now we can debug it.

image

Enjoy debugging!!

Discussion (2)

Collapse
jwisener profile image
Jason Wisener • Edited

Man this lack of coordination between group at ms. Talk about lack of integration/collaboration.

Collapse
kenakamu profile image
Kenichiro Nakamura Author

I really hope we can simply do F5 to debug as soon as possible :)