|
| 1 | +@page "/__main" |
| 2 | +@using Microsoft.JSInterop |
| 3 | +@using System.Runtime.Loader |
| 4 | +@using Try.UserComponents |
| 5 | +@using TryMudBlazor.Client.Services |
| 6 | +@inject IJSRuntime JSRuntime |
| 7 | +@inject UserComponentsAssemblyService AssemblyService |
| 8 | +@implements IAsyncDisposable |
| 9 | + |
| 10 | +<MudDialogProvider FullWidth="true" MaxWidth="MaxWidth.ExtraSmall" /> |
| 11 | +<MudSnackbarProvider /> |
| 12 | + |
| 13 | +<DynamicComponent Type="@_type" /> |
| 14 | + |
| 15 | +@code { |
| 16 | + private Type _type = typeof(__Main); |
| 17 | + private AssemblyLoadContext _loadContext; |
| 18 | + private readonly Lazy<DotNetObjectReference<UserMainPage>> _ref; |
| 19 | + |
| 20 | + public UserMainPage() |
| 21 | + { |
| 22 | + _ref = new Lazy<DotNetObjectReference<UserMainPage>>(CreateDotNetObject()); |
| 23 | + } |
| 24 | + |
| 25 | + protected override async Task OnAfterRenderAsync(bool firstRender) |
| 26 | + { |
| 27 | + if (firstRender) |
| 28 | + { |
| 29 | + await JSRuntime.InvokeVoidAsync("Try.registerHotReload", _ref.Value); |
| 30 | + } |
| 31 | + } |
| 32 | + |
| 33 | + [JSInvokable] |
| 34 | + public async Task HotReload() |
| 35 | + { |
| 36 | + try |
| 37 | + { |
| 38 | + var base64 = await JSRuntime.InvokeAsync<string>("sessionStorage.getItem", "TryMudBlazor.UserComponentsDllBase64"); |
| 39 | + if (base64 == null) |
| 40 | + { |
| 41 | + // sessionStorage was cleared (e.g. crash recovery) — fall back to a full reload |
| 42 | + // so loadBootResource picks up the safe static DLL. |
| 43 | + await JSRuntime.InvokeVoidAsync("Try.requestFullReload", "/__main"); |
| 44 | + return; |
| 45 | + } |
| 46 | + |
| 47 | + var bytes = Convert.FromBase64String(base64); |
| 48 | + |
| 49 | + // Swap to a fresh collectible context; previous one becomes eligible for GC |
| 50 | + // once Blazor disposes the old component instance below. |
| 51 | + var previousContext = _loadContext; |
| 52 | + _loadContext = new AssemblyLoadContext(name: null, isCollectible: true); |
| 53 | + var assembly = _loadContext.LoadFromStream(new MemoryStream(bytes)); |
| 54 | + previousContext?.Unload(); |
| 55 | + |
| 56 | + // UserStartup requires DI re-registration — fall back to full iframe reload |
| 57 | + if (assembly.GetType("UserStartup") is not null || |
| 58 | + assembly.GetType("Try.UserComponents.UserStartup") is not null) |
| 59 | + { |
| 60 | + await JSRuntime.InvokeVoidAsync("Try.requestFullReload", "/__main"); |
| 61 | + return; |
| 62 | + } |
| 63 | + |
| 64 | + var newType = assembly.GetType("Try.UserComponents.__Main"); |
| 65 | + if (newType is not null) |
| 66 | + { |
| 67 | + // Update the Router's AdditionalAssemblies before rendering the new __Main |
| 68 | + // so that any NavigateTo calls inside it can resolve user-defined @page routes. |
| 69 | + await AssemblyService.UpdateAssemblyAsync(assembly); |
| 70 | + _type = newType; |
| 71 | + await InvokeAsync(StateHasChanged); |
| 72 | + } |
| 73 | + } |
| 74 | + catch |
| 75 | + { |
| 76 | + await JSRuntime.InvokeVoidAsync("Try.requestFullReload", "/__main"); |
| 77 | + } |
| 78 | + } |
| 79 | + |
| 80 | + public async ValueTask DisposeAsync() |
| 81 | + { |
| 82 | + if (_ref.IsValueCreated) |
| 83 | + { |
| 84 | + await JSRuntime.InvokeVoidAsync("Try.unregisterHotReload"); |
| 85 | + _ref.Value.Dispose(); |
| 86 | + } |
| 87 | + _loadContext?.Unload(); |
| 88 | + } |
| 89 | + |
| 90 | + private DotNetObjectReference<UserMainPage> CreateDotNetObject() => DotNetObjectReference.Create(this); |
| 91 | +} |
0 commit comments