12

We use Blazor WebAssembly and I want to call an non-static method in my Index.razor-File by JavaScript.

JavaScript:

(function () {

    // keydown event
    window.addEventListener("keydown", function (e) {
        DotNet.invokeMethodAsync('MyBlazorWebAssemblyApp.Client', 'MyMethod');
    });
})();

Index.razor:

@page "/"
@inject HttpClient Http

@code {
    
    // [...]
    
    [JSInvokable]
    public async Task MyMethod()
    {
        var lResponse = await Http.GetFromJsonAsync<object>("Controller/Action");
    }
}

When I execute the code by an keydown, then the developer tools in Microsoft Edge shows me the following error:

blazor.webassembly.js:1 System.ArgumentException: The assembly 'MyBlazorWebAssemblyApp.Client' does not contain a public invokable method with [JSInvokableAttribute("MyMethod")].

When I replace the attribute [JSInvokable] by [JSInvokableAttribute("MyMethod")] then the same error appears.

How can I fix this problem?

Simon
  • 4,157
  • 2
  • 46
  • 87
  • https://learn.microsoft.com/en-us/aspnet/core/blazor/call-dotnet-from-javascript?view=aspnetcore-3.1#instance-method-call – agua from mars Jul 17 '20 at 13:35
  • What happens if you just use MyBlazorWebAssemblyApp i.e. remove the .Client from the app assembly argument? – Mark3308 Jul 17 '20 at 14:55
  • @Mark3308: System.ArgumentException: There is no loaded assembly with the name 'MyBlazorWebAssemblyApp'. – Simon Jul 20 '20 at 07:33

3 Answers3

19

Got it working now by my self. Here is the code:

JavaScript:

var GLOBAL = {};
GLOBAL.DotNetReference = null;
GLOBAL.SetDotnetReference = function (pDotNetReference) {
    GLOBAL.DotNetReference = pDotNetReference;
};

(function () {

    // keydown event
    window.addEventListener("keydown", function (e) {
        GLOBAL.DotNetReference.invokeMethodAsync('MyMethod');
    });
})();

Index.razor:

@page "/"
@inject HttpClient Http

@code {
    
    protected override async Task OnInitializedAsync()
    {
        var lDotNetReference = DotNetObjectReference.Create(this);
        JSRuntime.InvokeVoidAsync("GLOBAL.SetDotnetReference", lDotNetReference);
    }
    
    // [...]
    
    [JSInvokable("MyMethod")]
    public async Task MyMethod()
    {
        var lResponse = await Http.GetFromJsonAsync<object>("Controller/Action");
    }
}
Ogglas
  • 62,132
  • 37
  • 328
  • 418
Simon
  • 4,157
  • 2
  • 46
  • 87
2

If you check out the Microsoft documentation at the following link:

https://learn.microsoft.com/en-gb/aspnet/core/blazor/call-dotnet-from-javascript?view=aspnetcore-3.1#instance-method-call

This differs from your example in the following ways:

Your JavaScript appears to be trying to call a static .NET method.

Your C# code is not a static method.

Mark3308
  • 1,298
  • 11
  • 22
2

You must mention <<Namespace>>.<<TypeName>>.<<MethodName>>

Important: C# Method must static

window.addEventListener("keydown", async function (e) {
        await DotNet.invokeMethodAsync('MyBlazorWebAssemblyApp.Client', 'MyMethod');
});

#40110 issue

Zanyar Jalal
  • 1,407
  • 12
  • 29