Can I debug OnConnected method in SignalR Hub?

拥有回忆 提交于 2019-12-11 04:19:17

问题


I have a Hub inside my asp.net project , and I overridden the OnConnected method, can I debug the code inside the OnConnected method? I put a breakpoint but it's not firing.

    public class ConsultasHub : Hub
    {                    
        public override Task OnConnected()
        {
            //breakpoint here
            //some code
            //[...]
            return base.OnConnected();
        }

    }
}

回答1:


This is a tricky one. By design, if you don't have any subscriptions to the hub, then the client can't get any messages from server so the OnConnected won't get called. I tried the following:

using System;
using System.Web;
using Microsoft.AspNet.SignalR;
using System.Web.Script.Serialization;
using System.Collections.Generic;
using System.Threading.Tasks;

public class ConsultasHub : Hub {
    public override Task OnConnected() {
        //breakpoint here
        //some code
        //[...]
        return base.OnConnected();
    }
}

call:

var chat = $.connection.consultasHub;

console.log("connecting...");
$.connection.hub.start().done(function () {
    console.log("done.");
});

And the OnConnected not fired, console message 'done.' is written.

However, once I have an event and a subscription,

using System;
using System.Web;
using Microsoft.AspNet.SignalR;
using System.Web.Script.Serialization;
using System.Collections.Generic;
using System.Threading.Tasks;

public class ConsultasHub : Hub {
    public override Task OnConnected() {
        //breakpoint here
        //some code
        //[...]
        return base.OnConnected();
    }

    public void SendMessage( string message ) {
        Clients.All.message(message);
    }
}   
var chat = $.connection.consultasHub;
chat.client.message = function (message) {
    console.log("message: " + message);
};

console.log("connecting...");
$.connection.hub.start().done(function () {
    console.log("done.");
});

OnConnected fired (breakpoint reached). Try it out! I Hope it helps.



来源:https://stackoverflow.com/questions/31798239/can-i-debug-onconnected-method-in-signalr-hub

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!