Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Bug] OnChange Invoke error on ISyncLocalStorageService .NET 9 #253

Open
B2BSystems opened this issue Dec 18, 2024 · 5 comments
Open

[Bug] OnChange Invoke error on ISyncLocalStorageService .NET 9 #253

B2BSystems opened this issue Dec 18, 2024 · 5 comments
Labels
Bug Something isn't working Triage Issue needs to be triaged

Comments

@B2BSystems
Copy link

B2BSystems commented Dec 18, 2024

Describe the bug
IJSInProcessRuntime not available is thrown when trying to read from local storage.
Developing a cart where I use OnChange event to update the cart item counter. This have worked before with older .NET version.

To Reproduce

@inject Blazored.LocalStorage.ISyncLocalStorageService LocalStorage
@implements IDisposable

....

private int GetCartItemCount()
{
    var cart = LocalStorage.GetItem<List<CartItemDto>>("b_cart"); // ERROR: IJSInProcessRuntime not available
    return cart != null ? cart.Count : 0;
}

// subscribe to the cart changes event
protected override void OnInitialized()
{
    CartService.OnChange += StateHasChanged;
}

public void Dispose()
{
    CartService.OnChange -= StateHasChanged;
}

After adding items to the cart I invoke the OnChange event.

public event Action OnChange;

....

await _localStorage.SetItemAsync("b_cart", cart);
OnChange.Invoke();

Expected behavior
Counter should be updated with number of items in cart.

Hosting Model (is this issue happening with a certain hosting model?):

  • Blazor WebAssembly .NET 9
@B2BSystems B2BSystems added Bug Something isn't working Triage Issue needs to be triaged labels Dec 18, 2024
@chrissainty
Copy link
Member

This error is almost always because of trying to access local storage during a pre-rendering phase. What render mode are you using?

@B2BSystems
Copy link
Author

I haven't configured any special render mode, but as it is a Blazor Webassembly I guess that the default is InteractiveWebAssembly.

@chrissainty
Copy link
Member

Have you upgraded from .NET 8 or an earlier version?

@B2BSystems
Copy link
Author

No, this is a new solution.
But I have used the same code before in .NET 7 where it worked.

@chrissainty
Copy link
Member

There were some major changes in how things worked in .NET 8. It sounds like you might need to have a look into them. It sounds to me like you're possibly hitting the pre-rendering scenario.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug Something isn't working Triage Issue needs to be triaged
Projects
None yet
Development

No branches or pull requests

2 participants