{"id":19175428,"url":"https://github.com/jzo001/forge.wasm.browserstorages","last_synced_at":"2026-04-18T01:32:55.718Z","repository":{"id":61721675,"uuid":"552465245","full_name":"JZO001/Forge.Wasm.BrowserStorages","owner":"JZO001","description":"This library provides access to the browsers local and session storage APIs for WASM applications","archived":false,"fork":false,"pushed_at":"2023-05-25T14:22:48.000Z","size":31,"stargazers_count":1,"open_issues_count":0,"forks_count":0,"subscribers_count":2,"default_branch":"main","last_synced_at":"2025-02-04T08:42:49.071Z","etag":null,"topics":["blazor","localstorage","localstorage-api","provider","serialization","sessionstorage","sessionstorage-api","wasm","webassembly"],"latest_commit_sha":null,"homepage":"","language":"C#","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/JZO001.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2022-10-16T16:53:19.000Z","updated_at":"2024-06-28T13:47:07.000Z","dependencies_parsed_at":"2022-10-20T11:45:37.196Z","dependency_job_id":null,"html_url":"https://github.com/JZO001/Forge.Wasm.BrowserStorages","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/JZO001%2FForge.Wasm.BrowserStorages","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/JZO001%2FForge.Wasm.BrowserStorages/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/JZO001%2FForge.Wasm.BrowserStorages/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/JZO001%2FForge.Wasm.BrowserStorages/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/JZO001","download_url":"https://codeload.github.com/JZO001/Forge.Wasm.BrowserStorages/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":240254181,"owners_count":19772386,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["blazor","localstorage","localstorage-api","provider","serialization","sessionstorage","sessionstorage-api","wasm","webassembly"],"created_at":"2024-11-09T10:23:13.927Z","updated_at":"2026-04-18T01:32:55.651Z","avatar_url":"https://github.com/JZO001.png","language":"C#","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Forge.Wasm.BrowserStorages\nForge.Wasm.BrowserStorages is a library that provides access to the browsers local and session storage APIs for WASM applications.\n\n\n## Installing\n\nTo install the package add the following line to you csproj file replacing x.x.x with the latest version number:\n\n```\n\u003cPackageReference Include=\"Forge.Wasm.BrowserStorages\" Version=\"x.x.x\" /\u003e\n```\n\nYou can also install via the .NET CLI with the following command:\n\n```\ndotnet add package Forge.Wasm.BrowserStorages\n```\n\nIf you're using Visual Studio you can also install via the built in NuGet package manager.\n\n## Setup\n\nYou will need to register the local storage services with the service collection in your _Startup.cs_ file in Blazor Server.\n\n```c#\npublic void ConfigureServices(IServiceCollection services)\n{\n    services.AddForgeLocalStorage();\n    services.AddForgeSessionStorage();\n}\n``` \n\nOr in your _Program.cs_ file in Blazor WebAssembly.\n\n```c#\npublic static async Task Main(string[] args)\n{\n    var builder = WebAssemblyHostBuilder.CreateDefault(args);\n    builder.RootComponents.Add\u003cApp\u003e(\"app\");\n\n    builder.Services.AddForgeLocalStorage();\n    builder.Services.AddForgeSessionStorage();\n\n    await builder.Build().RunAsync();\n}\n```\n\nIn the case, if IJSInProcessRuntime does not available in your project type, you can not use the syncronized version of the storage.\nIt is possible to skip the registration of the sync providers and services which are requires the implementation of this interface,\nuse the following code to archieve it:\n\n```c#\npublic static async Task Main(string[] args)\n{\n    var builder = WebAssemblyHostBuilder.CreateDefault(args);\n    builder.RootComponents.Add\u003cApp\u003e(\"app\");\n\n    builder.Services.AddForgeLocalStorageAsyncOnly();\n    builder.Services.AddForgeSessionStorageAsyncOnly();\n\n    await builder.Build().RunAsync();\n}\n```\n\n\n### Registering services as Singleton\nIf you would like to register LocalStorage or SessionStorage services as singletons, it is possible by using the following method:\n\n```csharp\nbuilder.Services.AddForgeLocalStorageAsSingleton();\nbuilder.Services.AddForgeSessionStorageAsSingleton();\n```\n\nOr\n\n```csharp\nbuilder.Services.AddForgeLocalStorageAsyncOnlyAsSingleton();\nbuilder.Services.AddForgeSessionStorageAsyncOnlyAsSingleton();\n```\n\nThis method is not recommended in the most cases, try to avoid using it.\n\n\n## Usage\nTo use Forge.Wasm.BrowserStorages in Blazor WebAssembly, inject the `ILocalStorageServiceAsync` | `ISessionStorageServiceAsync` per the example below.\n\n```c#\n@inject Forge.Wasm.BrowserStorages.Services.LocalStorage.ILocalStorageServiceAsync localStorage\n@inject Forge.Wasm.BrowserStorages.Services.SessionStorage.ISessionStorageServiceAsync sessionStorage\n\n@code {\n\n    protected override async Task OnInitializedAsync()\n    {\n        await localStorage.SetAsync(\"Username\", \"johndoe\");\n        var usernameFromLocalStorage = await localStorage.GetAsync\u003cstring\u003e(\"Username\");\n\n        await sessionStorage.SetAsync(\"Username\", \"johndoe\");\n        var usernameFromSessionStorage = await sessionStorage.GetAsync\u003cstring\u003e(\"Username\");\n    }\n\n}\n```\n\nIt is possible to use synchonous version of API. Change the `ILocalStorageServiceAsync` for `ILocalStorageServiceSync` which allows you to avoid use of `async`/`await`.\nPlease keep it in mind, this is not the recommended way. Other issue can be, if the IJSInProcessRuntime is not available in your environment. \nIf this is the case, you cannot use synchronous version of storage providers. \nIt 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.\n\n```c#\n@inject Forge.Wasm.BrowserStorages.Services.LocalStorage.ILocalStorageServiceAsync localStorage\n@inject Forge.Wasm.BrowserStorages.Services.SessionStorage.ISessionStorageServiceAsync sessionStorage\n\n@code {\n\n    protected override void OnInitialized()\n    {\n        localStorage.Set(\"Username\", \"johndoe\");\n        var usernameFromLocalStorage = localStorage.Get\u003cstring\u003e(\"Username\");\n\n        sessionStorage.Set(\"Username\", \"johndoe\");\n        var usernameFromSessionStorage = sessionStorage.Get\u003cstring\u003e(\"Username\");\n    }\n\n}\n```\n\n## Usage (Blazor Server)\n\n**NOTE:** Due to pre-rendering in Blazor Server you can't perform any JS interop until the `OnAfterRender` lifecycle method.\n\n```c#\n@inject Forge.Wasm.BrowserStorages.Services.LocalStorage.ILocalStorageServiceSync localStorage\n@inject Forge.Wasm.BrowserStorages.Services.SessionStorage.ISessionStorageServiceSync sessionStorage\n\n@code {\n\n    protected override async Task OnAfterRenderAsync(bool firstRender)\n    {\n\t// localStorage example\n        await localStorage.Set(\"Username\", \"john_doe\");\n        var usernameFromLocalStorage = await localStorage.Get\u003cstring\u003e(\"Username\");\n\n\t// sessionStorage example\n        await sessionStorage.Set(\"Username\", \"john_doe\");\n        var usernameFromSessionStorage = await sessionStorage.Get\u003cstring\u003e(\"Username\");\n    }\n\n}\n```\n\nThe following low-level API methods are exposed:\n\n- asynchronous via `ILocalStorageServiceAsync` | `ISessionStorageServiceAsync`:\n  - ContainsKeyAsync()\n  - ClearAsync()\n  - GetAsync()\n  - GetAsStringAsync()\n  - LengthAsync()\n  - KeyAsync()\n  - RemoveAsync()\n  - SetAsync()\n  - SetAsStringAsync()\n  \n- synchronous via `ILocalStorageServiceSync` | `ISessionStorageServiceSync`:\n  - ContainsKey()\n  - Clear()\n  - Get()\n  - GetAsString()\n  - Length()\n  - Key()\n  - Remove()\n  - Set()\n  - SetAsString()\n\n**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.\n\n## Configuring default (built-in) JSON serialization provider options\nYou can configure the options for the default serialization provider (System.Text.Json) when calling the `AddForgeLocalStorage` or `AddForgeSessionStorage` method to register services.\n\n```c#\npublic static async Task Main(string[] args)\n{\n    var builder = WebAssemblyHostBuilder.CreateDefault(args);\n    builder.RootComponents.Add\u003cApp\u003e(\"app\");\n\n    // for localStorage\n    builder.Services.AddForgeLocalStorage(config =\u003e\n\t// these are the defaults, this code is for demonstration purposes\n        config.SerializeOptions.PropertyNamingPolicy = JsonNamingPolicy.CamelCase;\n        config.DeserializeOptions.PropertyNameCaseInsensitive = true;\n    );\n\n    // for sessionStorage\n    builder.Services.AddForgeSessionStorage(config =\u003e\n\t// these are the defaults, this code is for demonstration purposes\n        config.SerializeOptions.PropertyNamingPolicy = JsonNamingPolicy.CamelCase;\n        config.DeserializeOptions.PropertyNameCaseInsensitive = true;\n    );\n\n    await builder.Build().RunAsync();\n}\n```\n\n## Using a custom JSON serialization provider\nBy 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.\n\nTo register your own serializer in place of the default one, you can do the following:\n\n```csharp\nbuilder.Services.AddForgeLocalStorage();\nbuilder.Services.Replace(ServiceDescriptor.Scoped\u003cForge.Wasm.BrowserStorages.Serialization.LocalStorage.ISerializationProvider, CustomSerializer\u003e());\n```\n\nA Newtonsoft.Json serializer implementation for this library at Github created, called Forge.Wasm.BrowserStorages.NewtonSoft.Json\n\nPlease also check the following projects in my repositories:\n- Forge.Yoda\n- Forge.Security.Jwt.Service\n- Forge.Security.Jwt.Service.Storage.SqlServer\n- Forge.Security.Jwt.Client\n- Forge.Security.Jwt.Client.Storage.Browser\n- Forge.Wasm.BrowserStorages\n- Forge.Wasm.BrowserStorages.NewtonSoft.Json\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjzo001%2Fforge.wasm.browserstorages","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fjzo001%2Fforge.wasm.browserstorages","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjzo001%2Fforge.wasm.browserstorages/lists"}