在asp.net中从外部集线器调用wpf signalr客户端方法

时间:2015-06-05 10:05:46

标签: c# asp.net wpf signalr

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();
}

我正在获取上面的集线器上下文,但它没有做任何事情,它会以无任何错误的方式静默执行,并且不反映任何更改

我介入了代码,它没有调用.net客户端上的方法,未达到客户端方法的断点

1 个答案:

答案 0 :(得分:0)

使用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);
        }
 }