question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Importing a JS module into a .NET MAUI Blazor app fails

See original GitHub issue

Repro steps:

  • Create a .NET MAUI Blazor app
  • Add a Razor Class Library to the solution and reference it
  • Add ExampleJsInterop as a singleton service
builder.Services.AddSingleton<ExampleJsInterop>();
  • Update Index.razor with the following:
@page "/"
@using RazorClassLibrary1
@inject ExampleJsInterop InteropTest

<button @onclick="ShowPrompt">Prompt</button>

@code {
    private async Task ShowPrompt()
    {
        var text = await InteropTest.Prompt("Favorite color?");
    }
}
  • Run the app and click the prompt button

Expected result: Prompt shown

Actual result:

System.NullReferenceException
  HResult=0x80004003
  Message=Object reference not set to an instance of an object.
  Source=Microsoft.AspNetCore.Components.WebView
  StackTrace:
   at Microsoft.AspNetCore.Components.WebView.Services.WebViewJSRuntime.BeginInvokeJS(Int64 taskId, String identifier, String argsJson, JSCallResultType resultType, Int64 targetInstanceId)
   at Microsoft.JSInterop.JSRuntime.InvokeAsync[TValue](Int64 targetInstanceId, String identifier, CancellationToken cancellationToken, Object[] args)
   at Microsoft.JSInterop.JSRuntime.<InvokeAsync>d__16`1.MoveNext()
   at RazorClassLibrary1.ExampleJsInterop.<Prompt>d__2.MoveNext() in C:\Users\daroth\source\repos\MauiApp8\RazorClassLibrary1\ExampleJsInterop.cs:line 23

  This exception was originally thrown at this call stack:
    [External Code]
    RazorClassLibrary1.ExampleJsInterop.Prompt(string) in ExampleJsInterop.cs

This is with .NET 6 and VS2022 17.3.6

Issue Analytics

  • State:closed
  • Created a year ago
  • Comments:5 (5 by maintainers)

github_iconTop GitHub Comments

2reactions
danroth27commented, Nov 3, 2022

Sorry I meant the actual ExampleJsInterop implemention.

That comes from the Razor Class Library template

0reactions
MackinnonBuckcommented, Nov 3, 2022

In MAUI Blazor, IJSRuntime is a scoped service, so it cannot be injected into singleton services.

Closing as by-design.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Blazor Maui JS Isolation
I keep getting the exception. Exception: Failed to fetch dynamically imported module: https://0.0.0.0/_content/MyAppName/js/MyFile.js
Read more >
ASP.NET Core Blazor JavaScript interoperability (JS interop)
Learn how to interact with JavaScript in Blazor apps. ... JavaScript into separate JavaScript modules that can be imported when needed.
Read more >
Blazor startup not working on Android #7965 - dotnet/maui
For completeness, I'll show how it should work. And in your app you would add a new file wwwroot/MauiApp1.lib.module.js (but change MauiApp1 to...
Read more >
Blazor Maui project calling JS function in Razor component ...
I created a simple Blazor MAUI project. I added a simple Razor class library, which contain two JavaScript files, a simple razor component...
Read more >
Pros and Cons of Using JavaScript Interop in Blazor
This blog post explains using JavaScript interop concepts in the Blazor framework, its advantages, and its limitations with code examples.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found