Search code examples
c#asp.net-corevisual-studio-codeblazorhot-reload

Why does DisplayClass11 generate in .NET Core hot reload?


How does hot reload implement in .NET Core? Does it modify the project's assembly ?

I open my Blazor project with dotnet watch, but got the following error:

fail: Microsoft.Extensions.Hosting.Internal.Host[9]
      BackgroundService failed
      System.TypeLoadException: Could not load type 'Invalid_Token.0x02000118' from assembly 'MyBlazorWebApp, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null'.
         at BooksService.<>c__DisplayClass11_0.<WatchChangesAsync>b__1
         at MongoDB.Driver.IAsyncCursorExtensions.ForEachAsync[TDocument](IAsyncCursor`1 source, Func`3 processor, CancellationToken cancellationToken)
         at BooksService.WatchChangesAsync(CancellationToken cancellationToken)
         at BookBackgroundService.ExecuteAsync(CancellationToken stoppingToken)
         at Microsoft.Extensions.Hosting.Internal.Host.TryExecuteBackgroundServiceAsync(BackgroundService backgroundService)
crit: Microsoft.Extensions.Hosting.Internal.Host[10]
      The HostOptions.BackgroundServiceExceptionBehavior is configured to StopHost. A BackgroundService has thrown an unhandled exception, and the IHost instance is stopping. To avoid this behavior, configure this to Ignore; however the BackgroundService will not be restarted.
      System.TypeLoadException: Could not load type 'Invalid_Token.0x02000118' from assembly 'MyBlazorWebApp, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null'.
         at BooksService.<>c__DisplayClass11_0.<WatchChangesAsync>b__1
         at MongoDB.Driver.IAsyncCursorExtensions.ForEachAsync[TDocument](IAsyncCursor`1 source, Func`3 processor, CancellationToken cancellationToken)
         at BooksService.WatchChangesAsync(CancellationToken cancellationToken)
         at BookBackgroundService.ExecuteAsync(CancellationToken stoppingToken)
         at Microsoft.Extensions.Hosting.Internal.Host.TryExecuteBackgroundServiceAsync(BackgroundService backgroundService)
info: Microsoft.Hosting.Lifetime[0]

I have no idea why this error contains DisplayClass11_0 which I don't find anywhere in the project.

Can I think it was added in through hot reload mode?


Solution

  • how does hot reload implement in .net core ? does it modify project's assembly ?

    .NET Hot Reload applies code changes, including changes to stylesheets, to a running app without restarting the app and without losing app state. Hot Reload is supported for all ASP.NET Core 6.0 and later projects.

    It will recompile the modified codes and updating the application by replacing the types, method, IL codes.

    I have no idea why this error contains DisplayClass11_0 which doesn't find in project. can I think it was added in through hot reload mode ?

    Follow this answer, you could find the reason that a closure class is called "DisplayClass" is a bit unfortunate: this is jargon used by the debugger team to describe a class that has special behaviours when displayed in the debugger. Obviously we do not want to display "x" as a field of an impossibly-named class when you are debugging your code; rather, you want it to look like any other local variable. There is special gear in the debugger to handle doing so for this kind of display class. It probably should have been called "ClosureClass" instead, to make it easier to read disassembly.

    The class is not your class, it is generated by the compiler for the anonymous methods, lambda expressions, and local functions.