Skip to content

Commit 4b2513c

Browse files
[release/10.0] [Blazor] Clear RootTypeCache cache on HotReload (#63653)
* Clear RootTypeCache cache on HotReload * ClearCache method * Fix --------- Co-authored-by: Marek Fišera <[email protected]>
1 parent 2210c0c commit 4b2513c

File tree

3 files changed

+22
-1
lines changed

3 files changed

+22
-1
lines changed

src/Components/Server/src/Microsoft.AspNetCore.Components.Server.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@
3939
</PropertyGroup>
4040

4141
<ItemGroup>
42+
<Compile Include="$(ComponentsSharedSourceRoot)src\HotReloadManager.cs" LinkBase="HotReload" />
4243
<Compile Include="$(ComponentsSharedSourceRoot)src\CacheHeaderSettings.cs" Link="Shared\CacheHeaderSettings.cs" />
4344
<Compile Include="$(ComponentsSharedSourceRoot)src\ArrayBuilder.cs" LinkBase="Circuits" />
4445
<Compile Include="$(ComponentsSharedSourceRoot)src\RenderBatchWriter.cs" LinkBase="Circuits" />

src/Components/Shared/src/RootTypeCache.cs

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
// Licensed to the .NET Foundation under one or more agreements.
22
// The .NET Foundation licenses this file to you under the MIT license.
33

4+
using Microsoft.AspNetCore.Components.HotReload;
45
using System.Collections.Concurrent;
56
using System.Diagnostics.CodeAnalysis;
67
using System.Reflection;
@@ -12,10 +13,28 @@ namespace Microsoft.AspNetCore.Components;
1213
#endif
1314

1415
// A cache for root component types
15-
internal sealed class RootTypeCache
16+
internal sealed class RootTypeCache : IDisposable
1617
{
1718
private readonly ConcurrentDictionary<Key, Type?> _typeToKeyLookUp = new();
1819

20+
public RootTypeCache()
21+
{
22+
if (HotReloadManager.Default.MetadataUpdateSupported)
23+
{
24+
HotReloadManager.Default.OnDeltaApplied += ClearCache;
25+
}
26+
}
27+
28+
internal void ClearCache() => _typeToKeyLookUp.Clear();
29+
30+
public void Dispose()
31+
{
32+
if (HotReloadManager.Default.MetadataUpdateSupported)
33+
{
34+
HotReloadManager.Default.OnDeltaApplied -= ClearCache;
35+
}
36+
}
37+
1938
public Type? GetRootType(string assembly, string type)
2039
{
2140
var key = new Key(assembly, type);

src/Components/WebAssembly/WebAssembly/src/Microsoft.AspNetCore.Components.WebAssembly.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
</ItemGroup>
2525

2626
<ItemGroup>
27+
<Compile Include="$(ComponentsSharedSourceRoot)src\HotReloadManager.cs" LinkBase="HotReload" />
2728
<Compile Include="$(ComponentsSharedSourceRoot)src\BrowserNavigationManagerInterop.cs" />
2829
<Compile Include="$(ComponentsSharedSourceRoot)src\PullFromJSDataStream.cs" />
2930
<Compile Include="$(ComponentsSharedSourceRoot)src\TransmitDataStreamToJS.cs" />

0 commit comments

Comments
 (0)