【发布时间】:2021-05-28 14:01:55
【问题描述】:
我们的基础项目使用 ASP.NET Boilerplate、ASP.NET MVC、AngularJS 实现。 我们有与 Web API 项目通信的桌面应用程序。 我们如何在桌面应用程序中使用 SignalR?
【问题讨论】:
标签: asp.net-mvc signalr aspnetboilerplate
我们的基础项目使用 ASP.NET Boilerplate、ASP.NET MVC、AngularJS 实现。 我们有与 Web API 项目通信的桌面应用程序。 我们如何在桌面应用程序中使用 SignalR?
【问题讨论】:
标签: asp.net-mvc signalr aspnetboilerplate
在 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 窗体之间进行通信的提示。
【讨论】: