Can I debug OnConnected method in SignalR Hub?

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();
        }

    }
}

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.

链接地址: http://www.djcxy.com/p/96180.html

上一篇: 从另一个项目调用SignalR hub方法

下一篇: 我可以在SignalR Hub中调试OnConnected方法吗?