Why is debugging Blazor applications different from Angular or React?
But if the built in developer tools does not support Blazor, what to use?
At this time there is only very early support for debugging client-side Blazor applications. And the only browser where debugging is supported is in Chrome.
The reason only Chrome is supported at this time is because Blazor provides a debugging proxy that implements the Chrome DevTools Protocol. This allows the DevTools in Chrome to talk to the proxy, which in turn connects to your running Blazor application. In order for the proxy to be able to connect to the running application, remote debugging must be enabled in Chrome. It’s a bit cumbersome, but I will go through the steps required in detail below.
Hopefully the Blazor team will focus on improving the debugging support since it is a very important ingredient if Blazor is to be popular among developers.
Debugging – step-by-step guide
Follow these steps and you will have a debugging session up and running in no time. I will assume you have Chrome installed and a working Blazor application that you wish to debug.
- Open up the application in Visual Studio (I was unable to start the debugging session when started on the command line)
- Ensure that Visual Studio is set up for building the application in Debug mode and the target browser is set to Chrome (see image below)
- Press F5 to start the application and it should open up Chrome and load it
- Press Shift+Alt+D (I use Dvorak keyboard layout where the QWERTY layout D is mapped to the letter E, so I had to press Shift+Alt+E)
- Chrome will open a new tab, showing an error message that it wasn’t started with remote debugging enabled
- Follow the instructions in the error message (close Chrome, then restart it using Win+R and paste a command similar to ”%programfiles(x86)%\Google\Chrome\Application\chrome.exe” –remote-debugging-port=9222 http://localhost:54308/)
- In the new Chrome window, press Shift+Alt+D again
- A new tab should open in Chrome showing the remote debug utils
- You should be able to find a heading named ”Sources” (see image below) where you find the Blazor DLL and under that the source files
- Add breakpoints and switch back to the other tab to interact with the application. Once a breakpoint is hit Chrome will display a message saying that the application is paused since it is stopped at a breakpoint
Remember that I wrote that debugging support is still in a very early stage. This means that there are a lot of things that are not supported in the debugger yet. Limitations include, but may not be limited to:
- No support for stepping into child methods
- Values of locals of other types than int, string, and bool cannot be inspected
- Values of class properties and fields cannot be inspected
- It is not possible to see values of variables by hoovering over them
- Expressions cannot be evaluated in the console
As you understand by now there is still a lot of work to do in order to get full debugging support with Blazor, but the fact that there are some debugging support in place is promising. It is a bit cumbersome to start a debug session, but it is not hard. I have worked in really large projects with custom build tools without a working debugger and it is not a good spot to be in. However, with Blazor I have good hopes that the development team understands the importance of a good debugger.