Skip to content

Instantly share code, notes, and snippets.

@deluvas
Created August 27, 2017 08:37
Show Gist options
  • Save deluvas/62e2395e94aba1fdbb6f29c55e4f2fdf to your computer and use it in GitHub Desktop.
Save deluvas/62e2395e94aba1fdbb6f29c55e4f2fdf to your computer and use it in GitHub Desktop.
SignalR client connection extensions
using System;
using System.Collections.Generic;
using System.Reflection;
using Microsoft.AspNet.SignalR.Client;
using Microsoft.AspNet.SignalR.Client.Hubs;
namespace YourNamespace
{
public static class HubConnectionExtensions
{
/// <summary>
/// Attempts to add a hub proxy to the connection even after connecting to the socket
/// </summary>
/// <example>
/// Add the hub proxy after connecting to the socket:
/// <code>
/// await _connection.Start( ... );
/// _hub = _connection.CreateHubProxyLate( ... );
/// </code>
/// </example>
public static IHubProxy CreateHubProxyLate( this HubConnection connection, string hubName )
{
var _hubs = typeof( HubConnection ).GetField( "_hubs", BindingFlags.NonPublic | BindingFlags.Instance )
.GetValue( connection ) as IDictionary<string,HubProxy>;
HubProxy hubProxy;
if ( !_hubs.TryGetValue( hubName, out hubProxy ) ) {
hubProxy = new HubProxy( connection, hubName );
_hubs[hubName] = hubProxy;
}
return hubProxy;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment