Ľuboš Pilka
Ľuboš Pilka

Reputation: 1148

Call method on hub from HubPipelineModule OnIncomingError event

I need to implement custom error logging on signalR hubs. This can be done by creating ErrorModule which will inherit from HubPipelineModule and it will be registered on Startup like this

   GlobalHost.HubPipeline.AddModule(new ErrorModule()); 

This is my hub

 public class FooHub : Hub
{
    public void MethodWithException()
    {
        throw new Exception();
    }

    public void FooMethod()
    {

    }
}

And this is my module

public class ErrorModule : HubPipelineModule
{
    protected override void OnIncomingError(ExceptionContext exceptionContext, IHubIncomingInvokerContext invokerContext)
    {
        //call foo method on foo hub 

        //log error
    }
}

Lets say MethodWithException() is called. It is possible to call FooMethod() on hub which invoked event OnIncomingError ?

Upvotes: 1

Views: 1026

Answers (1)

Quentin Roger
Quentin Roger

Reputation: 6538

With something like this :

protected override void OnIncomingError(ExceptionContext exceptionContext,
                                                IHubIncomingInvokerContext invokerContext)
        {
            dynamic caller = invokerContext.Hub.Clients.Caller;
            caller.ExceptionHandler(exceptionContext.Error.Message);

            //log something here
            Debug.WriteLine(exceptionContext.Error.Message);
        }

EDIT

To call a particular method of the hub you can do :

protected override void OnIncomingError(ExceptionContext exceptionContext,
                                                IHubIncomingInvokerContext invokerContext)
        {
            var hub = (invokerContext.Hub as HubType); //
            hub.MyMethod("myparam","mysecondparam");

            //log something here
            Debug.WriteLine(exceptionContext.Error.Message);
        }

You can find the full code here : How to handle SignalR server exception at client?

Upvotes: 1

Related Questions