Debug .NET 5 Function with Visual Studio/Visual Studio Code
Tags: azure, csharp, dotnet, function
Published: 2021-05-06 08:00:28

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.

```shell
mkdir debugmyfunction
cd debugmyfunction
func new -n debugtest --worker-runtime dotnet-isolated
```

2\. Select any trigger as you want. I selected HttpTrigger.
![image](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/j4io50mhemkki16ig12y.png)
 
3\. Run the func for test.

```shell
func start
```
![image](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/0j5d3lnn1hfgbbi8sx8f.png)
 
4\. Call the function for test.

![image](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/hutto7oc2usi349mtprx.png)

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

```shell
func start --dotnet-isolated-debug
```

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

![image](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/qndvahdvllobr7ryh7wp.png)

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

![image](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/x2i8lq85yciilf23ym29.png)
  
4\. Find **dotnet.exe** and attach to it. Don't attach to **func,exe**!

![image](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/pklp6w5tq6zbp4pcgizg.png)
 
5\. Now we can do startup debug.

![image](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/cy1bfgih7jt0h8gqlpi5.png)
 
# 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**
```json
{
    "version": "0.2.0",
    "configurations": [
        {
            "name": "Attach to .NET Functions",
            "type": "coreclr",
            "request": "attach",
            "processId": "${command:azureFunctions.pickProcess}"
        }
    ]
}
```

**tasks.json**
Optional.
```json
{
	"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"
		}
	]
}
```

## Use --dotnet-isolated-debug

We can do the same as Visual Studio, too.

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

```shell
func start --dotnet-isolated-debug
```
2\. Go to Visual Studio Code and set break point.

![image](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ctif1v5s23waxs6tsfxs.png)

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

![image](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/mryfnf4gcclfc4odphav.png)

4\. Select .NET Core.

![image](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/y0zjjnlgjqtdstkx5jsx.png)
 
5\. From dropdown, select .NET Core Attach.

![image](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/rlufnye0h5hzy03r7q12.png)
 
6\. Press F5 key and select the dotnet.exe 

![image](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/l4tubwhl60fcmazm6z6n.png)
 
7\. Now we can debug it.

![image](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/kc0r3qregp3mevcxdnb1.png)


Enjoy debugging!!