Tewr / BlazorWorker

Library for creating DotNet Web Worker threads/multithreading in Client side Blazor
MIT License
377 stars 34 forks source link

Add to documentation: Method called via LINQ expression must not be internal #108

Closed PhilipEve closed 5 months ago

PhilipEve commented 6 months ago

I just spent an hour and a half trying to figure out why I wasn't able to call a particular method on my background service (my IWorkerBackgroundService) without getting an incomprehensible error message. The long and the short of it is that the method DoThing() in your code that looks like this:

_workerBackgroundService.RunAsync(s => s.DoThing());

... can't be an internal method. Why did I make it an internal method? Because I was incrementally adding more parameters as I made sure they (de)serialized correctly, and at some point I had a parameter that was an internal type, and when I got the "inconsistent visibility" error I made the method internal in order to resolve it.

If DoThing() is an internal method, then when you try to run it, you'll get a long error message the first few lines of which look like this:

Uncaught ManagedError 
    at Jn (path-to-your-project\Server\_framework\https:\raw.githubusercontent.com\dotnet\runtime\9f4b1f5d664afdfc80e1508ab7ed099dff210fbd\src\mono\wasm\runtime\marshal-to-js.ts:349:18)
    at kr (path-to-your-project\Server\_framework\https:\raw.githubusercontent.com\dotnet\runtime\9f4b1f5d664afdfc80e1508ab7ed099dff210fbd\src\mono\wasm\runtime\invoke-cs.ts:277:19)
    at <anonymous> (path-to-your-project\Server\_framework\https:\raw.githubusercontent.com\dotnet\runtime\9f4b1f5d664afdfc80e1508ab7ed099dff210fbd\src\mono\wasm\runtime\invoke-cs.ts:247:13)
    at invokeDotNetFromJS (localhost꞉52925/_framework/blazor.webassembly.js:1:45370)
    at invokeDotNetMethod (localhost꞉52925/_framework/blazor.webassembly.js:1:3485)
    at invokeMethod (localhost꞉52925/_framework/blazor.webassembly.js:1:5372)
    at worker.onmessage (https://localhost:52925/f9f62d0a-1c37-4a60-978b-81bf968ebf17:267:30)

To save other people the aggro I had, I'd like to suggest that this restriction should be mentioned somewhere in the documentation.

PhilipEve commented 6 months ago

Section "BlazorWorker.BackgroundService" reads in part as follows:

The public methods that you expose in your service can then be called from the IWorkerBackgroundService interface.

So you could argue that this is already mentioned. However, the qualifier "public" in this sentence is easily missed or dismissed. And if you make the mistake of trying to call an internal method rather than a public one, there's nothing to clue you in to what is wrong. The error message you get certainly isn't any help.

Tewr commented 6 months ago

Hello, thank you for your feedback and interest in the project.

This error is not caught explicitly anywhere and I can't remember what causes it, probably a prerequisite of the Serialize.Linq library serialization.

Tewr commented 6 months ago

There might be a bug here. At least a functional bug. Not the fact that we cannot serialize internal method calls, that is expected behavior. But I would like to see some kind of exception here that closes the message loop and it appears that the message loop breaks instead