Search code examples
asp.net-mvcsignalrsignalr.clientsignalr.net-client

Signalr .Net Client Console application receive messages from hub only once


I'm using Signalr .Net Client in my Console application to receive messages from the Signalr Hub which is a separate web application.

My console application is connecting to the hub correctly and receive message from the hub only once. Then the client method in the Signalr .Net client not getting called.

Once I stop the console application and run it, again it receive a message from the hub only once and nothing happens.

Here is my Hub Code

public override Task OnConnected()
        {
            try
            {
                 var cType = Context.QueryString["type"];
                var connectionId = Context.ConnectionId;
                var connectedUserList = (from d in Users
                                         where d.ClientType == cType
                                                          select d).ToList();
                if (connectedUserList.Count > 0)
                {
                    var conUser = connectedUserList.First<ConnectedUsers>();
                    conUser.ConnectionIds.Add(connectionId);
                }
                else
                {
                    var newUser = new ConnectedUsers
                    {
                        ConnectionIds = new HashSet<string> {connectionId}
                       ,
                        ClientType = cType
                    };
                    Users.Add(newUser);
                }
            }
            catch (Exception ex)
            {

           ).Error(ex);
            }
            return base.OnConnected();
        }

And My .Net Client Connection

static  void Main(string[] args)
        {

            SignalrHandler();
            Console.ReadLine();

        }

        public static async void SignalrHandler()
        {
            var url = ConfigurationSettings.AppSettings["Url"] ?? @"http://localhost:1010/";
            var querystringData = new Dictionary<string, string> { { "type", "WIN" } };
            _hubConnection = new HubConnection(url, querystringData);
            MarcolinMainProxy = _hubConnection.CreateHubProxy("MainHub");
            MarcolinMainProxy.On<string>("sendAlert", type => InvokeMethod(type));    
           await _hubConnection.Start();

        }

Client Method

private static void InvokeMethod(string type)
        {
            Console.WriteLine(String.Format("Recieved Message From Server On :{0}",System.DateTime.Now.ToString()));
            Console.WriteLine("Message Received");

            Console.ReadLine();


        }

And This happens when I use an Invoke method with following line

MarcolinMainProxy.On<string>("sendAlert", type => InvokeMethod(type));

And when I use following line it works..

MarcolinMainProxy.On<string>("sendAlert", stock => Console.WriteLine("Symbol {0} Price {1}", "sd", "sdde"));

Solution

  • Check the following link

    https://damienbod.com/2013/11/13/signalr-messaging-a-complete-client-with-a-console-application/

    You have to change your code to

    MarcolinMainProxy.On<string>("sendAlert", InvokeMethod);