DamianEdwards / RazorSlices

Lightweight Razor-based templates for ASP.NET Core without MVC, Razor Pages, or Blazor.
MIT License
297 stars 12 forks source link

Support reloading slices when Hot Reload occurs #4

Open DamianEdwards opened 1 year ago

DamianEdwards commented 1 year ago

Do the needful.

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

DamianEdwards commented 1 year ago

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

DamianEdwards commented 1 month ago

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.

dodyg commented 3 weeks ago

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

DamianEdwards commented 3 weeks ago

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