简体   繁体   English

启动时的ASP.NET SignalR C#客户端应用程序异常

[英]ASP.NET SignalR C# Client App Exception on Start

I have a ASP.NET application. 我有一个ASP.NET应用程序。 I run it by hitting debug. 我通过点击调试来运行它。 It runs on localhost:2842. 它运行在localhost:2842上。

I have a SignalR Hub defined in a class with no methods on the server. 我在类中定义了一个SignalR Hub,服务器上没有方法。

I have a WPF Windows C# application. 我有一个WPF Windows C#应用程序。 I added the SignalR NuGet package. 我添加了SignalR NuGet包。 I use the following code to instantiate the client proxy: 我使用以下代码来实例化客户端代理:

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

        IHubProxy myHubProxy = hubConnection.CreateHubProxy("MyHub");
        myHubProxy.On<string>("SendMeData", dataString => {

            Console.WriteLine("String value {0}", dataString);
        });

        hubConnection.Start().Wait();

Here is my hub class defined on the server: 这是我在服务器上定义的集线器类:

public class MyHub : Hub
{
    public static void SendMeData(string dataString)
    {
        var context = GlobalHost.ConnectionManager.GetHubContext<MyHub>();
        if (context != null)
            context.Clients.All.SendMeData(dataString);
    }
}

I have this code in Startup.cs: 我在Startup.cs中有这个代码:

public partial class Startup
{
    public void Configuration(IAppBuilder app)
    {
        ConfigureAuth(app);

        app.MapSignalR(new HubConfiguration
        {
            EnableDetailedErrors = true
        });
    }
}

The problem I am having is that when the C# client calls hubConnection.Start().Wait(); 我遇到的问题是,当C#客户端调用hubConnection.Start()时,Wait(); I get a very unhelpful exception. 我得到了一个非常无益的例外。 Here is the exception: 这是一个例外:

StatusCode: 500, ReasonPhrase: 'Internal Server Error', Version: 1.1, Content:
System.Net.Http.StreamContent, Headers:  Transfer-Encoding: chunked
X-SourceFiles: =?UTF-8?B?QzpcTXlEZXZcUmVhbHRpbWVNRFxSZWFsdGltZU1EUHJvdG9cUmVhbHRpbWVNRFByb3RvXHNpZ25hbHJcbmVnb3RpYXRl?=
Cache-Control: private  Date: Thu, 27 Apr 2017 04:23:18 GMT  Server: Microsoft-IIS/10.0
X-AspNet-Version: 4.0.30319  X-Powered-By: ASP.NET  Content-Type: text/html; charset=utf-8

I set break points in the server code and it never hits any of them. 我在服务器代码中设置了断点,它永远不会击中它们中的任何一个。

How do I figure this out? 我怎么知道这个?

In your MyHub class, you've got the following line: 在你的MyHub类中,你有以下几行:

var context = GlobalHost.ConnectionManager.GetHubContext<MyHub>();
        if (context != null)

Get rid of this. 摆脱这个。 Instead your MyHub class should be: 相反,你的MyHub类应该是:

public class MyHub : Hub
{
    public static void SendMeData(string dataString)
    {
       Clients.All.SendMeData(dataString);
    }
}

GetHubContext is used for getting a reference to a Hub from another class within the same AppDomain. GetHubContext用于从同一AppDomain中的另一个类获取对Hub的引用。 For example, if you are in a Controller class and need to call a method on a Hub within the same AppDomain, you would get a reference to the Hub via GetHubContext. 例如,如果您在Controller类中并且需要在同一AppDomain中的Hub上调用方法,则可以通过GetHubContext获取对Hub的引用。

Also in your client code, please remove the .Wait(). 同样在您的客户端代码中,请删除.Wait()。 The code should be 代码应该是

await hubConnection.Start();

Reference 参考

The whole problem ended up being that I had changed the name of my Hub class in the server without changing the name used in CreateHubProxy. 整个问题最终是我在服务器中更改了我的Hub类的名称,而没有更改CreateHubProxy中使用的名称。 it is working now. 它现在正在运作。

There're probably two reasons for this symptom: 这种症状可能有两个原因:

  1. The hub name is not found, or misspelled. 未找到集线器名称或拼写错误。
  2. The hub class is not public. 集线器类不公开。

Anyway, the SignalR library is too bad to not pointing out the real cause, with only a 500 code in error!! 无论如何,SignalR库太糟糕了,没有指出真正的原因,只有500个代码出错!!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM