2

Two DIFFERENT Blazor components define the following instance method:

[JSInvokable]
public void MyInstanceMethod()
{
...
}

At load time they call a js function, passing themselves to js:

await JS.InvokeAsync<object>("jsFunction", new DotNetObjectRef(this));

In js, the passed .NET object reference is saved in a variable named _callback. Later, an event occurring in javascript calls back the instance method

_callback.invokeMethodAsync("MyInstanceMethod");

The browser console fails with the following error:

blazor.webassembly.js:1 Uncaught (in promise) Error: 
System.InvalidOperationException: 
The assembly 'WebApplication7.Client' contains more than one [JSInvokable] method 
with identifier 'MyInstanceMethod'.
All [JSInvokable] methods within the same assembly must have different identifiers.

BTW, everything goes well if only one component has the method.

Isn't this a violation of one of the fundamental scope features in any OO language?

Can anybody tell me why methods, including instance methods, are required to have different identifiers to be JSInvokable?

If this is a limit of Blazor, is there a plan to fix it?

3
  • This is a limitation of Blazor, I suppose... Why don't you address this post to Steve Anderson in github ? This is a real issue I've never given it a thought. Commented Jun 2, 2019 at 7:25
  • The caller (JS) probably isn't aware of which component is considered "active" - perhaps a [JSInvokable("MyComponent.MyInstanceMethod")] overload would solve part of the issue for you? Commented Jun 3, 2019 at 13:25
  • I've copied the post to github github.com/aspnet/AspNetCore/issues/11131 Commented Jun 12, 2019 at 5:54

1 Answer 1

3

Looks like there is a bug in Blazor. Steve Sanderson:


Sounds like a bug. Thanks for reporting it.

It makes sense to require static methods to have assembly-wide unique identifiers. However it doesn't make sense that instance methods need assembly-wide unique identifiers. We should only require the identifier to be unique within the type of the instance you're passing.

As a workaround, you could do something like:

[JSInvokable("SomeUniqueIdentifier")]
public void MyInstanceMethod()
{
...
}

I know it's annoying to need a workaround like this. We'll fix the underlying bug (most likely after 3.0 ships).

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.