Blazored / LocalStorage

A library to provide access to local storage in Blazor applications
https://blazored.github.io/LocalStorage/
MIT License
1.2k stars 118 forks source link

`IJSInProcessRuntime not available` on SetItem in Razor Components #10

Closed ransagy closed 5 years ago

ransagy commented 5 years ago

First of all, A little background: This is an app that started as a standalone Blazor project. I later added a Server-Side Blazor project to call into the standalone one (as a way to debug things locally before i publish the standalone one). I upgraded those throughout the Blazor versions and now into 0.9. It's quite likely i am doing something bad/unsupported/broken.

I have a login component that gets injected with ILocalStorageService and attempts to call SetItem on a successful login (<form onsubmit="async () => await DoStuffAndCallStorage()").

This throws an exception as such:

System.InvalidOperationException: IJSInProcessRuntime not available
   at Blazored.LocalStorage.LocalStorageService.Blazored.LocalStorage.ISyncLocalStorageService.GetItem[T](String key)
   at Blazored.LocalStorage.LocalStorageService.GetItem(String key)
   at Blazored.LocalStorage.LocalStorageService.RaiseOnChanging(String key, Object data, ChangingEventArgs& e)
   at Blazored.LocalStorage.LocalStorageService.SetItem(String key, Object data)

This used to work in 1.0.1, But quite a few things changed in this library since. I believe this is related to these changes:

https://github.com/Blazored/LocalStorage/blob/58b1c5577adf4f7d5b251cb24e3b0a2cecc72825/src/Blazored.LocalStorage/LocalStorageService.cs#L135

https://github.com/Blazored/LocalStorage/blob/58b1c5577adf4f7d5b251cb24e3b0a2cecc72825/src/Blazored.LocalStorage/LocalStorageService.cs#L127

.. where the call to GetItem inside the event handler is forcing the usage of the sync version, Which obviously fails if you're in an async scenario/didn't inject the sync version on purpose to begin with.

chrissainty commented 5 years ago

This should be fixed by #11 @ransagy