MobileBlazorBindings icon indicating copy to clipboard operation
MobileBlazorBindings copied to clipboard

AttachToIpcChannel must be called before using IJSRuntime

Open arivera12 opened this issue 5 years ago • 16 comments
trafficstars

What does this error means?

arivera12 avatar Nov 05 '20 05:11 arivera12

https://github.com/xamarin/MobileBlazorBindings/blob/aa6112e7fae894ebc2f1e0fb1bb2cc6f5bd342ee/src/Microsoft.MobileBlazorBindings.WebView/BlazorHybridJSRuntime.cs#L60

arivera12 avatar Nov 05 '20 05:11 arivera12

It's an internal error that shouldn't happen! 😄

Can you share how to reproduce this?

Eilon avatar Nov 05 '20 20:11 Eilon

Repro.zip

arivera12 avatar Nov 06 '20 04:11 arivera12

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!

arivera12 avatar Nov 06 '20 13:11 arivera12

On debug the app gets partially rendered but throws that error and crashes on android not tested on iOS ATM 😢

arivera12 avatar Nov 06 '20 13:11 arivera12

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.

arivera12 avatar Nov 06 '20 16:11 arivera12

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.

arivera12 avatar Nov 06 '20 23:11 arivera12

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.

arivera12 avatar Nov 09 '20 16:11 arivera12

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?

Eilon avatar Nov 09 '20 17:11 Eilon

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.

arivera12 avatar Nov 09 '20 19:11 arivera12

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 avatar Nov 23 '20 08:11 jspuij

@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 avatar Nov 23 '20 13:11 arivera12

@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.

Eilon avatar Nov 23 '20 19:11 Eilon

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.

arivera12 avatar Jan 20 '21 17:01 arivera12

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 avatar Jan 20 '21 22:01 Eilon

@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.

arivera12 avatar Jan 21 '21 01:01 arivera12