作者:阳光-假日 | 来源:互联网 | 2024-12-21 16:55
在尝试使用C#WindowsForms客户端通过SignalR连接到ASP.NET服务器时,遇到了内部服务器错误(500)。本文将详细探讨问题的原因及解决方案。
我正在使用ASP.NET网站上的股票SignalR示例,具体参考:
官方文档链接
服务器运行正常,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>("GetAllStocks").Result;
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}
}
在尝试调用
connection.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.b__1(HttpResponseMessage responseMessage)
at Microsoft.AspNet.SignalR.TaskAsyncHelper.<>c__DisplayClass19`2.b__17(Task`1 t)
at Microsoft.AspNet.SignalR.TaskAsyncHelper.TaskRunners`2.<>c__DisplayClass3a.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.b__1(HttpResponseMessage responseMessage)
at Microsoft.AspNet.SignalR.TaskAsyncHelper.<>c__DisplayClass19`2.b__17(Task`1 t)
at Microsoft.AspNet.SignalR.TaskAsyncHelper.TaskRunners`2.<>c__DisplayClass3a.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 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>("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");
此外,请检查服务器端日志以获取更多详细的错误信息,并确保服务器端的SignalR配置正确无误。