Skip to content

Conversation

Copilot
Copy link
Contributor

@Copilot Copilot AI commented Jun 17, 2025

This PR adds registration of persistent component state services to the AddBlazorWebView extension method, enabling persistent component state functionality in Blazor WebView applications.

Changes Made

  • Added ComponentStatePersistenceManager service registration as scoped
  • Added PersistentComponentState service registration as scoped with factory that retrieves the State property from the manager
  • Added necessary using statement for Microsoft.AspNetCore.Components.Infrastructure
  • Added comprehensive unit tests to verify services are registered correctly with proper lifetime scope

Problem

Previously, Blazor WebView applications couldn't use persistent component state because the required services (ComponentStatePersistenceManager and PersistentComponentState) were not registered in the service collection. Components that tried to inject PersistentComponentState would fail at runtime.

Solution

The fix follows the same pattern used in RazorComponentsServiceCollectionExtensions.AddRazorComponents:

services.TryAddScoped<ComponentStatePersistenceManager>();
services.TryAddScoped<PersistentComponentState>(sp => sp.GetRequiredService<ComponentStatePersistenceManager>().State);

This enables Blazor WebView applications to use persistent component state just like server-side and WebAssembly applications.

Testing

Added unit tests that verify:

  • Services are registered and can be resolved from the service provider
  • Services are registered with the correct scoped lifetime
  • The PersistentComponentState service correctly returns the State property from the manager

Fixes #62383.


💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.

@Copilot Copilot AI changed the title [WIP] [Blazor] Register persistent component state on Blazor Webview [Blazor] Register persistent component state services in AddBlazorWebView Jun 17, 2025
@Copilot Copilot AI requested a review from javiercn June 17, 2025 21:51
Copilot finished work on behalf of javiercn June 17, 2025 21:51
@javiercn javiercn marked this pull request as ready for review June 18, 2025 09:30
@javiercn javiercn requested a review from a team as a code owner June 18, 2025 09:30
@javiercn
Copy link
Member

Adds the required registrations so that WebView based apps don't fail when a component contains [PersistentComponentState]

@javiercn
Copy link
Member

@copilot rebase this PR against main

Copy link
Contributor Author

Copilot AI commented Jun 18, 2025

@copilot rebase this PR against main

Done! Successfully rebased the PR against main. The branch is now up to date with the latest changes from the main branch. (c4f6ea3)

Copilot finished work on behalf of javiercn June 18, 2025 10:30
@javiercn javiercn force-pushed the copilot/fix-62383 branch from c4f6ea3 to b37571f Compare June 18, 2025 10:34
@javiercn javiercn merged commit 823c50d into main Jun 18, 2025
27 checks passed
@javiercn javiercn deleted the copilot/fix-62383 branch June 18, 2025 12:58
@dotnet-policy-service dotnet-policy-service bot added this to the 10.0-preview7 milestone Jun 18, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[Blazor] Register persistent component state on Blazor Webview
3 participants