RazorSlices icon indicating copy to clipboard operation
RazorSlices copied to clipboard

Support reloading slices when Hot Reload occurs

Open DamianEdwards opened this issue 2 years ago • 4 comments

Do the needful.

Docs: https://learn.microsoft.com/dotnet/api/system.reflection.metadata.metadataupdatehandlerattribute

DamianEdwards avatar Mar 07 '23 22:03 DamianEdwards

Example in MVC: https://github.com/dotnet/aspnetcore/blob/main/src/Mvc/Mvc/src/HotReloadService.cs

DamianEdwards avatar Jun 02 '23 21:06 DamianEdwards

This might already be working for free due to the fact that the underlying types generated by the Razor compiler for each .cshtml file are attributed to reload the whole type on hot reload. It certainly seems to be working in the sample app in the repo. I have a hunch it might be only work when the project is configured to publish native AOT though as in that case slice creation goes through Activator.CreateInstance which is likely honoring the replacement type metadata whereas when not using native AOT I generate a custom delegate that directly news up the slice type via its constructor. That will likely need to be updated to explicitly support Hot Reload.

DamianEdwards avatar Jun 07 '24 00:06 DamianEdwards

I just tried with a simple sample - the slice isn't reloaded on dotnet watch when modified.

https://github.com/dodyg/practical-aspnetcore/tree/net8.0/projects/razor-slices/hello-world

dodyg avatar Jun 12 '24 07:06 dodyg

@dodyg yep, but it will be if you add <PublishAot>true</PublishAot> in the project file. It's a side effect of the different code paths taken to create slices when the runtime doesn't support dynamic code compilation. I intend to update it to support hot reload in all cases though.

DamianEdwards avatar Jun 12 '24 14:06 DamianEdwards