Forge.Wasm.BrowserStorages
1.0.4
dotnet add package Forge.Wasm.BrowserStorages --version 1.0.4
NuGet\Install-Package Forge.Wasm.BrowserStorages -Version 1.0.4
<PackageReference Include="Forge.Wasm.BrowserStorages" Version="1.0.4" />
<PackageVersion Include="Forge.Wasm.BrowserStorages" Version="1.0.4" />
<PackageReference Include="Forge.Wasm.BrowserStorages" />
paket add Forge.Wasm.BrowserStorages --version 1.0.4
#r "nuget: Forge.Wasm.BrowserStorages, 1.0.4"
#addin nuget:?package=Forge.Wasm.BrowserStorages&version=1.0.4
#tool nuget:?package=Forge.Wasm.BrowserStorages&version=1.0.4
Forge.Wasm.BrowserStorages
Forge.Wasm.BrowserStorages is a library that provides access to the browsers local and session storage APIs for WASM applications.
Installing
To install the package add the following line to you csproj file replacing x.x.x with the latest version number:
<PackageReference Include="Forge.Wasm.BrowserStorages" Version="x.x.x" />
You can also install via the .NET CLI with the following command:
dotnet add package Forge.Wasm.BrowserStorages
If you're using Visual Studio you can also install via the built in NuGet package manager.
Setup
You will need to register the local storage services with the service collection in your Startup.cs file in Blazor Server.
public void ConfigureServices(IServiceCollection services)
{
services.AddForgeLocalStorage();
services.AddForgeSessionStorage();
}
Or in your Program.cs file in Blazor WebAssembly.
public static async Task Main(string[] args)
{
var builder = WebAssemblyHostBuilder.CreateDefault(args);
builder.RootComponents.Add<App>("app");
builder.Services.AddForgeLocalStorage();
builder.Services.AddForgeSessionStorage();
await builder.Build().RunAsync();
}
In the case, if IJSInProcessRuntime does not available in your project type, you can not use the syncronized version of the storage. It is possible to skip the registration of the sync providers and services which are requires the implementation of this interface, use the following code to archieve it:
public static async Task Main(string[] args)
{
var builder = WebAssemblyHostBuilder.CreateDefault(args);
builder.RootComponents.Add<App>("app");
builder.Services.AddForgeLocalStorageAsyncOnly();
builder.Services.AddForgeSessionStorageAsyncOnly();
await builder.Build().RunAsync();
}
Registering services as Singleton
If you would like to register LocalStorage or SessionStorage services as singletons, it is possible by using the following method:
builder.Services.AddForgeLocalStorageAsSingleton();
builder.Services.AddForgeSessionStorageAsSingleton();
Or
builder.Services.AddForgeLocalStorageAsyncOnlyAsSingleton();
builder.Services.AddForgeSessionStorageAsyncOnlyAsSingleton();
This method is not recommended in the most cases, try to avoid using it.
Usage
To use Forge.Wasm.BrowserStorages in Blazor WebAssembly, inject the ILocalStorageServiceAsync
| ISessionStorageServiceAsync
per the example below.
@inject Forge.Wasm.BrowserStorages.Services.LocalStorage.ILocalStorageServiceAsync localStorage
@inject Forge.Wasm.BrowserStorages.Services.SessionStorage.ISessionStorageServiceAsync sessionStorage
@code {
protected override async Task OnInitializedAsync()
{
await localStorage.SetAsync("Username", "johndoe");
var usernameFromLocalStorage = await localStorage.GetAsync<string>("Username");
await sessionStorage.SetAsync("Username", "johndoe");
var usernameFromSessionStorage = await sessionStorage.GetAsync<string>("Username");
}
}
It is possible to use synchonous version of API. Change the ILocalStorageServiceAsync
for ILocalStorageServiceSync
which allows you to avoid use of async
/await
.
Please keep it in mind, this is not the recommended way. Other issue can be, if the IJSInProcessRuntime is not available in your environment.
If this is the case, you cannot use synchronous version of storage providers.
It can be one of possible way to detect the synchronous mode support, if you try to case IJSRuntime to IJSInProcessRuntime. If it is possible, synchronous mode could be work.
@inject Forge.Wasm.BrowserStorages.Services.LocalStorage.ILocalStorageServiceAsync localStorage
@inject Forge.Wasm.BrowserStorages.Services.SessionStorage.ISessionStorageServiceAsync sessionStorage
@code {
protected override void OnInitialized()
{
localStorage.Set("Username", "johndoe");
var usernameFromLocalStorage = localStorage.Get<string>("Username");
sessionStorage.Set("Username", "johndoe");
var usernameFromSessionStorage = sessionStorage.Get<string>("Username");
}
}
Usage (Blazor Server)
NOTE: Due to pre-rendering in Blazor Server you can't perform any JS interop until the OnAfterRender
lifecycle method.
@inject Forge.Wasm.BrowserStorages.Services.LocalStorage.ILocalStorageServiceSync localStorage
@inject Forge.Wasm.BrowserStorages.Services.SessionStorage.ISessionStorageServiceSync sessionStorage
@code {
protected override async Task OnAfterRenderAsync(bool firstRender)
{
// localStorage example
await localStorage.Set("Username", "john_doe");
var usernameFromLocalStorage = await localStorage.Get<string>("Username");
// sessionStorage example
await sessionStorage.Set("Username", "john_doe");
var usernameFromSessionStorage = await sessionStorage.Get<string>("Username");
}
}
The following low-level API methods are exposed:
asynchronous via
ILocalStorageServiceAsync
|ISessionStorageServiceAsync
:- ContainsKeyAsync()
- ClearAsync()
- GetAsync()
- GetAsStringAsync()
- LengthAsync()
- KeyAsync()
- RemoveAsync()
- SetAsync()
- SetAsStringAsync()
synchronous via
ILocalStorageServiceSync
|ISessionStorageServiceSync
:- ContainsKey()
- Clear()
- Get()
- GetAsString()
- Length()
- Key()
- Remove()
- Set()
- SetAsString()
Note: LocalStorage \ SessionStorage methods will handle the serialisation and de-serialization of the data for you. Except the SetAsString[Async]
and GetAsString[Async]
methods which are save the string directly and return it as a string value from local \ session storage without using the serialization provider.
Configuring default (built-in) JSON serialization provider options
You can configure the options for the default serialization provider (System.Text.Json) when calling the AddForgeLocalStorage
or AddForgeSessionStorage
method to register services.
public static async Task Main(string[] args)
{
var builder = WebAssemblyHostBuilder.CreateDefault(args);
builder.RootComponents.Add<App>("app");
// for localStorage
builder.Services.AddForgeLocalStorage(config =>
// these are the defaults, this code is for demonstration purposes
config.SerializeOptions.PropertyNamingPolicy = JsonNamingPolicy.CamelCase;
config.DeserializeOptions.PropertyNameCaseInsensitive = true;
);
// for sessionStorage
builder.Services.AddForgeSessionStorage(config =>
// these are the defaults, this code is for demonstration purposes
config.SerializeOptions.PropertyNamingPolicy = JsonNamingPolicy.CamelCase;
config.DeserializeOptions.PropertyNameCaseInsensitive = true;
);
await builder.Build().RunAsync();
}
Using a custom JSON serialization provider
By default, the library uses System.Text.Json
. If you would like to use an other JSON library as serialization provider, you can provide your own serialization provider which implements the Forge.Wasm.BrowserStorages.Serialization.LocalStorage.ISerializationProvider
and/or Forge.Wasm.BrowserStorages.Serialization.SessionStorage.ISerializationProvider
interface.
To register your own serializer in place of the default one, you can do the following:
builder.Services.AddForgeLocalStorage();
builder.Services.Replace(ServiceDescriptor.Scoped<Forge.Wasm.BrowserStorages.Serialization.LocalStorage.ISerializationProvider, CustomSerializer>());
A Newtonsoft.Json serializer implementation for this library at Github created, called Forge.Wasm.BrowserStorages.NewtonSoft.Json
Please also check the following projects in my repositories:
- Forge.Yoda
- Forge.Security.Jwt.Service
- Forge.Security.Jwt.Service.Storage.SqlServer
- Forge.Security.Jwt.Client
- Forge.Security.Jwt.Client.Storage.Browser
- Forge.Wasm.BrowserStorages
- Forge.Wasm.BrowserStorages.NewtonSoft.Json
Product | Versions Compatible and additional computed target framework versions. |
---|---|
.NET | net5.0 was computed. net5.0-windows was computed. net6.0 is compatible. net6.0-android was computed. net6.0-ios was computed. net6.0-maccatalyst was computed. net6.0-macos was computed. net6.0-tvos was computed. net6.0-windows was computed. net7.0 is compatible. net7.0-android was computed. net7.0-ios was computed. net7.0-maccatalyst was computed. net7.0-macos was computed. net7.0-tvos was computed. net7.0-windows was computed. 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 is compatible. 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. |
.NET Core | netcoreapp2.0 was computed. netcoreapp2.1 was computed. netcoreapp2.2 was computed. netcoreapp3.0 was computed. netcoreapp3.1 is compatible. |
.NET Standard | netstandard2.0 is compatible. netstandard2.1 was computed. |
.NET Framework | net461 was computed. net462 was computed. net463 was computed. net47 was computed. net471 was computed. net472 was computed. net48 was computed. net481 was computed. |
MonoAndroid | monoandroid was computed. |
MonoMac | monomac was computed. |
MonoTouch | monotouch was computed. |
Tizen | tizen40 was computed. tizen60 was computed. |
Xamarin.iOS | xamarinios was computed. |
Xamarin.Mac | xamarinmac was computed. |
Xamarin.TVOS | xamarintvos was computed. |
Xamarin.WatchOS | xamarinwatchos was computed. |
-
.NETCoreApp 3.1
- Microsoft.AspNetCore.Components.Web (>= 3.1.32)
-
.NETStandard 2.0
- Microsoft.AspNetCore.Components.Web (>= 3.1.32)
-
net6.0
- Microsoft.AspNetCore.Components.Web (>= 6.0.12)
-
net7.0
- Microsoft.AspNetCore.Components.Web (>= 7.0.0)
-
net8.0
- Microsoft.AspNetCore.Components.Web (>= 8.0.0)
-
net9.0
- Microsoft.AspNetCore.Components.Web (>= 9.0.0)
NuGet packages (2)
Showing the top 2 NuGet packages that depend on Forge.Wasm.BrowserStorages:
Package | Downloads |
---|---|
Forge.Security.Jwt.Client.Storage.Browser
Forge Security Jwt Web Browser storages |
|
Forge.Wasm.BrowserStorages.NewtonSoft.Json
NewtonSoft.Json serializer for Forge.Wasm.BrowserStorages |
GitHub repositories
This package is not used by any popular GitHub repositories.