maatwerkgiethoorn.blazor.extendedsessionstate.server 1.4.6

dotnet add package maatwerkgiethoorn.blazor.extendedsessionstate.server --version 1.4.6
                    
NuGet\Install-Package maatwerkgiethoorn.blazor.extendedsessionstate.server -Version 1.4.6
                    
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="maatwerkgiethoorn.blazor.extendedsessionstate.server" Version="1.4.6" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="maatwerkgiethoorn.blazor.extendedsessionstate.server" Version="1.4.6" />
                    
Directory.Packages.props
<PackageReference Include="maatwerkgiethoorn.blazor.extendedsessionstate.server" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add maatwerkgiethoorn.blazor.extendedsessionstate.server --version 1.4.6
                    
#r "nuget: maatwerkgiethoorn.blazor.extendedsessionstate.server, 1.4.6"
                    
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#:package maatwerkgiethoorn.blazor.extendedsessionstate.server@1.4.6
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=maatwerkgiethoorn.blazor.extendedsessionstate.server&version=1.4.6
                    
Install as a Cake Addin
#tool nuget:?package=maatwerkgiethoorn.blazor.extendedsessionstate.server&version=1.4.6
                    
Install as a Cake Tool

maatwerkgiethoorn.Blazor.ExtendedSessionState

'maatwerkgiethoorn.Blazor.ExtendedSessionState' is a package that extends the session manager for a .NET 8 Blazor Web application. It can be used to ensure the app state is synchronized between the server and WebAssembly with automatic re-rendering whenever the state changes.

Installation

Install the package via NuGet:

dotnet add package maatwerkgiethoorn.Blazor.ExtendedSessionState

Usage

Server-side setup

To use this package in a Blazor Server project, follow these steps:

  1. Open your Program.cs file.
  2. Add the following lines to register the necessary services and middleware:
using maatwerkgiethoorn.blazor.extendedsessionstate;

var builder = WebApplication.CreateBuilder(args);

builder.Services
    .AddHttpContextAccessor()  // Ensure HttpContextAccessor is available
    .AddExtendedSessionStateServer<AppState>();  // Register extended session state service

var app = builder.Build();

app.UseExtendedSessionState<AppState>();  // Activate extended session state
  • Replace AppState with any serializable class that has a default constructor and represents the state of your application.

WebAssembly setup

For Blazor WebAssembly projects, follow these steps:

  1. Open your Program.cs file.
  2. Add the following lines to register the session state services:
using maatwerkgiethoorn.blazor.extendedsessionstate;
var builder = WebAssemblyHostBuilder.CreateDefault(args);

builder.Services
    .AddExtendedSessionStateWebAssembly<AppState>(new Uri(builder.HostEnvironment.BaseAddress));  // Register extended session state for WebAssembly

await builder.Build().RunAsync();

AppState Example

The AppState class can be any serializable class or record. Here's an example of a simple AppState class:

[Serializable]
public class AppState
{
    public int CounterCount { get; set; }
}

This class holds the shared state of your application. You can extend it with other properties as needed to maintain the state across the client and server.

Blazor component example

You can use the state in your componentas like this:

@page "/counter"
@rendermode InteractiveAuto
@inject ISessionManager<AppState> SessionManager
<PageTitle>Counter</PageTitle>
<h1>Counter</h1>

<p role="status">Current count: @currentCount</p>
<button class="btn btn-primary" @onclick="IncrementCount">Click me</button>

@code {
 AppState? session;
    protected override async Task OnInitializedAsync()
    {
        session = await SessionManager.GetSessionAsync();
        currentCount = session.CounterCount;
    }

    private async Task IncrementCount()
    {
        currentCount++;
        session.CurrentCount = currentCount;
        
        await SessionManager.UpdateSessionAsync(session);
    }
}

Product Compatible and additional computed target framework versions.
.NET net8.0 is compatible.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed.  net9.0 was computed.  net9.0-android was computed.  net9.0-browser was computed.  net9.0-ios was computed.  net9.0-maccatalyst was computed.  net9.0-macos was computed.  net9.0-tvos was computed.  net9.0-windows was computed.  net10.0 was computed.  net10.0-android was computed.  net10.0-browser was computed.  net10.0-ios was computed.  net10.0-maccatalyst was computed.  net10.0-macos was computed.  net10.0-tvos was computed.  net10.0-windows was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.