You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: aspnetcore/blazor/state-management/prerendered-state-persistence.md
+64-14Lines changed: 64 additions & 14 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -32,6 +32,10 @@ The first logged count occurs during prerendering. The count is set again after
32
32
33
33
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)).
34
34
35
+
By initializing components with the same state used during prerendering, any expensive initialization steps are only executed once. The rendered UI also matches the prerendered UI, so no flicker occurs in the browser.
36
+
37
+
The persisted prerendered state is transferred to the client, where it's used to restore the component state. During client-side rendering (CSR, `InteractiveWebAssembly`), the data is exposed to the browser and must not contain sensitive, private information. During interactive server-side rendering (interactive SSR, `InteractiveServer`), [ASP.NET Core Data Protection](xref:security/data-protection/introduction) ensures that the data is transferred securely. The `InteractiveAuto` render mode combines WebAssembly and Server interactivity, so it's necessary to consider data exposure to the browser, as in the CSR case.
38
+
35
39
:::moniker range=">= aspnetcore-10.0"
36
40
37
41
<!-- UPDATE 10.0 - API cross-links -->
@@ -136,6 +140,8 @@ In the following example that serializes state for multiple components of the sa
136
140
}
137
141
```
138
142
143
+
## Serialize state for services
144
+
139
145
In the following example that serializes state for a dependency injection service:
140
146
141
147
* Properties annotated with the `[PersistentState]` attribute are serialized during prerendering and deserialized when the app becomes interactive.
@@ -148,12 +154,19 @@ In the following example that serializes state for a dependency injection servic
148
154
> [!NOTE]
149
155
> Only persisting scoped services is supported.
150
156
151
-
<!-- UPDATE 10.0 - Flesh out with a fully-working example. -->
157
+
Serialized properties are identified from the actual service instance:
152
158
153
-
`CounterService.cs`:
159
+
* This approach allows marking an abstraction as a persistent service.
160
+
* Enables actual implementations to be internal or different types.
161
+
* Supports shared code in different assemblies.
162
+
* Results in each instance exposing the same properties.
163
+
164
+
The following counter service, `CounterTracker`, marks its current count property, `CurrentCount` with the `[PersistentState]` attribute. The property is serialized during prerendering and deserialized when the app becomes interactive wherever the service is injected.
165
+
166
+
`CounterTracker.cs`:
154
167
155
168
```csharp
156
-
publicclassCounterService
169
+
publicclassCounterTracker
157
170
{
158
171
[PersistentState]
159
172
publicintCurrentCount { get; set; }
@@ -165,19 +178,60 @@ public class CounterService
165
178
}
166
179
```
167
180
168
-
In `Program.cs`:
181
+
In the `Program` file, register the scoped service and register the service for persistence with `RegisterPersistentService`. In the following example, the `CounterTracker` service is available for both the Interactive Server and Interactive WebAssembly render modes if a component renders in either of those modes because it's registered with `RenderMode.InteractiveAuto`.
182
+
183
+
If the `Program` file doesn't already use the <xref:Microsoft.AspNetCore.Components.Web?displayProperty=fullName> namespace, add the following `using` statement to the top of the file:
169
184
170
185
```csharp
186
+
usingMicrosoft.AspNetCore.Components.Web;
187
+
```
188
+
189
+
Where services are registered in the `Program` file:
Serialized properties are identified from the actual service instance:
198
+
Inject the `CounterTracker` service into a component and use it to increment a counter. For demonstration purposes in the following example, the value of the service's `CurrentCount` property is set to 10 only during prerendering.
176
199
177
-
* This approach allows marking an abstraction as a persistent service.
178
-
* Enables actual implementations to be internal or different types.
179
-
* Supports shared code in different assemblies.
180
-
* Results in each instance exposing the same properties.
To use preceding component to demonstrate persisting the count of 10 in `CounterTracker.CurrentCount`, navigate to the component and refresh the browser, which triggers prerendering. When prerendering occurs, you briefly see <xref:Microsoft.AspNetCore.Components.RendererInfo.Name%2A?displayProperty=nameWithType> indicate "`Static`" before displaying "`Server`" after final rendering. The counter starts at 10.
233
+
234
+
## Use the `PersistentComponentState` service directly instead of the declarative model
181
235
182
236
As an alternative to using the declarative model for persisting state with the `[PersistentState]` attribute, you can use the <xref:Microsoft.AspNetCore.Components.PersistentComponentState> service directly, which offers greater flexibility for complex state persistence scenarios. Call <xref:Microsoft.AspNetCore.Components.PersistentComponentState.RegisterOnPersisting%2A?displayProperty=nameWithType> to register a callback to persist the component state during prerendering. The state is retrieved when the component renders interactively. Make the call at the end of initialization code in order to avoid a potential race condition during app shutdown.
183
237
@@ -268,10 +322,6 @@ When the component executes, `currentCount` is only set once during prerendering
268
322
269
323
:::moniker-end
270
324
271
-
By initializing components with the same state used during prerendering, any expensive initialization steps are only executed once. The rendered UI also matches the prerendered UI, so no flicker occurs in the browser.
272
-
273
-
The persisted prerendered state is transferred to the client, where it's used to restore the component state. During client-side rendering (CSR, `InteractiveWebAssembly`), the data is exposed to the browser and must not contain sensitive, private information. During interactive server-side rendering (interactive SSR, `InteractiveServer`), [ASP.NET Core Data Protection](xref:security/data-protection/introduction) ensures that the data is transferred securely. The `InteractiveAuto` render mode combines WebAssembly and Server interactivity, so it's necessary to consider data exposure to the browser, as in the CSR case.
274
-
275
325
:::moniker range=">= aspnetcore-10.0"
276
326
277
327
## Serialization extensibility for persistent component state
Copy file name to clipboardExpand all lines: aspnetcore/blazor/state-management/server.md
+2Lines changed: 2 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -51,6 +51,8 @@ An app can only persist *app state*. UIs can't be persisted, such as component i
51
51
52
52
## Circuit state persistence
53
53
54
+
<!-- UPDATE 10.0 - API doc cross-links -->
55
+
54
56
During server-side rendering, Blazor Web Apps can persist a user's session (circuit) state when the connection to the server is lost for an extended period of time or proactively paused, as long as a full-page refresh isn't triggered. This allows users to resume their session without losing unsaved work in the following scenarios:
0 commit comments