What could be the cause of this exception error : "Could not load type 'WebAssembly.JSInterop.JSCallInfo'" in Blazor WebAssembly?

327 Views Asked by At

I have created an integration test project to mock API with WireMock.Net library. My application is a Blazor WebAssembly with ASP.NET Core Hosted. However, when I try to debug my test case I get this exception error: "Could not load type 'WebAssembly.JSInterop.JSCallInfo' assembly 'Microsoft.JSInterop.WebAssembly, Version=7.0.2.0". What could be the cause of this error?

Here is my integration test project looks like

public class ContactControllerTests : IClassFixture<WebApplicationFactory<Program>>
{
    public ContactControllerTests(WebApplicationFactory<Program> applicationFactory)
    {
       _httpClient = applicationFactory.CreateDefaultClient();
    }

    [Fact]
    public async Task GetContacts_WhenGetAll_ThenReturnAllContacts()
    {

    }
} 

And in the same project, I have a 'Program.cs' file that has this configuration

using Microsoft.AspNetCore.Components.WebAssembly.Hosting;
using System.Net.Http.Headers;
....//some other usings

var builder = WebAssemblyHostBuilder.CreateDefault(args); //Here is where it throws the exception
builder.RootComponents.Add<App>("#app");

builder.Services.AddScoped(sp => new HttpClient { BaseAddress = new Uri(builder.HostEnvironment.BaseAddress) });

await builder.Build().RunAsync();

public partial class Program { }

With the above configuration, it looks like I am not able to create an in-memory test server that is hosting WebAssembly. I would appreciate any help or feedback.

2

There are 2 best solutions below

2
mw509 On

It could be that the required assembly is not being included in your test project. Verifying that the assembly is referenced in your project's dependencies, and that its Copy Local property is set to True.

It's also possible that the version of the assembly you are referencing in your test project is different from the version that your Blazor WebAssembly application is using. Try updating your test project's Microsoft.JSInterop.WebAssembly package to match the version used by your Blazor WebAssembly application. You can also try specifying the specific version of the package in your test project's dependencies to ensure that the correct version is being used. This specificity of the package can be done from the .csproj file of your project.

lets see if any of these help. if not you may want to perhaps use the WebAssemblyHostBuilder to create an in-memory test server that is hosting WebAssembly.

0
CoolWolf On

Same problem on empty .Net Core 8 WebAssembly project. It was crashing on the following line of the Program.cs file:

var builder = WebAssemblyHostBuilder.CreateDefault(args);

Just add valid json text to the wwwroot/appsettings.json file. For example :

{}

or

{"MySettingValue":1}