Some cleanup and error checking
- Dispose hubs after invoke
This commit is contained in:
parent
53858495dc
commit
4221db3890
|
|
@ -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.Context = new HubCallerContext(connection);
|
||||
hub.Groups = new GroupManager<THub>(connection, _lifetimeManager);
|
||||
|
|
|
|||
|
|
@ -51,7 +51,6 @@ namespace SocketsSample.EndPoints.Hubs
|
|||
|
||||
public Task Invoke(string method, params object[] args)
|
||||
{
|
||||
// TODO: More than just chat
|
||||
return _lifetimeManager.InvokeAll(method, args);
|
||||
}
|
||||
}
|
||||
|
|
@ -77,7 +76,7 @@ namespace SocketsSample.EndPoints.Hubs
|
|||
public class GroupManager<THub> : IGroupManager
|
||||
{
|
||||
private readonly Connection _connection;
|
||||
private HubLifetimeManager<THub> _lifetimeManager;
|
||||
private readonly HubLifetimeManager<THub> _lifetimeManager;
|
||||
|
||||
public GroupManager(Connection connection, HubLifetimeManager<THub> lifetimeManager)
|
||||
{
|
||||
|
|
|
|||
|
|
@ -118,7 +118,15 @@ namespace SocketsSample
|
|||
|
||||
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);
|
||||
|
||||
|
|
@ -146,6 +154,12 @@ namespace SocketsSample
|
|||
{
|
||||
AfterInvoke(connection, value);
|
||||
}
|
||||
|
||||
if (created)
|
||||
{
|
||||
// Dispose the object if it's disposable and we created it
|
||||
(value as IDisposable)?.Dispose();
|
||||
}
|
||||
}
|
||||
|
||||
return invocationResult;
|
||||
|
|
|
|||
|
|
@ -1,9 +1,57 @@
|
|||
using System.Threading.Tasks;
|
||||
using System;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
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()
|
||||
{
|
||||
return Task.CompletedTask;
|
||||
|
|
@ -14,10 +62,28 @@ namespace SocketsSample.Hubs
|
|||
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);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -21,8 +21,6 @@ namespace SocketsSample
|
|||
services.AddSingleton(typeof(RpcEndpoint<>), typeof(RpcEndpoint<>));
|
||||
|
||||
services.AddSingleton<ChatEndPoint>();
|
||||
services.AddSingleton<Chat>();
|
||||
|
||||
services.AddSingleton<ProtobufSerializer>();
|
||||
services.AddSingleton<InvocationAdapterRegistry>();
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue