Skip to content

Instantly share code, notes, and snippets.

@patroza
Forked from lethek/AirtimeHub.cs
Last active September 4, 2015 07:08
Show Gist options
  • Save patroza/10999735 to your computer and use it in GitHub Desktop.
Save patroza/10999735 to your computer and use it in GitHub Desktop.
Using Mef to inject dependencies into SignalR hubs and manage dependency lifetimes. Currently makes assumptions based on using the request scoped container implementation from https://github.com/sickboy/Heliar-Web-Composition/tree/develop
public abstract class BaseHub : Hub
{
public EventHandler Disposing;
bool _disposed;
protected override void Dispose(bool disposing) {
if (_disposed)
return;
_disposed = true;
base.Dispose(disposing);
if (!disposing)
return;
var handler = Disposing;
if (handler != null)
handler(this, EventArgs.Empty);
}
}
public static class ContainerExtensions
{
const CompositionOptions DefaultCompositionOptions =
CompositionOptions.DisableSilentRejection | CompositionOptions.IsThreadSafe;
public static CompositionContainer BeginScope(this CompositionContainer parentScope) {
var filteredCatalog = (FilteredCatalog) parentScope.Catalog;
return parentScope.BeginScope(filteredCatalog.Complement);
}
static CompositionContainer BeginScope(this ExportProvider parentScope, ComposablePartCatalog catalog) {
var compositionContainer = new CompositionContainer(catalog, DefaultCompositionOptions, parentScope);
ConfigureContainer(compositionContainer);
return compositionContainer;
}
static void ConfigureContainer(CompositionContainer compositionContainer) {
var compositionBatch = new CompositionBatch();
compositionBatch.AddExportedValue(compositionContainer);
compositionBatch.AddExportedValue(compositionContainer.Catalog);
compositionContainer.Compose(compositionBatch);
}
}
public static class DependencyConfig
{
internal static CompositionProvider CompositionProvider;
public static void RegisterDependencies() {
var configs = new List<IResolverConfiguration> {
new MvcResolverConfiguration(),
new WebApiResolverConfiguration()
};
CompositionProvider = new CompositionProvider(new CompositionCatalog(), configs.ToArray());
}
}
public class CompositionCatalog : WebApplicationCatalog
{
public CompositionCatalog() : base(SelectAssemblies()) {}
static IEnumerable<Assembly> SelectAssemblies() {
return new[] {Assembly.GetExecutingAssembly()}.Distinct();
}
protected override RegistrationBuilder DefineConventions(RegistrationBuilder conventions = null) {
conventions = base.DefineConventions(conventions);
conventions.ForType<MefScopeManager>()
.SetCreationPolicy(CreationPolicy.Shared)
.Export<IScopeManager>();
conventions.ForType<MefHubActivator>()
.SetCreationPolicy(CreationPolicy.Shared)
.ExportInterfaces();
conventions.ForTypesDerivedFrom<BaseHub>()
.SetCreationPolicy(CreationPolicy.NonShared)
.Export<IHubActivator>();
return conventions;
}
}
public interface IScopeManager
{
T CreateHub<T>(Type hubType) where T : BaseHub;
}
[ApplicationShared]
public class MefHubActivator : IHubActivator
{
readonly IScopeManager _scopeManager;
public MefHubActivator(IScopeManager scopeManager) {
_scopeManager = scopeManager;
}
public IHub Create(HubDescriptor descriptor) {
return typeof (BaseHub).IsAssignableFrom(descriptor.HubType)
? _scopeManager.CreateHub<BaseHub>(descriptor.HubType)
: (IHub) Activator.CreateInstance(descriptor.HubType);
}
}
[ApplicationShared]
public class MefScopeManager : IScopeManager
{
readonly CompositionContainer _rootScope;
readonly ConcurrentDictionary<IHub, CompositionContainer> _hubLifetimeScopes =
new ConcurrentDictionary<IHub, CompositionContainer>();
public MefScopeManager(CompositionContainer rootScope) {
_rootScope = rootScope;
}
public T CreateHub<T>(Type hubType) where T : BaseHub {
var scope = _rootScope.BeginScope();
var hub = scope.GetInstance<T>(hubType);
hub.Disposing += HubOnDisposing;
_hubLifetimeScopes.TryAdd(hub, scope);
return hub;
}
void HubOnDisposing(object sender, EventArgs eventArgs) {
var hub = sender as BaseHub;
if (hub == null)
return;
hub.Disposing -= HubOnDisposing;
CompositionContainer scope;
if (!_hubLifetimeScopes.TryRemove(hub, out scope))
return;
scope.Dispose();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment