【问题标题】:SignalR - Calling a WPF Client method from a Self-Hosted Console ServerSignalR - 从自托管控制台服务器调用 WPF 客户端方法
【发布时间】:2016-11-26 12:23:27
【问题描述】:

我按照this 教程,成功搭建了一个Client -> ServerServer -> Client 实时通信演示。但是,当尝试在 WPF 项目(而不是控制台项目)中重新创建相同的场景时,我似乎无法从 SignalR Hub 调用 WPF 项目的方法。

注意: WPF 项目和自托管控制台项目在相同 Visual Studio 解决方案中

SignalR Hub:(在自托管服务器控制台项目中)

public class TestHub : Hub
{
    public void NotifyAdmin_Signup()
    {
        Clients.All.NotifyStation_Signup();
        //This should call the WPF Project's NotifyStation_Signup() method
    }
}

从同一个控制台启动服务器并调用 Hub 方法:

class Program
{
    static void Main(string[] args)
    {
        //Start the Local server
        string url = @"http://localhost:8080/";
        using (WebApp.Start<Startup>(url))
        {
            Console.WriteLine(string.Format("Server running at {0}", url));
            Console.ReadLine();
        }

        IHubContext hubContext = GlobalHost.ConnectionManager.GetHubContext<TestHub>();
        hubContext.Clients.All.NotifyAdmin_Signup();
    }
}

WPF 项目中的 MainWindow.xaml.cs:

public partial class MainWindow : Window
{
    public MainWindow()
    {
         InitializeComponent();

         var hubConnection = new HubConnection("http://localhost:8080/");
         IHubProxy _hub = hubConnection.CreateHubProxy("TestHub");
         hubConnection.Start().Wait();

         //Call a local method when the Server sends a signal 
         _hub.On("NotifyStation_Signup", x => PrintAccountCount());
    }

    //This never gets called :(
    private void PrintAccountCount()
    {
         //Display a Message in the Window UI
         var dispatcher = Application.Current.Dispatcher;
         dispatcher.Invoke(() => counter_accounts.Content = "I got the signal!");
    }
}

没有没有错误。 WPF 项目的“NotifyStation_Signup”方法永远不会被服务器调用。我做错了什么?

【问题讨论】:

    标签: c# wpf signalr signalr-hub signalr.client


    【解决方案1】:

    尝试在启动集线器连接之前注册事件。

         var hubConnection = new HubConnection("http://localhost:8080/");
         IHubProxy _hub = hubConnection.CreateHubProxy("TestHub");
    
         //Call a local method when the Server sends a signal 
         _hub.On("NotifyStation_Signup", x => PrintAccountCount());
    
         hubConnection.Start().Wait();
    

    【讨论】:

    • 同样的结果。不被称为兄弟
    【解决方案2】:

    解决了!我在 using() 方法之外调用 hub 方法犯了一个愚蠢的错误:

    static void Main(string[] args)
    {
        //Start the Local server
        string url = @"http://localhost:8080/";
        using (WebApp.Start<Startup>(url))
        {
            Console.WriteLine(string.Format("Server running at {0}", url));
            //Instead of having the following two lines outside of this, 
            //I put it in here and it worked :)
            IHubContext hubContext = 
                     GlobalHost.ConnectionManager.GetHubContext<TestHub>();
            hubContext.Clients.All.NotifyAdmin_Signup();
            Console.ReadLine();
        }
    }
    

    【讨论】:

      猜你喜欢
      • 2020-02-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-10-03
      • 2018-04-20
      相关资源
      最近更新 更多