如果 WPF 和 WebForms 应用程序都连接到同一台服务器,那么这很容易实现。
设置 SignalR 集线器:
public class ProgressHub : Hub {
}
加载 WebForms 应用程序时,以普通方式加载/显示当前进度。设置 SignalR 以获取进度的实时更新:
var appHubProxy = $.connection.appHub;
appHubProxy.client.progress = function (orderId, percent) {
console.log(orderId + ': ' + percent);
};
$.connection.hub.start()
WPF 应用程序调用服务器来更新进度(使用例如 WebAPI),在此处理程序中调用信号器客户端进度方法:
public class ProgressController : ApiController {
public void Post(string orderId, int percent) {
// <Save progress to DB, etc>
// Get instance of the SignalR ProgressHub context
var hubContext = GlobalHost.ConnectionManager.GetHubContext<ProgressHub>();
// Invoke the progress method on all connected clients.
// You probably want to use Groups to only send customers
// events for its own orders
hubContext.Clients.All.progress(orderId, percent);
}
}
或者您可以让 WPF 使用 .NET SignalR API 来调用集线器中的方法:
public class ProgressHub : Hub {
public void Progress(string orderId, int percent) {
// <Save progress to DB, etc>
// Invoke the progress method on all connected clients.
// You probably want to use Groups to only send customers
// events for its own orders
Clients.All.progress(orderId, percent);
}
}