问题
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