From 9a74fbba425a9ecd7beaf5f361d274b7557119e7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marek=20Fi=C5=A1era?= Date: Thu, 4 Sep 2025 20:37:10 +0200 Subject: [PATCH 1/3] Clear RootTypeCache cache on HotReload --- src/Components/Shared/src/RootTypeCache.cs | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/src/Components/Shared/src/RootTypeCache.cs b/src/Components/Shared/src/RootTypeCache.cs index 9c41b8cd1533..6da3cd920f9e 100644 --- a/src/Components/Shared/src/RootTypeCache.cs +++ b/src/Components/Shared/src/RootTypeCache.cs @@ -4,6 +4,9 @@ using System.Collections.Concurrent; using System.Diagnostics.CodeAnalysis; using System.Reflection; +#if COMPONENTS +using Microsoft.AspNetCore.Components.HotReload; +#endif #if COMPONENTS namespace Microsoft.AspNetCore.Components.Infrastructure; @@ -16,6 +19,16 @@ internal sealed class RootTypeCache { private readonly ConcurrentDictionary _typeToKeyLookUp = new(); +#if COMPONENTS + static RootTypeCache() + { + if (HotReloadManager.Default.MetadataUpdateSupported) + { + HotReloadManager.Default.OnDeltaApplied += ClearCache; + } + } +#endif + public Type? GetRootType(string assembly, string type) { var key = new Key(assembly, type); From 217018d916728b0719a6610634c551e9b076d217 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marek=20Fi=C5=A1era?= Date: Fri, 5 Sep 2025 11:57:51 +0200 Subject: [PATCH 2/3] ClearCache method --- src/Components/Shared/src/RootTypeCache.cs | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/Components/Shared/src/RootTypeCache.cs b/src/Components/Shared/src/RootTypeCache.cs index 6da3cd920f9e..f1aa224f462b 100644 --- a/src/Components/Shared/src/RootTypeCache.cs +++ b/src/Components/Shared/src/RootTypeCache.cs @@ -26,7 +26,9 @@ static RootTypeCache() { HotReloadManager.Default.OnDeltaApplied += ClearCache; } - } + }; + + private static void ClearCache() => _typeToKeyLookUp.Clear(); #endif public Type? GetRootType(string assembly, string type) From e755b75932a658eab34a65225dabe6e58d43a4ae Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marek=20Fi=C5=A1era?= Date: Thu, 11 Sep 2025 13:33:54 +0200 Subject: [PATCH 3/3] Fix --- ...rosoft.AspNetCore.Components.Server.csproj | 1 + src/Components/Shared/src/RootTypeCache.cs | 22 +++++++++++-------- ...t.AspNetCore.Components.WebAssembly.csproj | 1 + 3 files changed, 15 insertions(+), 9 deletions(-) diff --git a/src/Components/Server/src/Microsoft.AspNetCore.Components.Server.csproj b/src/Components/Server/src/Microsoft.AspNetCore.Components.Server.csproj index a26f7d17f2d1..216f05e54134 100644 --- a/src/Components/Server/src/Microsoft.AspNetCore.Components.Server.csproj +++ b/src/Components/Server/src/Microsoft.AspNetCore.Components.Server.csproj @@ -39,6 +39,7 @@ + diff --git a/src/Components/Shared/src/RootTypeCache.cs b/src/Components/Shared/src/RootTypeCache.cs index f1aa224f462b..016ab5acd814 100644 --- a/src/Components/Shared/src/RootTypeCache.cs +++ b/src/Components/Shared/src/RootTypeCache.cs @@ -1,12 +1,10 @@ // Licensed to the .NET Foundation under one or more agreements. // The .NET Foundation licenses this file to you under the MIT license. +using Microsoft.AspNetCore.Components.HotReload; using System.Collections.Concurrent; using System.Diagnostics.CodeAnalysis; using System.Reflection; -#if COMPONENTS -using Microsoft.AspNetCore.Components.HotReload; -#endif #if COMPONENTS namespace Microsoft.AspNetCore.Components.Infrastructure; @@ -15,21 +13,27 @@ namespace Microsoft.AspNetCore.Components; #endif // A cache for root component types -internal sealed class RootTypeCache +internal sealed class RootTypeCache : IDisposable { private readonly ConcurrentDictionary _typeToKeyLookUp = new(); -#if COMPONENTS - static RootTypeCache() + public RootTypeCache() { if (HotReloadManager.Default.MetadataUpdateSupported) { HotReloadManager.Default.OnDeltaApplied += ClearCache; } - }; + } - private static void ClearCache() => _typeToKeyLookUp.Clear(); -#endif + internal void ClearCache() => _typeToKeyLookUp.Clear(); + + public void Dispose() + { + if (HotReloadManager.Default.MetadataUpdateSupported) + { + HotReloadManager.Default.OnDeltaApplied -= ClearCache; + } + } public Type? GetRootType(string assembly, string type) { diff --git a/src/Components/WebAssembly/WebAssembly/src/Microsoft.AspNetCore.Components.WebAssembly.csproj b/src/Components/WebAssembly/WebAssembly/src/Microsoft.AspNetCore.Components.WebAssembly.csproj index f0418fd10354..ba479f5352f7 100644 --- a/src/Components/WebAssembly/WebAssembly/src/Microsoft.AspNetCore.Components.WebAssembly.csproj +++ b/src/Components/WebAssembly/WebAssembly/src/Microsoft.AspNetCore.Components.WebAssembly.csproj @@ -24,6 +24,7 @@ +