Blazored.LocalStorage 4.5.0
Blazored LocalStorage
Blazored LocalStorage is a library that provides access to the browsers local storage APIs for Blazor applications. An additional benefit of using this library is that it will handle serializing and deserializing values when saving or retrieving them.
Breaking Changes (v3 > v4)
JsonSerializerOptions
From v4 onwards we use the default the JsonSerializerOptions
for System.Text.Json
instead of using custom ones. This will cause values saved to local storage with v3 to break things.
To retain the old settings use the following configuration when adding Blazored LocalStorage to the DI container:
builder.Services.AddBlazoredLocalStorage(config =>
{
config.JsonSerializerOptions.DictionaryKeyPolicy = JsonNamingPolicy.CamelCase;
config.JsonSerializerOptions.DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull;
config.JsonSerializerOptions.IgnoreReadOnlyProperties = true;
config.JsonSerializerOptions.PropertyNameCaseInsensitive = true;
config.JsonSerializerOptions.PropertyNamingPolicy = JsonNamingPolicy.CamelCase;
config.JsonSerializerOptions.ReadCommentHandling = JsonCommentHandling.Skip;
config.JsonSerializerOptions.WriteIndented = false;
});
SetItem[Async] method now serializes string values
Prior to v4 we bypassed the serialization of string values as it seemed a pointless as string can be stored directly. However, this led to some edge cases where nullable strings were being saved as the string "null"
. Then when retrieved, instead of being null the value was "null"
. By serializing strings this issue is taken care of.
For those who wish to save raw string values, a new method SetValueAsString[Async]
is available. This will save a string value without attempting to serialize it and will throw an exception if a null string is attempted to be saved.
Installing
To install the package add the following line to you csproj file replacing x.x.x with the latest version number (found at the top of this file):
<PackageReference Include="Blazored.LocalStorage" Version="x.x.x" />
You can also install via the .NET CLI with the following command:
dotnet add package Blazored.LocalStorage
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.AddBlazoredLocalStorage();
}
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.AddBlazoredLocalStorage();
await builder.Build().RunAsync();
}
Registering services as Singleton - Blazor WebAssembly ONLY
99% of developers will want to register Blazored LocalStorage using the method described above. However, in some very specific scenarios developer may have a need to register services as Singleton as apposed to Scoped. This is possible by using the following method:
builder.Services.AddBlazoredLocalStorageAsSingleton();
This method will not work with Blazor Server applications as Blazor's JS interop services are registered as Scoped and cannot be injected into Singletons.
Usage (Blazor WebAssembly)
To use Blazored.LocalStorage in Blazor WebAssembly, inject the ILocalStorageService
per the example below.
@inject Blazored.LocalStorage.ILocalStorageService localStorage
@code {
protected override async Task OnInitializedAsync()
{
await localStorage.SetItemAsync("name", "John Smith");
var name = await localStorage.GetItemAsync<string>("name");
}
}
With Blazor WebAssembly you also have the option of a synchronous API, if your use case requires it. You can swap the ILocalStorageService
for ISyncLocalStorageService
which allows you to avoid use of async
/await
. For either interface, the method names are the same.
@inject Blazored.LocalStorage.ISyncLocalStorageService localStorage
@code {
protected override void OnInitialized()
{
localStorage.SetItem("name", "John Smith");
var name = localStorage.GetItem<string>("name");
}
}
Usage (Blazor Server)
NOTE: Due to pre-rendering in Blazor Server you can't perform any JS interop until the OnAfterRender
lifecycle method.
@inject Blazored.LocalStorage.ILocalStorageService localStorage
@code {
protected override async Task OnAfterRenderAsync(bool firstRender)
{
await localStorage.SetItemAsync("name", "John Smith");
var name = await localStorage.GetItemAsync<string>("name");
}
}
The APIs available are:
asynchronous via
ILocalStorageService
:- SetItemAsync()
- SetItemAsStringAsync()
- GetItemAsync()
- GetItemAsStringAsync()
- RemoveItemAsync()
- ClearAsync()
- LengthAsync()
- KeyAsync()
- ContainKeyAsync()
synchronous via
ISyncLocalStorageService
(Synchronous methods are only available in Blazor WebAssembly):- SetItem()
- SetItemAsString()
- GetItem()
- GetItemAsString()
- RemoveItem()
- Clear()
- Length()
- Key()
- ContainKey()
Note: Blazored.LocalStorage methods will handle the serialisation and de-serialisation of the data for you, the exceptions are the SetItemAsString[Async]
and GetItemAsString[Async]
methods which will save and return raw string values from local storage.
Configuring JSON Serializer Options
You can configure the options for the default serializer (System.Text.Json) when calling the AddBlazoredLocalStorage
method to register services.
public static async Task Main(string[] args)
{
var builder = WebAssemblyHostBuilder.CreateDefault(args);
builder.RootComponents.Add<App>("app");
builder.Services.AddBlazoredLocalStorage(config =>
config.JsonSerializerOptions.WriteIndented = true
);
await builder.Build().RunAsync();
}
Using a custom JSON serializer
By default, the library uses System.Text.Json
. If you prefer to use a different JSON library for serialization--or if you want to add some custom logic when serializing or deserializing--you can provide your own serializer which implements the Blazored.LocalStorage.Serialization.IJsonSerializer
interface.
To register your own serializer in place of the default one, you can do the following:
builder.Services.AddBlazoredLocalStorage();
builder.Services.Replace(ServiceDescriptor.Scoped<IJsonSerializer, MySerializer>());
You can find an example of this in the Blazor Server sample project. The standard serializer has been replaced with a new serializer which uses NewtonsoftJson.
Testing with bUnit
The Blazored.LocalStorage.TestExtensions
package provides test extensions for use with the bUnit testing library. Using these test extensions will provide an in memory implementation which mimics local storage allowing more realistic testing of your components.
Installing
To install the package add the following line to you csproj file replacing x.x.x with the latest version number (found at the top of this file):
<PackageReference Include="Blazored.LocalStorage.TestExtensions" Version="x.x.x" />
You can also install via the .NET CLI with the following command:
dotnet add package Blazored.LocalStorage.TestExtensions
If you're using Visual Studio you can also install via the built in NuGet package manager.
Usage example
Below is an example test which uses these extensions. You can find an example project which shows this code in action in the samples folder.
public class IndexPageTests : TestContext
{
[Fact]
public async Task SavesNameToLocalStorage()
{
// Arrange
const string inputName = "John Smith";
var localStorage = this.AddBlazoredLocalStorage();
var cut = RenderComponent<BlazorWebAssembly.Pages.Index>();
// Act
cut.Find("#Name").Change(inputName);
cut.Find("#NameButton").Click();
// Assert
var name = await localStorage.GetItemAsync<string>("name");
Assert.Equal(inputName, name);
}
}
No packages depend on Blazored.LocalStorage.
.NET 6.0
- Microsoft.AspNetCore.Components.Web (>= 6.0.0)
.NET 7.0
- Microsoft.AspNetCore.Components.Web (>= 7.0.0)
.NET 8.0
- Microsoft.AspNetCore.Components.Web (>= 8.0.0)
Version | Downloads | Last updated |
---|---|---|
4.5.0 | 5 | 05/28/2024 |
4.4.0 | 4 | 05/26/2024 |
4.3.0 | 4 | 05/28/2024 |
4.3.0-preview.1 | 5 | 04/10/2024 |
4.2.0 | 6 | 04/04/2024 |
4.1.5 | 3 | 05/28/2024 |
4.1.4 | 4 | 05/28/2024 |
4.1.2 | 4 | 05/28/2024 |
4.1.1 | 3 | 05/28/2024 |
4.0.0 | 3 | 05/28/2024 |
3.0.0 | 5 | 05/08/2024 |
2.2.0 | 5 | 05/28/2024 |
2.1.6 | 3 | 05/28/2024 |
2.1.5 | 4 | 05/28/2024 |
2.1.4 | 4 | 05/28/2024 |
2.1.3 | 7 | 05/28/2024 |
2.1.1 | 7 | 05/28/2024 |
2.1.0 | 7 | 05/06/2024 |
2.0.14 | 5 | 05/28/2024 |
2.0.13 | 4 | 05/28/2024 |
2.0.12 | 3 | 05/28/2024 |
2.0.11 | 4 | 05/28/2024 |
2.0.10 | 4 | 05/28/2024 |
2.0.9 | 4 | 05/28/2024 |
2.0.8 | 4 | 05/28/2024 |
2.0.7 | 6 | 05/28/2024 |
2.0.6 | 4 | 04/06/2024 |
2.0.5 | 6 | 05/28/2024 |
2.0.3 | 6 | 05/28/2024 |
2.0.2 | 5 | 05/28/2024 |
2.0.1 | 4 | 05/28/2024 |
2.0.0 | 7 | 04/02/2024 |
1.2.1 | 5 | 05/28/2024 |
1.2.0 | 3 | 05/28/2024 |
1.1.0 | 6 | 05/28/2024 |
1.0.4 | 4 | 05/28/2024 |
1.0.3 | 3 | 05/28/2024 |
1.0.2 | 6 | 05/28/2024 |
1.0.1 | 4 | 05/03/2024 |
1.0.0 | 4 | 05/28/2024 |