SignalR .NET客户端在首次连接尝试时抛出TimeoutException

时间:2015-08-22 12:19:46

标签: c# asp.net signalr

我有两个控制台应用程序:

  • 第一个是包含SignalR v2的自托管ASP.NET WebApi应用程序
  • 第二个应用程序充当SignalR客户端

以下代码演示了我如何初始化SignalR服务器:

public void Configuration(IAppBuilder app)
{
    HttpConfiguration webApiConfiguration = ConfigureWebApi();
    app.UseWebApi(webApiConfiguration);

    app.UseCors(Microsoft.Owin.Cors.CorsOptions.AllowAll);
    app.MapSignalR(new HubConfiguration() { EnableDetailedErrors = true });
}

这就是我尝试从客户端应用程序建立与服务器的连接的方式:

public void Run()
{
    HubConnection hubConnection = new HubConnection(this.hubConnectionURL);
    hubConnection.TraceLevel = TraceLevels.All;
    hubConnection.TraceWriter = Console.Out;
    remoteServerHubProxy = hubConnection.CreateHubProxy("SignalRHub");
    remoteServerHubProxy.On<Int32, MyModel>("MethodName", new Action<Int32, MyModel>((lid, model) => this.MethodName(lid, model)));
    hubConnection.Start().Wait();
}

每当Start().Wait()执行时,发生以下异常 clientside

System.AggregateException was unhandled
  HResult=-2146233088
  Message=One or more errors occurred.
  Source=mscorlib
  StackTrace:
       at System.Threading.Tasks.Task.ThrowIfExceptional(Boolean includeTaskCanceledExceptions)
       at System.Threading.Tasks.Task.Wait(Int32 millisecondsTimeout, CancellationToken cancellationToken)
       at System.Threading.Tasks.Task.Wait()
       ...
       at System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args)
       at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
       at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
       at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
       at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
       at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
       at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
       at System.Threading.ThreadHelper.ThreadStart()
  InnerException: System.TimeoutException
       HResult=-2146233083
       Message=Transport timed out trying to connect
       InnerException: 

有趣的是,这个异常仅在来自客户端的第一次连接尝试期间发生(服务器应用程序重新启动后的第一次连接尝试,就是这样)。如果我重新启动客户端或启动另一个客户端实例,那么它连接正常,SignalR通信工作顺利。

任何人都知道我在做错了什么?

我还从服务器和客户端附加SignalR日志 服务器日志:

SignalR.PerformanceCounterManager Error: 0 : Performance counter failed to load:
 System.InvalidOperationException: The requested Performance Counter is not a cu
stom counter, it has to be initialized as ReadOnly.
   at System.Diagnostics.PerformanceCounter.InitializeImpl()
   at System.Diagnostics.PerformanceCounter..ctor(String categoryName, String co
unterName, String instanceName, Boolean readOnly)
   at Microsoft.AspNet.SignalR.Infrastructure.PerformanceCounterManager.LoadCoun
ter(String categoryName, String counterName, String instanceName, Boolean isRead
Only)

-- Client attempts to connect at this point --

SignalR.ReflectedHubDescriptorProvider Warning: 0 : Some of the classes from ass
embly "System.Web.Helpers, Version=2.0.0.0, Culture=neutral, PublicKeyToken=31bf
3856ad364e35" could Not be loaded when searching for Hubs. [C:\WINDOWS\Microsoft
.Net\assembly\GAC_MSIL\System.Web.Helpers\v4.0_2.0.0.0__31bf3856ad364e35\System.
Web.Helpers.dll]
Original exception type: ReflectionTypeLoadException
Original exception message: Unable to load one or more of the requested types. R
etrieve the LoaderExceptions property for more information.

客户日志:

12:03:07.1550299 - null - ChangeState(Disconnected, Connecting)
12:03:10.8516530 - 51a4e258-e99d-4cb3-ae14-6db438b38f95 - WS Connecting to: ws:/
/localhost:50439/signalr/connect?clientProtocol=1.4&transport=webSockets&connect
ionData=[{"Name":"SignalRHub"}]&connectionToken=AQAAANCMnd8BFdERjHoAwE%2FCl%2BsB
AAAAnx4jsfsOuUa3e0rrW2DifQAAAAACAAAAAAAQZgAAAAEAACAAAAA1tTgi2Va0M73SCNJ9d0%2FKsU
IxJ2aa2MrFGOPAWa7ToAAAAAAOgAAAAAIAACAAAABDorXWYymqxI%2BWurCF6ugPmi7YtifHOo9mqJNF
m2Cw8DAAAACgotOAC75ljMQHhlhFOUsrU4DQBJJ0HvVDP1VGDNfIc9onQ8niU3cmFHA%2BnUHsVExAAA
AAsdFd7ZtqMuHBBl%2FuT%2Bg0hDG5OWmEeJKDZWvkHuOPUHXUVM2S0fTRsDW1BdOg4bai4w3LvbYqqK
dYFqdY8KimvA%3D%3D
12:03:15.9569154 - 51a4e258-e99d-4cb3-ae14-6db438b38f95 - Auto: Failed to connec
t to using transport webSockets. System.TimeoutException: Transport timed out tr
ying to connect
12:03:15.9569154 - 51a4e258-e99d-4cb3-ae14-6db438b38f95 - SSE: GET http://localh
ost:50439/signalr/connect?clientProtocol=1.4&transport=serverSentEvents&connecti
onData=[{"Name":"SignalRHub"}]&connectionToken=AQAAANCMnd8BFdERjHoAwE%2FCl%2BsBA
AAAnx4jsfsOuUa3e0rrW2DifQAAAAACAAAAAAAQZgAAAAEAACAAAAA1tTgi2Va0M73SCNJ9d0%2FKsUI
xJ2aa2MrFGOPAWa7ToAAAAAAOgAAAAAIAACAAAABDorXWYymqxI%2BWurCF6ugPmi7YtifHOo9mqJNFm
2Cw8DAAAACgotOAC75ljMQHhlhFOUsrU4DQBJJ0HvVDP1VGDNfIc9onQ8niU3cmFHA%2BnUHsVExAAAA
AsdFd7ZtqMuHBBl%2FuT%2Bg0hDG5OWmEeJKDZWvkHuOPUHXUVM2S0fTRsDW1BdOg4bai4w3LvbYqqKd
YFqdY8KimvA%3D%3D
12:03:20.9764573 - 51a4e258-e99d-4cb3-ae14-6db438b38f95 - Auto: Failed to connec
t to using transport serverSentEvents. System.TimeoutException: Transport timed
out trying to connect
12:03:20.9764573 - 51a4e258-e99d-4cb3-ae14-6db438b38f95 - LP Connect: http://loc
alhost:50439/signalr/connect?clientProtocol=1.4&transport=longPolling&connection
Data=[{"Name":"SignalRHub"}]&connectionToken=AQAAANCMnd8BFdERjHoAwE%2FCl%2BsBAAA
Anx4jsfsOuUa3e0rrW2DifQAAAAACAAAAAAAQZgAAAAEAACAAAAA1tTgi2Va0M73SCNJ9d0%2FKsUIxJ
2aa2MrFGOPAWa7ToAAAAAAOgAAAAAIAACAAAABDorXWYymqxI%2BWurCF6ugPmi7YtifHOo9mqJNFm2C
w8DAAAACgotOAC75ljMQHhlhFOUsrU4DQBJJ0HvVDP1VGDNfIc9onQ8niU3cmFHA%2BnUHsVExAAAAAs
dFd7ZtqMuHBBl%2FuT%2Bg0hDG5OWmEeJKDZWvkHuOPUHXUVM2S0fTRsDW1BdOg4bai4w3LvbYqqKdYF
qdY8KimvA%3D%3D
12:03:25.9915200 - 51a4e258-e99d-4cb3-ae14-6db438b38f95 - Auto: Failed to connec
t to using transport longPolling. System.TimeoutException: Transport timed out t
rying to connect
12:03:25.9965215 - 51a4e258-e99d-4cb3-ae14-6db438b38f95 - Disconnected
12:03:25.9965215 - 51a4e258-e99d-4cb3-ae14-6db438b38f95 - Transport.Dispose(51a4
e258-e99d-4cb3-ae14-6db438b38f95)
12:03:25.9965215 - 51a4e258-e99d-4cb3-ae14-6db438b38f95 - Closed

1 个答案:

答案 0 :(得分:0)

问题出在库System.Web.Helpers上,该库是ASP.NET MVC安装的一部分。在CopyLocal=true中为此特定程序集设置References后,我的问题就消失了。