Can I debug the OnConnected method in a SignalR hub?

I have a hub inside my asp.net project and I redefined the OnConnected method, can I debug the code inside the OnConnected method? I set a breakpoint, but did not shoot.

public class ConsultasHub : Hub { public override Task OnConnected() { //breakpoint here //some code //[...] return base.OnConnected(); } } } 
+2
source share
1 answer

It's complicated. By design, if you do not have a hub subscription, the client cannot receive messages from the server, so OnConnected will not be 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 OnConnected is not enabled, the console message is "done." it is written.

However, as soon as 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). Give it a try! Hope this helps.

+7
source

Source: https://habr.com/ru/post/1232260/


All Articles