从asp.net的外部集线器调用wpf信号客户端方法
本文关键字:wpf 信号 客户端 方法 调用 集线器 asp net 外部 | 更新日期: 2023-09-27 17:53:27
public async Task ApplyChangedSettings()
{
if (ConnectionTimeEntryHub.State == Microsoft.AspNet.SignalR.Client.ConnectionState.Connected)
{
var d = await HubProxy.Invoke<TimeEntryV2.Models.Models.UpdatingSettings>("GetSettings", User.UserIdentity);
TimeEntrySettings = d;
}
}
上面的方法是在wpf客户端视图模型中创建的,我想在我的asp.net web应用程序中按照文档
从类外部调用这个方法。var hubContext = Microsoft.AspNet.SignalR.GlobalHost.ConnectionManager.GetHubContext<TimeEntryHub
s.TimeEntrySettingsHub>();
if (TimeEntryHubs.TimeEntrySettingsHub.Users.Any(f => f.Value == userId))
{
var connectionID = TimeEntryHubs.TimeEntrySettingsHub.Users.Where(f => f.Value == userId).Single().Key;
hubContext.Clients.Client(connectionID).ApplyChangedSettings();
}
我得到hub上下文如上所述,但它没有做任何事情,它被默默地执行,没有任何错误,不反映任何更改
我逐步通过代码,它没有调用。net客户端的方法,客户端方法的断点没有到达
使用Dependencyresolver解决了这个问题
服务器端
var dependencyResolver = GlobalHost.DependencyResolver;
var connectionManager = dependencyResolver.Resolve<IConnectionManager>();
var hubContext = connectionManager.GetHubContext<TimeEntryHubs.TimeEntrySettingsHub>();
if (TimeEntryHubs.TimeEntrySettingsHub.Users.Any(f => f.Key == userId))
{
var connectionID = TimeEntryHubs.TimeEntrySettingsHub.Users.Where(f => f.Key == userId).Single().Value;
hubContext.Clients.Client(connectionID).ApplyChangedSettings();
}
客户端
HubProxy.On("ApplyChangedSettings", () => ApplyChangedSettings().ConfigureAwait(false));
public async Task ApplyChangedSettings()
{
if (ConnectionTimeEntryHub.State == Microsoft.AspNet.SignalR.Client.ConnectionState.Connected)
{
await ApplySettings().ConfigureAwait(false);
}
}