Azure Web应用程序重新启动后,SignalR重新连接

时间:2016-11-15 17:05:02

标签: azure signalr azure-web-sites signalr-hub

重新启动承载SignalR Hub的Azure Web App后,我面临着一种奇怪的重新连接行为。当我重新启动时,即使应用程序重新启动的时间少于 DisconnectTimeout (测试时间为2分钟),客户端也不会重新连接。

我做错了吗?

中心代码

public class PingHub : Hub
{
    public void Hello()
    {
        Clients.All.hello();
    }

    public override Task OnReconnected()
    {
        Trace.WriteLine("Reconnect");
        return base.OnReconnected();
    }

    public override Task OnConnected()
    {
        Trace.WriteLine("Connect");
        return base.OnConnected();
    }

}

客户代码

var hubConnection = new HubConnection("http://url/");


            hubConnection.TraceLevel = TraceLevels.All;
            hubConnection.TraceWriter = Console.Out;

            IHubProxy hubProxy = hubConnection.CreateHubProxy("PingHub");

            hubProxy.On("hello", () => Console.WriteLine($"Hello {DateTime.Now.ToString()}"));

            hubConnection.Reconnected += () =>
            {
                Console.WriteLine("Reconnected");
            };

            hubConnection.Start().Wait();

客户跟踪日志

16:55:48.3999367 - null - ChangeState(Disconnected, Connecting)
16:55:48.8459354 - 6171c2d4-a9dd-4fa4-b710-0910af48132b - SSE: GET http://gf-test-signalr.azurewebsites.net/signalr/connect?clientProtocol=1.4&transport=serverSentEvents&connectionData=[{"Name":"PingHub"}]&connectionToken=9Vs1ACQjDX%2BQmrcJ2XnoLCCJN%2FDtlJd%2BM0r5o8QvORX50ydXDkrAzeeVUgVIzNc3d7JcDvJ49KmxI3oVPQ%2Bt8IUMJe8HGFAJDasufD%2FFwxEr2l23l40q2dlKVADnFJA5
16:55:48.9604385 - 6171c2d4-a9dd-4fa4-b710-0910af48132b - SSE: OnMessage(Data: initialized)
16:55:48.9609355 - 6171c2d4-a9dd-4fa4-b710-0910af48132b - SSE: OnMessage(Data: {"C":"d-B53A1D13-E,0|F,0|G,1","S":1,"M":[]})
16:55:49.1059354 - 6171c2d4-a9dd-4fa4-b710-0910af48132b - ChangeState(Connecting, Connected)
16:55:53.0300013 - 6171c2d4-a9dd-4fa4-b710-0910af48132b - SSE: OnMessage(Data: {})
16:56:03.0655798 - 6171c2d4-a9dd-4fa4-b710-0910af48132b - SSE: OnMessage(Data: {})
16:56:13.0791344 - 6171c2d4-a9dd-4fa4-b710-0910af48132b - SSE: OnMessage(Data: {})
16:56:23.0965041 - 6171c2d4-a9dd-4fa4-b710-0910af48132b - SSE: OnMessage(Data: {})
16:56:26.7919383 - 6171c2d4-a9dd-4fa4-b710-0910af48132b - ChangeState(Connected, Reconnecting)
16:56:26.7939373 - 6171c2d4-a9dd-4fa4-b710-0910af48132b - SSE: GET http://gf-test-signalr.azurewebsites.net/signalr/reconnect?clientProtocol=1.4&transport=serverSentEvents&connectionData=[{"Name":"PingHub"}]&connectionToken=9Vs1ACQjDX%2BQmrcJ2XnoLCCJN%2FDtlJd%2BM0r5o8QvORX50ydXDkrAzeeVUgVIzNc3d7JcDvJ49KmxI3oVPQ%2Bt8IUMJe8HGFAJDasufD%2FFwxEr2l23l40q2dlKVADnFJA5&messageId=d-B53A1D13-E%2C0%7CF%2C0%7CG%2C1
16:56:26.8962939 - 6171c2d4-a9dd-4fa4-b710-0910af48132b - OnError(Microsoft.AspNet.SignalR.Client.HttpClientException: StatusCode: 503, ReasonPhrase: 'Service Unavailable', Version: 1.1, Content: System.Net.Http.StreamContent, Headers:
{
  Date: Tue, 15 Nov 2016 16:56:22 GMT
  Set-Cookie: ARRAffinity=9fa33f4c59eaa0cb53ffc0472e2395fa67ff17a0f59613b57fb963b1519ab999;Path=/;Domain=gf-test-signalr.azurewebsites.net
  Server: Microsoft-IIS/8.0
  Content-Length: 326
  Content-Type: text/html; charset=us-ascii
}
   at Microsoft.AspNet.SignalR.Client.Http.DefaultHttpClient.<>c__DisplayClass5_0.<Get>b__1(HttpResponseMessage responseMessage)
   at Microsoft.AspNet.SignalR.TaskAsyncHelper.<>c__DisplayClass31_0`2.<Then>b__0(Task`1 t)
   at Microsoft.AspNet.SignalR.TaskAsyncHelper.TaskRunners`2.<>c__DisplayClass3_0.<RunTask>b__0(Task`1 t))
16:56:28.9148136 - 6171c2d4-a9dd-4fa4-b710-0910af48132b - SSE: GET http://gf-test-signalr.azurewebsites.net/signalr/reconnect?clientProtocol=1.4&transport=serverSentEvents&connectionData=[{"Name":"PingHub"}]&connectionToken=9Vs1ACQjDX%2BQmrcJ2XnoLCCJN%2FDtlJd%2BM0r5o8QvORX50ydXDkrAzeeVUgVIzNc3d7JcDvJ49KmxI3oVPQ%2Bt8IUMJe8HGFAJDasufD%2FFwxEr2l23l40q2dlKVADnFJA5&messageId=d-B53A1D13-E%2C0%7CF%2C0%7CG%2C1
16:56:29.0051243 - 6171c2d4-a9dd-4fa4-b710-0910af48132b - OnError(Microsoft.AspNet.SignalR.Client.HttpClientException: StatusCode: 503, ReasonPhrase: 'Service Unavailable', Version: 1.1, Content: System.Net.Http.StreamContent, Headers:
{
  Date: Tue, 15 Nov 2016 16:56:24 GMT
  Server: Microsoft-IIS/8.0
  Content-Length: 326
  Content-Type: text/html; charset=us-ascii
}
   at Microsoft.AspNet.SignalR.Client.Http.DefaultHttpClient.<>c__DisplayClass5_0.<Get>b__1(HttpResponseMessage responseMessage)
   at Microsoft.AspNet.SignalR.TaskAsyncHelper.<>c__DisplayClass31_0`2.<Then>b__0(Task`1 t)
   at Microsoft.AspNet.SignalR.TaskAsyncHelper.TaskRunners`2.<>c__DisplayClass3_0.<RunTask>b__0(Task`1 t))
16:56:31.0165736 - 6171c2d4-a9dd-4fa4-b710-0910af48132b - SSE: GET http://gf-test-signalr.azurewebsites.net/signalr/reconnect?clientProtocol=1.4&transport=serverSentEvents&connectionData=[{"Name":"PingHub"}]&connectionToken=9Vs1ACQjDX%2BQmrcJ2XnoLCCJN%2FDtlJd%2BM0r5o8QvORX50ydXDkrAzeeVUgVIzNc3d7JcDvJ49KmxI3oVPQ%2Bt8IUMJe8HGFAJDasufD%2FFwxEr2l23l40q2dlKVADnFJA5&messageId=d-B53A1D13-E%2C0%7CF%2C0%7CG%2C1
16:56:56.7950186 - 6171c2d4-a9dd-4fa4-b710-0910af48132b - OnError(System.TimeoutException: Couldn't reconnect within the configured timeout of 00:00:30, disconnecting.)
16:56:56.7959897 - 6171c2d4-a9dd-4fa4-b710-0910af48132b - Disconnected
16:56:56.8103502 - 6171c2d4-a9dd-4fa4-b710-0910af48132b - Transport.Dispose(6171c2d4-a9dd-4fa4-b710-0910af48132b)
16:56:56.8108527 - 6171c2d4-a9dd-4fa4-b710-0910af48132b - Closed

1 个答案:

答案 0 :(得分:2)

  

16:56:56.7950186 - 6171c2d4-a9dd-4fa4-b710-0910af48132b - OnError(System.TimeoutException:无法在配置的超时00:00:30内重新连接,断开连接。)

据我所知, DisconnectTimeout 的默认值为30秒。根据日志,重新连接大约需要30秒,所以请检查您是否set/change DisconnectTimeout setting in Application_Start

GlobalHost.Configuration.DisconnectTimeout = TimeSpan.FromSeconds(30);

此外,如果要在连接丢失后不断重新连接到集线器,可以从disconnect事件处理程序调用Start方法。有关更多详细信息,请参阅“How to continuously reconnect”。