.Net客户端控制台应用程序只从hub接收一次消息

时间:2020-12-20 09:50:19

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

我正在控制台应用程序中使用Signalr . net客户端从Signalr Hub(独立的web应用程序)接收消息。

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.

我的控制台应用程序正在正确地连接到集线器,并且只从集线器接收一次消息。然后,在Signalr . net客户端中不被调用的客户端方法。

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

一旦我停止控制台应用程序并运行它,它再次收到来自hub的消息只有一次,什么也没有发生。

Here is my Hub Code

这是我的Hub代码

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

还有我的。net客户端连接

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"));

1 个解决方案

#1


1  

Check the following link

检查以下链接

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

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

#1


1  

Check the following link

检查以下链接

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

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