Class LocalStorageService
Service for accessing browser local storage with fallback to in-memory cache.
Inherited Members
Namespace: OEMS.UI.Auth
Assembly: OEMS.UI.dll
Syntax
public class LocalStorageService
Remarks
This service provides a reliable way to use browser localStorage while handling:
- Prerendering scenarios where JS interop isn't available
- Fallback to in-memory storage when browser storage is unavailable
- Proper error handling for storage operations
Constructors
LocalStorageService(IJSRuntime)
Initializes a new instance of the local storage service.
Declaration
public LocalStorageService(IJSRuntime jsRuntime)
Parameters
Type | Name | Description |
---|---|---|
IJSRuntime | jsRuntime | JavaScript runtime for accessing browser storage |
Properties
IsInitialized
Gets a value indicating whether the service has been initialized.
Declaration
public bool IsInitialized { get; }
Property Value
Type | Description |
---|---|
bool |
Remarks
This property is useful to determine if localStorage has been successfully accessed at least once.
Methods
GetItemAsync(string)
Retrieves an item from local storage.
Declaration
public Task<string> GetItemAsync(string key)
Parameters
Type | Name | Description |
---|---|---|
string | key | The key of the item to retrieve |
Returns
Type | Description |
---|---|
Task<string> | The value stored under the key, or null if not found |
Remarks
This method attempts to get the value from browser localStorage first, falling back to the in-memory cache if JS interop is not available.
RemoveItemAsync(string)
Removes an item from local storage.
Declaration
public Task<bool> RemoveItemAsync(string key)
Parameters
Type | Name | Description |
---|---|---|
string | key | The key of the item to remove |
Returns
Type | Description |
---|---|
Task<bool> | True if the operation succeeded, otherwise false |
Remarks
This method attempts to remove the value from browser localStorage first, falling back to the in-memory cache if JS interop is not available.
SetItemAsync(string, string)
Stores an item in local storage.
Declaration
public Task<bool> SetItemAsync(string key, string value)
Parameters
Type | Name | Description |
---|---|---|
string | key | The key under which to store the value |
string | value | The value to store |
Returns
Type | Description |
---|---|
Task<bool> | True if the operation succeeded, otherwise false |
Remarks
This method attempts to store the value in browser localStorage first, falling back to the in-memory cache if JS interop is not available.