Skip to content

Commit 61c4e81

Browse files
authored
Move RegisterOnPersisting calls (#35116)
1 parent 3873b42 commit 61c4e81

File tree

5 files changed

+41
-39
lines changed

5 files changed

+41
-39
lines changed

aspnetcore/blazor/components/integration-hosted-webassembly.md

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -415,7 +415,7 @@ In `Pages/_Host.cshtml` of Blazor apps that are WebAssembly prerendered (`WebAss
415415
</body>
416416
```
417417
418-
Decide what state to persist using the <xref:Microsoft.AspNetCore.Components.PersistentComponentState> service. [`PersistentComponentState.RegisterOnPersisting`](xref:Microsoft.AspNetCore.Components.PersistentComponentState.RegisterOnPersisting%2A) registers a callback to persist the component state before the app is paused. The state is retrieved when the application resumes.
418+
Decide what state to persist using the <xref:Microsoft.AspNetCore.Components.PersistentComponentState> service. <xref:Microsoft.AspNetCore.Components.PersistentComponentState.RegisterOnPersisting%2A?displayProperty=nameWithType> registers a callback to persist the component state before the app is paused. The state is retrieved when the application resumes. Make the call at the end of initialization code in order to avoid a potential race condition during app shutdown.
419419

420420
In the following example:
421421

@@ -434,9 +434,6 @@ In the following example:
434434
435435
protected override async Task OnInitializedAsync()
436436
{
437-
persistingSubscription =
438-
ApplicationState.RegisterOnPersisting(PersistData);
439-
440437
if (!ApplicationState.TryTakeFromJson<{TYPE}>(
441438
"{TOKEN}", out var restored))
442439
{
@@ -446,6 +443,9 @@ In the following example:
446443
{
447444
data = restored!;
448445
}
446+
447+
// Call at the end to avoid a potential race condition at app shutdown
448+
persistingSubscription = ApplicationState.RegisterOnPersisting(PersistData);
449449
}
450450
451451
private Task PersistData()
@@ -514,22 +514,22 @@ else
514514
515515
protected override async Task OnInitializedAsync()
516516
{
517-
persistingSubscription =
518-
ApplicationState.RegisterOnPersisting(PersistForecasts);
519-
520517
if (!ApplicationState.TryTakeFromJson<WeatherForecast[]>(
521518
"fetchdata", out var restored))
522519
{
523-
forecasts =
524-
await WeatherForecastService.GetForecastAsync(DateOnly.FromDateTime(DateTime.Now));
520+
forecasts = await WeatherForecastService.GetForecastAsync(
521+
DateOnly.FromDateTime(DateTime.Now));
525522
}
526523
else
527524
{
528525
forecasts = restored!;
529526
}
527+
528+
// Call at the end to avoid a potential race condition at app shutdown
529+
persistingSubscription = ApplicationState.RegisterOnPersisting(PersistData);
530530
}
531531
532-
private Task PersistForecasts()
532+
private Task PersistData()
533533
{
534534
ApplicationState.PersistAsJson("fetchdata", forecasts);
535535
@@ -975,7 +975,7 @@ To solve these problems, Blazor supports persisting state in a prerendered page
975975
</body>
976976
```
977977
978-
Decide what state to persist using the <xref:Microsoft.AspNetCore.Components.PersistentComponentState> service. [`PersistentComponentState.RegisterOnPersisting`](xref:Microsoft.AspNetCore.Components.PersistentComponentState.RegisterOnPersisting%2A) registers a callback to persist the component state before the app is paused. The state is retrieved when the application resumes.
978+
Decide what state to persist using the <xref:Microsoft.AspNetCore.Components.PersistentComponentState> service. <xref:Microsoft.AspNetCore.Components.PersistentComponentState.RegisterOnPersisting%2A?displayProperty=nameWithType> registers a callback to persist the component state before the app is paused. The state is retrieved when the application resumes. Make the call at the end of initialization code in order to avoid a potential race condition during app shutdown.
979979

980980
The following example is an updated version of the `FetchData` component in a hosted Blazor WebAssembly app based on the Blazor project template. The `WeatherForecastPreserveState` component persists weather forecast state during prerendering and then retrieves the state to initialize the component. The [Persist Component State Tag Helper](xref:mvc/views/tag-helpers/builtin-th/persist-component-state-tag-helper) persists the component state after all component invocations.
981981

@@ -1029,9 +1029,6 @@ else
10291029
10301030
protected override async Task OnInitializedAsync()
10311031
{
1032-
persistingSubscription =
1033-
ApplicationState.RegisterOnPersisting(PersistForecasts);
1034-
10351032
if (!ApplicationState.TryTakeFromJson<WeatherForecast[]>(
10361033
"fetchdata", out var restored))
10371034
{
@@ -1042,9 +1039,12 @@ else
10421039
{
10431040
forecasts = restored!;
10441041
}
1042+
1043+
// Call at the end to avoid a potential race condition at app shutdown
1044+
persistingSubscription = ApplicationState.RegisterOnPersisting(PersistData);
10451045
}
10461046
1047-
private Task PersistForecasts()
1047+
private Task PersistData()
10481048
{
10491049
ApplicationState.PersistAsJson("fetchdata", forecasts);
10501050

aspnetcore/blazor/components/integration.md

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -402,7 +402,7 @@ In `Pages/_Host.cshtml` of Blazor apps that are `ServerPrerendered` in a Blazor
402402
</body>
403403
```
404404

405-
Decide what state to persist using the <xref:Microsoft.AspNetCore.Components.PersistentComponentState> service. [`PersistentComponentState.RegisterOnPersisting`](xref:Microsoft.AspNetCore.Components.PersistentComponentState.RegisterOnPersisting%2A) registers a callback to persist the component state before the app is paused. The state is retrieved when the application resumes.
405+
Decide what state to persist using the <xref:Microsoft.AspNetCore.Components.PersistentComponentState> service. <xref:Microsoft.AspNetCore.Components.PersistentComponentState.RegisterOnPersisting%2A?displayProperty=nameWithType> registers a callback to persist the component state before the app is paused. The state is retrieved when the application resumes. Make the call at the end of initialization code in order to avoid a potential race condition during app shutdown.
406406

407407
In the following example:
408408

@@ -421,9 +421,6 @@ In the following example:
421421
422422
protected override async Task OnInitializedAsync()
423423
{
424-
persistingSubscription =
425-
ApplicationState.RegisterOnPersisting(PersistData);
426-
427424
if (!ApplicationState.TryTakeFromJson<{TYPE}>(
428425
"{TOKEN}", out var restored))
429426
{
@@ -433,6 +430,9 @@ In the following example:
433430
{
434431
data = restored!;
435432
}
433+
434+
// Call at the end to avoid a potential race condition at app shutdown
435+
persistingSubscription = ApplicationState.RegisterOnPersisting(PersistData);
436436
}
437437
438438
private Task PersistData()
@@ -501,22 +501,23 @@ else
501501
502502
protected override async Task OnInitializedAsync()
503503
{
504-
persistingSubscription =
505-
ApplicationState.RegisterOnPersisting(PersistForecasts);
506-
507504
if (!ApplicationState.TryTakeFromJson<WeatherForecast[]>(
508505
"fetchdata", out var restored))
509506
{
510507
forecasts =
511-
await WeatherForecastService.GetForecastAsync(DateOnly.FromDateTime(DateTime.Now));
508+
await WeatherForecastService.GetForecastAsync(
509+
DateOnly.FromDateTime(DateTime.Now));
512510
}
513511
else
514512
{
515513
forecasts = restored!;
516514
}
515+
516+
// Call at the end to avoid a potential race condition at app shutdown
517+
persistingSubscription = ApplicationState.RegisterOnPersisting(PersistData);
517518
}
518519
519-
private Task PersistForecasts()
520+
private Task PersistData()
520521
{
521522
ApplicationState.PersistAsJson("fetchdata", forecasts);
522523
@@ -966,7 +967,7 @@ To solve these problems, Blazor supports persisting state in a prerendered page
966967
</body>
967968
```
968969

969-
Decide what state to persist using the <xref:Microsoft.AspNetCore.Components.PersistentComponentState> service. [`PersistentComponentState.RegisterOnPersisting`](xref:Microsoft.AspNetCore.Components.PersistentComponentState.RegisterOnPersisting%2A) registers a callback to persist the component state before the app is paused. The state is retrieved when the application resumes.
970+
Decide what state to persist using the <xref:Microsoft.AspNetCore.Components.PersistentComponentState> service. <xref:Microsoft.AspNetCore.Components.PersistentComponentState.RegisterOnPersisting%2A?displayProperty=nameWithType> registers a callback to persist the component state before the app is paused. The state is retrieved when the application resumes. Make the call at the end of initialization code in order to avoid a potential race condition during app shutdown.
970971

971972
The following example is an updated version of the `FetchData` component based on the Blazor project template. The `WeatherForecastPreserveState` component persists weather forecast state during prerendering and then retrieves the state to initialize the component. The [Persist Component State Tag Helper](xref:mvc/views/tag-helpers/builtin-th/persist-component-state-tag-helper) persists the component state after all component invocations.
972973

@@ -1020,9 +1021,6 @@ else
10201021
10211022
protected override async Task OnInitializedAsync()
10221023
{
1023-
persistingSubscription =
1024-
ApplicationState.RegisterOnPersisting(PersistForecasts);
1025-
10261024
if (!ApplicationState.TryTakeFromJson<WeatherForecast[]>(
10271025
"fetchdata", out var restored))
10281026
{
@@ -1033,9 +1031,12 @@ else
10331031
{
10341032
forecasts = restored!;
10351033
}
1034+
1035+
// Call at the end to avoid a potential race condition at app shutdown
1036+
persistingSubscription = ApplicationState.RegisterOnPersisting(PersistData);
10361037
}
10371038
1038-
private Task PersistForecasts()
1039+
private Task PersistData()
10391040
{
10401041
ApplicationState.PersistAsJson("fetchdata", forecasts);
10411042

aspnetcore/blazor/components/lifecycle.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -639,9 +639,6 @@ else
639639
640640
protected override async Task OnInitializedAsync()
641641
{
642-
persistingSubscription =
643-
ApplicationState.RegisterOnPersisting(PersistData);
644-
645642
if (!ApplicationState.TryTakeFromJson<string>("data", out var restored))
646643
{
647644
data = await LoadDataAsync();
@@ -650,6 +647,9 @@ else
650647
{
651648
data = restored!;
652649
}
650+
651+
// Call at the end to avoid a potential race condition at app shutdown
652+
persistingSubscription = ApplicationState.RegisterOnPersisting(PersistData);
653653
}
654654
655655
private Task PersistData()

aspnetcore/blazor/components/prerender.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ The first logged count occurs during prerendering. The count is set again after
4545

4646
To retain the initial value of the counter during prerendering, Blazor supports persisting state in a prerendered page using the <xref:Microsoft.AspNetCore.Components.PersistentComponentState> service (and for components embedded into pages or views of Razor Pages or MVC apps, the [Persist Component State Tag Helper](xref:mvc/views/tag-helpers/builtin-th/persist-component-state-tag-helper)).
4747

48-
To preserve prerendered state, decide what state to persist using the <xref:Microsoft.AspNetCore.Components.PersistentComponentState> service. <xref:Microsoft.AspNetCore.Components.PersistentComponentState.RegisterOnPersisting%2A?displayProperty=nameWithType> registers a callback to persist the component state before the app is paused. The state is retrieved when the app resumes.
48+
To preserve prerendered state, decide what state to persist using the <xref:Microsoft.AspNetCore.Components.PersistentComponentState> service. <xref:Microsoft.AspNetCore.Components.PersistentComponentState.RegisterOnPersisting%2A?displayProperty=nameWithType> registers a callback to persist the component state before the app is paused. The state is retrieved when the app resumes. Make the call at the end of initialization code in order to avoid a potential race condition during app shutdown.
4949

5050
The following example demonstrates the general pattern:
5151

@@ -64,9 +64,6 @@ The following example demonstrates the general pattern:
6464
6565
protected override async Task OnInitializedAsync()
6666
{
67-
persistingSubscription =
68-
ApplicationState.RegisterOnPersisting(PersistData);
69-
7067
if (!ApplicationState.TryTakeFromJson<{TYPE}>(
7168
"{TOKEN}", out var restored))
7269
{
@@ -76,6 +73,9 @@ The following example demonstrates the general pattern:
7673
{
7774
data = restored!;
7875
}
76+
77+
// Call at the end to avoid a potential race condition at app shutdown
78+
persistingSubscription = ApplicationState.RegisterOnPersisting(PersistData);
7979
}
8080
8181
private Task PersistData()

aspnetcore/blazor/security/index.md

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -627,7 +627,7 @@ internal sealed class ClientWeatherForecaster(HttpClient httpClient)
627627
The client project maintains a `Weather` component that:
628628

629629
* Enforces authorization with an [`[Authorize]` attribute](xref:Microsoft.AspNetCore.Authorization.AuthorizeAttribute).
630-
* Uses the [Persistent Component State service](xref:blazor/components/prerender#persist-prerendered-state) (<xref:Microsoft.AspNetCore.Components.PersistentComponentState>) to persist weather forecast data when the component transitions from static to interactive SSR on the server.
630+
* Uses the [Persistent Component State service](xref:blazor/components/prerender#persist-prerendered-state) (<xref:Microsoft.AspNetCore.Components.PersistentComponentState>) to persist weather forecast data when the component transitions from static to interactive SSR on the server. For more information, see <xref:blazor/components/prerender#persist-prerendered-state>.
631631

632632
```razor
633633
@page "/weather"
@@ -679,8 +679,6 @@ else
679679
680680
protected override async Task OnInitializedAsync()
681681
{
682-
persistingSubscription = ApplicationState.RegisterOnPersisting(PersistData);
683-
684682
if (!ApplicationState.TryTakeFromJson<IEnumerable<WeatherForecast>>(
685683
nameof(forecasts), out var restoredData))
686684
{
@@ -690,6 +688,9 @@ else
690688
{
691689
forecasts = restoredData!;
692690
}
691+
692+
// Call at the end to avoid a potential race condition at app shutdown
693+
persistingSubscription = ApplicationState.RegisterOnPersisting(PersistData);
693694
}
694695
695696
private Task PersistData()

0 commit comments

Comments
 (0)