javascript – 尝试使用connection.Start()连接到服务器时出现SignalR Client错误

我正在使用ASP.NET网站上的股票SignalR示例:

http://www.asp.net/signalr/overview/signalr-20/getting-started-with-signalr-20/tutorial-server-broadcast-with-signalr-20

服务器运行正常,JavaScript客户端正在按照预期工作并在彼此之间进行通信.服务器和JavaScript客户端在IIS上运行.

JavaScript客户端位于:

http://192.168.1.5:8080/RTStreamer/SignalR.Sample/StockTicker.html

现在我正在尝试使用C#Windows Forms客户端,但没有成功:

private void Form1_Load(object sender, EventArgs e)
{
    var connection = new HubConnection("http://192.168.1.5:8080/RTStreamer/signalr");
    IHubProxy proxy = connection.CreateHubProxy("StockTickerHub");

    try
    {
        connection.Start().Wait();
        var stocks = proxy.Invoke<IEnumerable<Stock>>("GetAllStocks").Result;
    }
    catch (Exception ex)
    {
        MessageBox.Show(ex.ToString());
    }
}

我在尝试连接时遇到此错误.Start():

System.AggregateException: One or more errors occurred. ---> Microsoft.AspNet.SignalR.Client.HttpClientException: StatusCode: 500, ReasonPhrase: 'Internal Server Error', Version: 1.1, Content: System.Net.Http.StreamContent, Headers:
{
  Transfer-Encoding: chunked
  Cache-Control: private
  Date: Tue, 12 Aug 2014 13:46:31 GMT
  Server: Microsoft-IIS/8.5
  X-AspNet-Version: 4.0.30319
  X-Powered-By: ASP.NET
  Content-Type: text/html; charset=utf-8
}
   at Microsoft.AspNet.SignalR.Client.Http.DefaultHttpClient.<>c__DisplayClass2.<Get>b__1(HttpResponseMessage responseMessage)
   at Microsoft.AspNet.SignalR.TaskAsyncHelper.<>c__DisplayClass19`2.<Then>b__17(Task`1 t)
   at Microsoft.AspNet.SignalR.TaskAsyncHelper.TaskRunners`2.<>c__DisplayClass3a.<RunTask>b__39(Task`1 t)
   --- End of inner exception stack trace ---
   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 TraderClient.Form1.Form1_Load(Object sender, EventArgs e) in c:\Ark\Form1.cs:line 31
---> (Inner Exception #0) Microsoft.AspNet.SignalR.Client.HttpClientException: StatusCode: 500, ReasonPhrase: 'Internal Server Error', Version: 1.1, Content: System.Net.Http.StreamContent, Headers:
{
  Transfer-Encoding: chunked
  Cache-Control: private
  Date: Tue, 12 Aug 2014 13:46:31 GMT
  Server: Microsoft-IIS/8.5
  X-AspNet-Version: 4.0.30319
  X-Powered-By: ASP.NET
  Content-Type: text/html; charset=utf-8
}
   at Microsoft.AspNet.SignalR.Client.Http.DefaultHttpClient.<>c__DisplayClass2.<Get>b__1(HttpResponseMessage responseMessage)
   at Microsoft.AspNet.SignalR.TaskAsyncHelper.<>c__DisplayClass19`2.<Then>b__17(Task`1 t)
   at Microsoft.AspNet.SignalR.TaskAsyncHelper.TaskRunners`2.<>c__DisplayClass3a.<RunTask>b__39(Task`1 t)<---

这是Hub代码:

namespace Microsoft.AspNet.SignalR.StockTicker
{
    [HubName("stockTicker")]
    public class StockTickerHub : Hub
    {
        private readonly StockTicker _stockTicker;

        public StockTickerHub()
            : this(StockTicker.Instance)
        {

        }

        public StockTickerHub(StockTicker stockTicker)
        {
            _stockTicker = stockTicker;
        }

        public IEnumerable<StockQuote> GetAllStocks()
        {
            return _stockTicker.GetAllStocks();
        }

        ...

    }
}

解决方法:

完全根据链接文章设置项目,适用于我的.NET客户端具有以下代码:

var connection = new HubConnection("http://localhost:65069/");
IHubProxy proxy = connection.CreateHubProxy("stockTickerMini");

try
{
    connection.Start().Wait();
    var stocks = proxy.Invoke<IEnumerable<Stock>>("GetAllStocks").Result;
}
...

在我的示例中,StockTicker.html页面位于http:// localhost:65069 / StockTicker.html,并且根据属性,集线器名称为stockTickerMini.

在您的情况下,下面的值应该是正确的:

var connection = new HubConnection("http://192.168.1.5:8080/RTStreamer");
IHubProxy proxy = connection.CreateHubProxy("stockTicker");
上一篇:2021-08-04


下一篇:c# – $.connections无法读取未定义的集线器(SignalR)