Blazor Hot Reload Debugging Using VSCode

Viewed 56

I want to debug with hot reload enabled using VSCode. I keep getting this error after I made some changes:

BadImageFormatException: Bad IL range. OTPHub.Pages.Counter..ctor()

TargetInvocationException: Exception has been thrown by the target of an invocation. System.RuntimeType.CreateInstanceDefaultCtor(bool publicOnly, bool wrapExceptions)

It seems that there is a problem with the hot reloaded built assembly.

Here is my launch.json:

{
  "version": "0.2.0",
  "configurations": [
    {
      "name": ".NET Core Launch (web)",
      "type": "coreclr",
      "request": "launch",
      "preLaunchTask": "build",
      "program": "dotnet",
      "args": [
        "watch",
        "--project",
        ".",
        "--verbose"
      ],
      "cwd": "${workspaceFolder}/OTPHub",
      "stopAtEntry": false,
      "serverReadyAction": {
        "action": "openExternally",
        "pattern": "\\bNow listening on:\\s+(https?://\\S+)"
      },
      "env": {
        "ASPNETCORE_ENVIRONMENT": "Development"
      },
      "sourceFileMap": {
        "/Views": "${workspaceFolder}/Views"
      }
    },
    {
      "name": ".NET Core Attach",
      "type": "coreclr",
      "request": "attach"
    }
  ]
}

Should change something from this launch.json setting? How can I debug it with hot reload enabled?

1 Answers

I've copied your launch.json over the one in a clean build project with one change to cwd (removed /OTPHub). It all works, so your problem is elsewhere.

Here's my steps:

  1. Create a directory
  2. Start VS in the directory
  3. dotnet new blazorserver
  4. dotnet watch.
  5. Ctrl-C.
  6. Cut and paste your config into launch.json
  7. dotnet watch.
  8. Change some text in Index.
Related