// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using System.Diagnostics.Contracts;
using System.Web.Http.Properties;
using Microsoft.AspNet.SignalR;
using Microsoft.AspNet.SignalR.Hubs;
using Microsoft.AspNet.SignalR.Infrastructure;
namespace System.Web.Http
{
///
/// Defines a base class for that exposes functionality for calling back
/// to clients connected to a particular SignalR hub.
///
[CLSCompliant(false)]
public abstract class HubControllerBase : ApiController
{
private IConnectionManager _connectionManager;
///
/// Initializes a new instance of the class.
///
protected HubControllerBase()
{
}
///
/// Gets an that represents the clients connected to the hub.
///
public IHubConnectionContext Clients
{
get
{
if (HubContext == null)
{
throw Error.InvalidOperation(SRResources.NullHubContext, GetType().Name);
}
return HubContext.Clients;
}
}
///
/// Gets the for the hub.
///
public IGroupManager Groups
{
get
{
if (HubContext == null)
{
throw Error.InvalidOperation(SRResources.NullHubContext, GetType().Name);
}
return HubContext.Groups;
}
}
///
/// Gets the for the associated hub.
///
protected abstract IHubContext HubContext
{
get;
}
///
/// Gets the used to resolve hub contexts. The connection manager will
/// be resolved by the controller's dependency resolver if the service is
/// available. Otherwise, the default GlobalHost will be returned instead.
///
protected IConnectionManager ConnectionManager
{
get
{
if (_connectionManager == null)
{
_connectionManager = ResolveConnectionManager();
}
return _connectionManager;
}
}
private IConnectionManager ResolveConnectionManager()
{
if (Configuration != null)
{
Contract.Assert(Configuration.DependencyResolver != null);
IConnectionManager connectionManager = Configuration.DependencyResolver.GetService(typeof(IConnectionManager)) as IConnectionManager;
if (connectionManager != null)
{
return connectionManager;
}
}
// If connection manager cannot be resolved by DependencyResolver, use the default connection manager instead.
return GlobalHost.ConnectionManager;
}
}
}