Skip to content

Update ResourceCollectionProvider to take advantage of Declarative persistent component state #61748

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -206,13 +206,28 @@ public PropertiesAccessor([DynamicallyAccessedMembers(LinkerFlags.Component)] Ty

public (string, Type)[] KeyTypePairs => _cachedKeysForService;

// Mapping for internal classes bundled in different assemblies during prerendering and WASM rendering.
private static readonly Dictionary<string, string> _canonicalMap = new(StringComparer.OrdinalIgnoreCase)
{
{ "Microsoft.AspNetCore.Components.Endpoints", "Microsoft.AspNetCore.Components" },
{ "Microsoft.AspNetCore.Components.WebAssembly", "Microsoft.AspNetCore.Components" }
};

private static string ComputeKey(Type keyType, string propertyName)
{
// This happens once per type and property combo, so allocations are ok.
var assemblyName = keyType.Assembly.FullName;
var assemblySimpleName = keyType.Assembly.GetName().Name ?? "";
if (_canonicalMap.TryGetValue(assemblySimpleName, out var canonicalAssembly))
{
assemblyName = canonicalAssembly;
}

var typeName = keyType.FullName;
var input = Encoding.UTF8.GetBytes(string.Join(".", assemblyName, typeName, propertyName));
return Convert.ToBase64String(SHA256.HashData(input));
var inputString = string.Join(".", assemblyName, typeName, propertyName);
var input = Encoding.UTF8.GetBytes(inputString);
var hash = SHA256.HashData(input);
return Convert.ToBase64String(hash);
}

internal static IEnumerable<PropertyInfo> GetCandidateBindableProperties(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ public static IRazorComponentsBuilder AddRazorComponents(this IServiceCollection
services.TryAddScoped<WebAssemblySettingsEmitter>();

services.TryAddScoped<ResourceCollectionProvider>();
RegisterPersistentComponentStateServiceCollectionExtensions.AddPersistentServiceRegistration<ResourceCollectionProvider>(services, RenderMode.InteractiveWebAssembly);

// Form handling
services.AddSupplyValueFromFormProvider();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ private static void InitializeResourceCollection(HttpContext httpContext)
var resourceCollectionProvider = resourceCollectionUrl != null ? httpContext.RequestServices.GetService<ResourceCollectionProvider>() : null;
if (resourceCollectionUrl != null && resourceCollectionProvider != null)
{
resourceCollectionProvider.SetResourceCollectionUrl(resourceCollectionUrl.Url);
resourceCollectionProvider.ResourceCollectionUrl = resourceCollectionUrl.Url;
resourceCollectionProvider.SetResourceCollection(resourceCollection ?? ResourceAssetCollection.Empty);
}
}
Expand Down
39 changes: 15 additions & 24 deletions src/Components/Shared/src/ResourceCollectionProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,36 +11,27 @@ namespace Microsoft.AspNetCore.Components;

internal class ResourceCollectionProvider
{
private const string ResourceCollectionUrlKey = "__ResourceCollectionUrl";
private string? _url;
private ResourceAssetCollection? _resourceCollection;
private readonly PersistentComponentState _state;
private readonly IJSRuntime _jsRuntime;

[UnconditionalSuppressMessage("Trimming", "IL2026:Members annotated with 'RequiresUnreferencedCodeAttribute' require dynamic access otherwise can break functionality when trimming application code", Justification = "Strings are not trimmed")]
public ResourceCollectionProvider(PersistentComponentState state, IJSRuntime jsRuntime)
[SupplyParameterFromPersistentComponentState]
public string? ResourceCollectionUrl
{
_state = state;
_jsRuntime = jsRuntime;
_ = _state.TryTakeFromJson(ResourceCollectionUrlKey, out _url);
get => _url;
set
{
if (_url != null)
{
throw new InvalidOperationException("The resource collection URL has already been set.");
}
_url = value;
}
}

[MemberNotNull(nameof(_url))]
[UnconditionalSuppressMessage("Trimming", "IL2026:Members annotated with 'RequiresUnreferencedCodeAttribute' require dynamic access otherwise can break functionality when trimming application code", Justification = "Strings are not trimmed")]
internal void SetResourceCollectionUrl(string url)
private ResourceAssetCollection? _resourceCollection;
private readonly IJSRuntime _jsRuntime;
public ResourceCollectionProvider(IJSRuntime jsRuntime)
{
if (_url != null)
{
throw new InvalidOperationException("The resource collection URL has already been set.");
}
_url = url;
PersistingComponentStateSubscription registration = default;
registration = _state.RegisterOnPersisting(() =>
{
_state.PersistAsJson(ResourceCollectionUrlKey, _url);
registration.Dispose();
return Task.CompletedTask;
}, RenderMode.InteractiveWebAssembly);
_jsRuntime = jsRuntime;
}

internal async Task<ResourceAssetCollection> GetResourceCollection()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -308,6 +308,7 @@ internal void InitializeDefaultServices()
Services.AddSupplyValueFromPersistentComponentStateProvider();
Services.AddSingleton<IErrorBoundaryLogger, WebAssemblyErrorBoundaryLogger>();
Services.AddSingleton<ResourceCollectionProvider>();
RegisterPersistentComponentStateServiceCollectionExtensions.AddPersistentServiceRegistration<ResourceCollectionProvider>(Services, RenderMode.InteractiveWebAssembly);
Services.AddLogging(builder =>
{
builder.AddProvider(new WebAssemblyConsoleLoggerProvider(DefaultWebAssemblyJSRuntime.Instance));
Expand Down
Loading