.Net Core ClientWebSocket:无法连接到服务器

时间:2017-04-18 22:31:44

标签: c# websocket .net-core predix

我正在构建一个连接到Predix Timeseries实例的Web应用程序,以通过websocket接收数据。当我尝试创建套接字时,我得到了这个例外:

  

System.Net.WebSockets.WebSocketException:'无法连接到   远程服务器'

我正在使用此代码创建我的websocket,并在ConnectAsync调用中抛出了我的错误:

public async Task openWebSocket()
    {
        _socket = new ClientWebSocket();

        _socket.Options.SetRequestHeader(headerName: "predix-zone-id", headerValue: PREDIX_ZONE_ID_HERE);
        _socket.Options.SetRequestHeader(headerName: "authorization", headerValue: "Bearer " + AUTH_TOKEN_HERE);
        _socket.Options.SetRequestHeader(headerName: "content-type", headerValue: "application/json");
        CancellationToken token = new CancellationToken();

        var uri = new Uri(uriString: "wss://gateway-predix-data-services.run.aws-usw02-pr.ice.predix.io/v1/stream/messages");
        await _socket.ConnectAsync(uri: uri, cancellationToken: token);

    }

这是我的异常堆栈跟踪:

System.Net.WebSockets.WebSocketException occurred
  HResult=0x80004005
  Message=Unable to connect to the remote server
  Source=<Cannot evaluate the exception source>
  StackTrace:
   at System.Net.WebSockets.WebSocketHandle.<ConnectAsyncCore>d__20.MoveNext()
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at System.Net.WebSockets.ClientWebSocket.<ConnectAsyncCore>d__16.MoveNext()
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter.GetResult()
   at PROJECTNAME.Services.TimeseriesService.<openWebSocket>d__6.MoveNext() in %PROJECTLOCATION%\Services\%SERVICENAME%.cs:line 34
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter.GetResult()
   at PROJECTNAME.Services.TimeseriesService.<Initialize>d__5.MoveNext() in %PROJECTLOCATION%\Services\%SERVICENAME%.cs:line 21
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter.GetResult()
   at PROJECTNAME.Program.<Initialize>d__1.MoveNext() in %PROJECTLOCATION%\Program.cs:line 52

Inner Exception 1:
WebSocketException: Unable to connect to the remote server

内部异常堆栈跟踪只有更多信息,但注意到可能有用的ThrowOnInvalidConnectState():

    at System.Net.WebSockets.WinHttpWebSocket.ThrowOnInvalidConnectState()
   at System.Net.WebSockets.WinHttpWebSocket.<ConnectAsync>d__18.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at System.Net.WebSockets.WebSocketHandle.<ConnectAsyncCore>d__20.MoveNext()

我不确定还有什么可以尝试 - 很多其他帖子在SO上至少在内部异常中有一些有用的信息。

我也尝试过使用Fiddler。我可以看到其他服务的消息,包括从UAA检索我的身份验证令牌,但此服务没有任何内容。

我在某处错过了一些配置步骤吗?任何帮助或指导将不胜感激。

2 个答案:

答案 0 :(得分:2)

我想出了这个问题。 Predix时间序列的三个必需标题是:

  • Predix-Zone-Id: zone_id_here
  • 授权:承载 auth_token_here
  • 原产地:“ https:// hostname_here

我的问题是我尝试了Origin标头,但application_uris的Predix VCAP_APPLCIATION环境变量不包含“https://”。一旦我手动添加它(“https://”+ uri),它就可以正常工作。

答案 1 :(得分:0)

我在C#中没有这样做,但“无法连接”听起来像是网络问题。你能打开一个websocket到另一台服务器吗?也许这个?

https://www.websocket.org/echo.html

可能是代理服务器问题?你可以试试不同的网络吗?