【问题标题】:How can we implement Windows desktop application integrate with SignalR based on ASP.NET Boilerplate?我们如何实现基于 ASP.NET Boilerplate 与 SignalR 集成的 Windows 桌面应用程序?
【发布时间】:2021-05-28 14:01:55
【问题描述】:

我们的基础项目使用 ASP.NET Boilerplate、ASP.NET MVC、AngularJS 实现。 我们有与 Web API 项目通信的桌面应用程序。 我们如何在桌面应用程序中使用 SignalR?

【问题讨论】:

    标签: asp.net-mvc signalr aspnetboilerplate


    【解决方案1】:

    在 windows 桌面框架 4.8 中添加来自 NuGet 的 Microsoft.AspNetCore.Http.Connections.Client

    创建 Hub 连接方法

    地图接收事件hubConnection.On

    StartAsync()开头

    HubConnection hubConnection;
    string _messageRecived;
    
    async Task InizializzaConnectioTuBlazorHub()
    {
        hubConnection = new HubConnectionBuilder()
       .WithUrl("http://localhost:5000/chathub")
       .Build();
    
        hubConnection.On<string>("ReceiveMessage", (message) =>
        {
            _messageRecived = message;
            textBox1.Text= _messageRecived;
        });
    
        await hubConnection.StartAsync();
    }
    

    然后,发送消息。

    async Task Send()
    {
        if (hubConnection != null)
        {
            await hubConnection.SendAsync("SendMessage", "Desktop: Ciao");
        }
    }
    

    请参阅链接中的Blazor Communicates with Win Desktop App through SignalR,我已经提出了在 Blazor 和 Windows 窗体之间进行通信的提示。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-10-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-08-08
      相关资源
      最近更新 更多