C#SignalR异常 - 在收到调用结果之前,连接开始重新连接

bri*_*342 4 c# asp.net websocket signalr

我正在开发2个应用程序,第一个是C#控制台应用程序,另一个是Asp.net Web应用程序.我正在使用SignalR连接两者.

这是我的C#控制台应用程序(客户端)

public class RoboHub
{
    public static IHubProxy _hub;

    public RoboHub()
    {
        StartHubConnection();
        _hub.On("GetGoals", () => GetGoals());
        _hub.On("PrintMessageRobot", x => PrintMessageRobot(x));

        Thread thread = new Thread(MonitorHubStatus);
        thread.Start();
    }

    public void GetGoals()
    {
        //TODO: Does stuff
    }

    public void PrintMessageRobot(string msg)
    {
        Console.WriteLine(msg);
    }

    public void StartHubConnection()
    {
        Console.WriteLine("Robo Hub Starting");
        string url = @"http://localhost:46124/";
        var connection = new HubConnection(url);
        _hub = connection.CreateHubProxy("WebHub");
        connection.Start().Wait();
        Console.WriteLine("Robo Hub Running");
    }

    public void MonitorHubStatus()
    {
        while (true)
        {
            Thread.Sleep(1000);
            _hub.Invoke("Ping", "ping").Wait();
            Console.WriteLine("WebHub Pinged : " + DateTime.Now);
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

控制台应用程序运行时,它会创建RoboHub类的实例.它反过来启动与SignalR集线器的连接,并在一个单独的线程上启动方法MonitorHubStatus,这是我实现的,以检查C#控制台应用程序客户端是否仍然主动连接到集线器.

这是我的Web中心(在Asp.net Web应用程序中)

public class WebHub : Hub
{
    /// <summary>
    /// This method should be called by the Web Clients. 
    /// This method should call the method on the robot clients.
    /// </summary>
    public void GetGoalsHub()
    {
        lock (UserHandler.Connections)
        {
            if (UserHandler.Connections.Any(connection => connection.Contains("Robot")))
            {
                Clients.All.GetGoals();
            }
        }
        //TODO add Error method to call on the client
    }

    /// <summary>
    /// Override Methods
    /// </summary>
    /// <returns></returns>
    public override Task OnConnected()
    {
        lock (UserHandler.Connections)
        {
            //Find out who is connecting based on the User-Agent header
            var query = (from r in Context.Headers
                where r.Key == "User-Agent"
                select r).SingleOrDefault().ToString();

            if (query.Contains("SignalR.Client.NET45"))
            {
                UserHandler.Connections.Add("Robot : " + Context.ConnectionId);
            }
            else
            {
                UserHandler.Connections.Add("Web Application : " + Context.ConnectionId);
                GetGoalsHub();
            }
        }
        Clients.All.UpdateConnections(UserHandler.Connections);
        return base.OnConnected();
    }

    public override Task OnDisconnected(bool stopCalled)
    {
        lock (UserHandler.Connections)
        {
            for (int i = 0; i < UserHandler.Connections.Count; i++)
            {
                if (UserHandler.Connections[i].Contains(Context.ConnectionId))
                {
                    UserHandler.Connections.Remove(UserHandler.Connections[i]);
                }
            }
        }
        Clients.All.UpdateConnections(UserHandler.Connections);
        return base.OnDisconnected(stopCalled);
    }

    public void Ping(string msg)
    {
        Clients.All.PrintMessageRobot("Pong : " + DateTime.Now);
    }
}
public static class UserHandler
{
    public static List<string> Connections = new List<string>(); 
}
Run Code Online (Sandbox Code Playgroud)

目前这两个应用程序似乎工作了一段时间,直到一段时间后这个错误随机出现:连接在收到调用结果之前开始重新连接.

Web集线器还应该调用C#控制台客户端上的任何其他方法,例如GetGoals方法.'乒乓'方法冻结,一段时间后抛出类似的异常.在此过程中,Web客户端继续完美运行,Web客户端可以与中心服务器来回通信.

任何人都可以建议问题是什么?编辑:进一步的调查让我相信它与线程有关,但是很难找到问题的根源.

bri*_*342 6

我找到了解决方案.

问题在于调用调用:

_hub.Invoke("MethodName", "Parameters").Wait();
Run Code Online (Sandbox Code Playgroud)

在这里我告诉它等待响应但是我没有在Web服务器中编写任何回复机制.

通过切换到:修复此错误:

_hub.Invoke("MethodName", "Parameters");
Run Code Online (Sandbox Code Playgroud)

现在它遵循"即发即忘"的方法,我现在不再得到这个错误.如果其他人得到此错误,请务必检查您是否需要回复.

  • 这不是正确的答案.在这里,`等待()`使你的代码等待异步调用的执行(这是调用,并可能导致可能的死锁),并且你不需要在Web服务器编程任何答复机制,服务器将无论您的集线器代码是什么,都会返回响应.通过删除`Wait()`,你只是使用**fire和forget**模式,通过启动异步调用而不是等到它完成.这样做只是一个糟糕的解决方法,可能会出现其他问题,您应该更好地理解信号生命周期并理解您的代码被破坏的原因. (7认同)