MobileBlazorBindings
MobileBlazorBindings copied to clipboard
AttachToIpcChannel must be called before using IJSRuntime
What does this error means?
https://github.com/xamarin/MobileBlazorBindings/blob/aa6112e7fae894ebc2f1e0fb1bb2cc6f5bd342ee/src/Microsoft.MobileBlazorBindings.WebView/BlazorHybridJSRuntime.cs#L60
It's an internal error that shouldn't happen! 😄
Can you share how to reproduce this?
That zip is an existing blazor app that I am porting to MBB 😁 it's fully ported but I need to get this error fixed!
On debug the app gets partially rendered but throws that error and crashes on android not tested on iOS ATM 😢
Investigation Note:
It's seems its a bug related with the services that uses the IJSRuntime.
I commented all services that uses IJSRuntime and the issue it's gone.
Take note that the repro I shared here it's a working existing application.
It's seems I have found a few issues also not related to this one, I am enumerating them in the meantime. Also have some ideas for enhancements.
Solution Feedback.
It's seems for some reason but I haven't still went deeper but some libraries that uses jsruntime and interop with some browser specific capabilities or features may raise this error.
In my case was the browser storage so I needed to reimplement this on platform specific storage code for example SecureStorage from xamarin essentials.
I already fixed this issue reimplementing the same patterns and my project is already running after few reimplementation based on the running platform whether is blazor wasm/server or MBB.
I think we can close this issues but I want you to let you know that people may open issues related or with the same title as this one with the same issue.
For Developers hitting this error
Developers needs to be very careful when using the IJSRuntime over MBB and try not to do any interaction with hardware capabilities like browser storage, download file via JS over the browser, instead of this do it on top of c# and xamarin.
I'm re-opening to see if we can at least make this experience a bit better. I think that at a minimum we could have the exception with a better message. But perhaps we can just fix this somehow?
I'm re-opening to see if we can at least make this experience a bit better. I think that at a minimum we could have the exception with a better message. But perhaps we can just fix this somehow?
A descriptive message on where this error ocurred with a stacktrace should be enough.
I've had this error and it usually happens because IJSRuntime is resolved too early (while it isn't initialized yet), or when it is resolved from the wrong scope (the root scope). There are a few WebAssembly libraries out there that trigger this because they were never really tested with anything other than webassembly. (Where most services are registered as singleton instead of scoped). It might be beneficial to investigate the repo and report back to the asp.net core team if there is a bug there.
@jspuij this makes sense, even if I could not debug this when I hit this error, I removed libraries that did some initializations using the IJSRuntime and error dissapeared. The fact is this would not make all js libraries portable as is.
@arivera12 I think if there's a case where an existing JS library doesn't work we would assume at first it is a bug in Mobile Blazor Bindings, and only after investigation make a final determination. There are surely some things that will never work in MBB because they might assume a particular browser behavior that only exists in a full browser and not in a WebView, but I'm hoping that there are very few such scenarios.
Just a head notes.
Every library which uses IJSRuntime it should be scoped or transient.
Singleton or early access of IJSRuntime like in the startup of the app will raise this error.
In Mobile Blazor Bindings it's registered as scoped:
- https://github.com/xamarin/MobileBlazorBindings/blob/master/src/Microsoft.MobileBlazorBindings.WebView/BlazorHybridServiceCollectionExtensions.cs#L18
In Blazor Server it's registered as scoped:
- https://github.com/dotnet/aspnetcore/blob/b795ac3546eb3e2f47a01a64feb3020794ca33bb/src/Components/Server/src/DependencyInjection/ComponentServiceCollectionExtensions.cs#L78
In Blazor WebAssembly (WASM) it's registered as singleton:
- https://github.com/dotnet/aspnetcore/blob/902c3996902fed110ca96d7580a5659aa7c4fa02/src/Components/WebAssembly/WebAssembly/src/Hosting/WebAssemblyHostBuilder.cs#L228
So it seems that for libraries you are correct that it can only be safely consumed from scoped or transient lifetime locations. Singleton will work only in Blazor WASM.
@Eilon Yes it's true I just wanted to contribute to this issue and based on my experience migrating an entire existing blazor wasm app to MBB and it's working properly for now!
I will later give more feedback when the app it's published to the app store.