Some cleanup and error checking

- Dispose hubs after invoke
This commit is contained in:
David Fowler 2016-11-01 23:27:49 -07:00
parent 53858495dc
commit 4221db3890
5 changed files with 90 additions and 14 deletions

View File

@ -68,14 +68,13 @@ namespace SocketsSample
} }
} }
protected override void BeforeInvoke(Connection connection, THub endpoint) protected override void BeforeInvoke(Connection connection, THub hub)
{ {
Initialize(connection, endpoint); Initialize(connection, hub);
} }
private void Initialize(Connection connection, THub endpoint) private void Initialize(Connection connection, THub hub)
{ {
var hub = endpoint;
hub.Clients = this; hub.Clients = this;
hub.Context = new HubCallerContext(connection); hub.Context = new HubCallerContext(connection);
hub.Groups = new GroupManager<THub>(connection, _lifetimeManager); hub.Groups = new GroupManager<THub>(connection, _lifetimeManager);

View File

@ -51,7 +51,6 @@ namespace SocketsSample.EndPoints.Hubs
public Task Invoke(string method, params object[] args) public Task Invoke(string method, params object[] args)
{ {
// TODO: More than just chat
return _lifetimeManager.InvokeAll(method, args); return _lifetimeManager.InvokeAll(method, args);
} }
} }
@ -77,7 +76,7 @@ namespace SocketsSample.EndPoints.Hubs
public class GroupManager<THub> : IGroupManager public class GroupManager<THub> : IGroupManager
{ {
private readonly Connection _connection; private readonly Connection _connection;
private HubLifetimeManager<THub> _lifetimeManager; private readonly HubLifetimeManager<THub> _lifetimeManager;
public GroupManager(Connection connection, HubLifetimeManager<THub> lifetimeManager) public GroupManager(Connection connection, HubLifetimeManager<THub> lifetimeManager)
{ {

View File

@ -118,7 +118,15 @@ namespace SocketsSample
using (var scope = _serviceScopeFactory.CreateScope()) using (var scope = _serviceScopeFactory.CreateScope())
{ {
var value = scope.ServiceProvider.GetService<T>() ?? Activator.CreateInstance<T>(); var value = scope.ServiceProvider.GetService<T>();
bool created = false;
if (value == null)
{
value = Activator.CreateInstance<T>();
created = true;
}
BeforeInvoke(connection, value); BeforeInvoke(connection, value);
@ -146,6 +154,12 @@ namespace SocketsSample
{ {
AfterInvoke(connection, value); AfterInvoke(connection, value);
} }
if (created)
{
// Dispose the object if it's disposable and we created it
(value as IDisposable)?.Dispose();
}
} }
return invocationResult; return invocationResult;

View File

@ -1,9 +1,57 @@
using System.Threading.Tasks; using System;
using System.Threading.Tasks;
namespace SocketsSample.Hubs namespace SocketsSample.Hubs
{ {
public class Hub public class Hub : IDisposable
{ {
private bool _disposed;
private IHubConnectionContext _clients;
private HubCallerContext _context;
private IGroupManager _groups;
public IHubConnectionContext Clients
{
get
{
CheckDisposed();
return _clients;
}
set
{
CheckDisposed();
_clients = value;
}
}
public HubCallerContext Context
{
get
{
CheckDisposed();
return _context;
}
set
{
CheckDisposed();
_context = value;
}
}
public IGroupManager Groups
{
get
{
CheckDisposed();
return _groups;
}
set
{
CheckDisposed();
_groups = value;
}
}
public virtual Task OnConnectedAsync() public virtual Task OnConnectedAsync()
{ {
return Task.CompletedTask; return Task.CompletedTask;
@ -14,10 +62,28 @@ namespace SocketsSample.Hubs
return Task.CompletedTask; return Task.CompletedTask;
} }
public IHubConnectionContext Clients { get; set; } protected virtual void Dispose(bool disposing)
{
}
public HubCallerContext Context { get; set; } public void Dispose()
{
if (_disposed)
{
return;
}
public IGroupManager Groups { get; set; } Dispose(true);
_disposed = true;
}
private void CheckDisposed()
{
if (_disposed)
{
throw new ObjectDisposedException(GetType().Name);
}
}
} }
} }

View File

@ -21,8 +21,6 @@ namespace SocketsSample
services.AddSingleton(typeof(RpcEndpoint<>), typeof(RpcEndpoint<>)); services.AddSingleton(typeof(RpcEndpoint<>), typeof(RpcEndpoint<>));
services.AddSingleton<ChatEndPoint>(); services.AddSingleton<ChatEndPoint>();
services.AddSingleton<Chat>();
services.AddSingleton<ProtobufSerializer>(); services.AddSingleton<ProtobufSerializer>();
services.AddSingleton<InvocationAdapterRegistry>(); services.AddSingleton<InvocationAdapterRegistry>();
} }