Quantcast
Channel: Active questions tagged blazor - Stack Overflow
Viewing all articles
Browse latest Browse all 4839

Blazer Server: Trying to call a .net method from JavaScript using window,DotNet always give the error 'No call dispatcher has been set'

$
0
0

I am trying to call a .net method from some JavaScript in my Blazer server application, following this documentation but I always get No call dispatcher has been set.

I want to have the .net methods in a service that is always there, NOT a component (I find the Blazer components always seem to dispose themselves). For example I will use for logging.

I have declared this (everything static)....

namespace MyNamespace.Utility{  public static class JsHelperService  {    static JsHelperService()    {      var i = 0; // just for testing with breakpoint    }    // Call from program.cs to make sure class is instantiated    public static void Init()    {    }    [JSInvokable("WriteInfo")]    public static void WriteInfo(string message)    {      Logger.Instance.WriteInfo(message);    }  }}

Then in JavaScript I have

function setupLogging() {    debugger; // just while debugging           window.dotnetLogger = window.dotnetLogger || {};    window.dotnetLogger.writeInfo = function (message) {        window.DotNet.invokeMethodAsync('MyNamespace.JsHelperService', 'WriteInfo');    };   }

I then call setupLogging() to set this up.

When I debug, all seems to be there, ie window.DotNet is available etc.

However, when I then call window.dotnetLogger.writeInfo from elsewhere in JavaScript, I can see the function being called, but as soon as it executes the

window.DotNet.invokeMethodAsync

I get the error

Uncaught Error: No call dispatcher has been set.  at v (blazor.web.js:1:1776)  at e.invokeMethodAsync (blazor.web.js:1:2231)  at window.dotnetLogger.writeInfo (site.js:41:18)

I was not sure what to use for the namespace, we the doco does not even have an example, so I tried both MyNamespace.JsHelperService and just MyNamespace but neiother made a difference.

Would anyone know how I can fix this?


Viewing all articles
Browse latest Browse all 4839

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>