wpf 客户端未收到 SignalR 消息

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16753482/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 08:52:06  来源:igfitidea点击:

SignalR message not being received on the client

c#wpfwcfsignalr

提问by Bob Horn

I've been trying to get my WPF client app to receive a SignalR message sent by the WCF service. I've tried many things and have now resorted to hacking away in the hopes that something just works. I've followed tutorials and examples online, and I simply can't get my WPF OnSignalRMessage() method to get called. Where am I going wrong here?

我一直在尝试让我的 WPF 客户端应用程序接收 WCF 服务发送的 SignalR 消息。我已经尝试了很多东西,现在已经求助于黑客攻击,希望某些东西能够奏效。我已经在线学习了教程和示例,但我根本无法调用 WPF OnSignalRMessage() 方法。我哪里出错了?

My hub:

我的枢纽:

public class PrestoHub : Hub
{
    public void Send(string message)
    {
        Clients.All.OnSignalRMessage(message);
    }
}

My startup class:

我的创业班:

public class Startup
{
    public void Configuration(IAppBuilder app)
    {
        var config = new HubConfiguration { EnableCrossDomain = true };

        app.MapHubs("http://localhost:8084", config);
    }
}

The method that starts my SignalR host (within my WCF service host):

启动我的 SignalR 主机的方法(在我的 WCF 服务主机内):

    private void StartSignalRHost()
    {
        const string url = "http://localhost:8084";
        WebApplication.Start<Startup>(url);
    }

The code to actually send some message:

实际发送一些消息的代码:

GlobalHost.ConnectionManager.GetHubContext<PrestoHub>().Clients.All.OnSignalRMessage("snuh");
Console.WriteLine("Sent 'snuh' to all clients...");

My WPF client methods:

我的 WPF 客户端方法:

    private void InitializeSignalR()
    {
        var hubConnection = new Connection("http://localhost:8084");
        hubConnection.Start();
        hubConnection.Received += OnSignalRMessage;
    }

    private void OnSignalRMessage(string data)
    {
        MessageBox.Show(data);
    }

采纳答案by Bob Horn

While I'm still struggling to understand the how and why, I was able to get it working. +1 to N. Taylor Mullen for pointing me in the right direction. In addition to his suggestion on the client side, I had to change some server code as well, namely using an empty hub and a simplified Startup class.

虽然我仍在努力理解如何以及为什么,但我能够让它发挥作用。+1 给 N. Taylor Mullen 指出我正确的方向。除了他在客户端的建议之外,我还必须更改一些服务器代码,即使用空集线器和简化的 Startup 类。

My hub:

我的枢纽:

public class PrestoHub : Hub{}

Note: The hub is empty because we're not calling methods within it. As we'll see later, we get the hub context and send messages to the clients.

注意:集线器是空的,因为我们没有在其中调用方法。正如我们稍后将看到的,我们获取集线器上下文并向客户端发送消息。

My startup class:

我的创业班:

public class Startup
{
    public void Configuration(IAppBuilder app)
    {
        app.MapHubs();
    }
}

The above code seems to be what fixed the problem. This also works:

上面的代码似乎是解决问题的方法。这也有效:

var config = new HubConfiguration { EnableCrossDomain = true };
app.MapHubs(config);

But as soon as I specify a URL, my client doesn't receive the messages (tried with and without the "SignalR" part):

但是一旦我指定了一个 URL,我的客户端就不会收到消息(尝试使用和不使用“SignalR”部分):

app.MapHubs("http://localhost:8084/SignalR", config);

The method that starts my SignalR host (within my WCF service host):

启动我的 SignalR 主机的方法(在我的 WCF 服务主机内):

private void StartSignalRHost()
{
    const string url = "http://localhost:8084";
    WebApplication.Start<Startup>(url);
}

The code to actually send some message:

实际发送一些消息的代码:

var hubContext = GlobalHost.ConnectionManager.GetHubContext<PrestoHub>();
hubContext.Clients.All.OnSignalRMessage("snuh");

My WPF client method:

我的 WPF 客户端方法:

private void InitializeSignalR()
{
    var hubConnection = new HubConnection("http://localhost:8084");
    var prestoHubProxy = hubConnection.CreateHubProxy("PrestoHub");
    prestoHubProxy.On<string>("OnSignalRMessage", (data) =>
        {
            MessageBox.Show(data);
        });
    hubConnection.Start();
}

回答by N. Taylor Mullen

You're creating a PersistentConnection not a hub connection. In order to get messages from your PrestoHub you first need to connect with a HubConnection and then you need to handle the event "OnSignalRMessage".

您正在创建 PersistentConnection 而不是集线器连接。为了从您的 PrestoHub 获取消息,您首先需要连接 HubConnection,然后您需要处理事件“OnSignalRMessage”。

So your client code would now look like:

所以你的客户端代码现在看起来像:

private void InitializeSignalR()
{
    var hubConnection = new HubConnection("http://localhost:8084");
    var prestoHubProxy = hubConnection.CreateHubProxy("PrestoHub");

    // Bind the "OnSignalRMessage" to a function
    prestoHubProxy.On<string>("OnSignalRMessage", (data) => {
        MessageBox.Show(data);
    });

    hubConnection.Start();  
}

回答by Bruno Costa

If your methods on the server side are asynchronous make sure they return a task instead of void. That is you should have

如果服务器端的方法是异步的,请确保它们返回一个任务而不是 void。那是你应该有

public async Task Method(){ }

and not

并不是

public async void Method(){ }