Click here to Skip to main content
16,022,060 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a blazor asp.net core application. In my Hostcshtml.file body tag
i have this script added to it.

<script src="_framework/blazor.server.js" autostart="false"></script>

<script>
    Blazor.start().then(() => {
        Blazor.defaultReconnectionHandler._reconnectCallback = function (d) {
            // Invoke C# method from JavaScript to log the error
            DotNet.invokeMethodAsync('wsp.Blazor.Server.Logs', 'LogErrorFromJS', "Attempting to reconnect, Network Disconnect")
                .finally(() => {
                    // Reload the page after logging the error
                    document.location.reload();
                });
        };
    });
    console.log("testing");
</script>


Basically when ever attempting to connect to server appears I want to invoke C# function from this log class.


This is log class.

namespace wsp.Blazor.Server.Logs
{
    

    public static class LogTester
    {
        [JSInvokable]
        public static void LogErrorFromJS(string errorMessage)
        {
            // Call the logger to log the error
            Protokoll.GetLogger("").Error(errorMessage);
        }
    }

}


What I have tried:

Tried different ways to do this invoke function call, the debugger goes to DotNet.invokeMEthodAsync but then it does not reach or go to log function.
Posted

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900