Importing a JS module into a .NET MAUI Blazor app fails
See original GitHub issueRepro 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:
- Created a year ago
- Comments:5 (5 by maintainers)
Top 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 >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found

That comes from the Razor Class Library template
In MAUI Blazor,
IJSRuntimeis a scoped service, so it cannot be injected into singleton services.Closing as by-design.