diff --git a/samples/SocketsSample/EndPoints/HubEndPoint.cs b/samples/SocketsSample/EndPoints/HubEndPoint.cs index 7e89efde20..827425e554 100644 --- a/samples/SocketsSample/EndPoints/HubEndPoint.cs +++ b/samples/SocketsSample/EndPoints/HubEndPoint.cs @@ -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(connection, _lifetimeManager); diff --git a/samples/SocketsSample/EndPoints/Hubs/Proxies.cs b/samples/SocketsSample/EndPoints/Hubs/Proxies.cs index 2012abb258..8f534fa85c 100644 --- a/samples/SocketsSample/EndPoints/Hubs/Proxies.cs +++ b/samples/SocketsSample/EndPoints/Hubs/Proxies.cs @@ -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 : IGroupManager { private readonly Connection _connection; - private HubLifetimeManager _lifetimeManager; + private readonly HubLifetimeManager _lifetimeManager; public GroupManager(Connection connection, HubLifetimeManager lifetimeManager) { diff --git a/samples/SocketsSample/EndPoints/RpcEndpoint.cs b/samples/SocketsSample/EndPoints/RpcEndpoint.cs index 270cee717e..8896bae96f 100644 --- a/samples/SocketsSample/EndPoints/RpcEndpoint.cs +++ b/samples/SocketsSample/EndPoints/RpcEndpoint.cs @@ -118,7 +118,15 @@ namespace SocketsSample using (var scope = _serviceScopeFactory.CreateScope()) { - var value = scope.ServiceProvider.GetService() ?? Activator.CreateInstance(); + var value = scope.ServiceProvider.GetService(); + + bool created = false; + + if (value == null) + { + value = Activator.CreateInstance(); + 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; diff --git a/samples/SocketsSample/Hubs/Hub.cs b/samples/SocketsSample/Hubs/Hub.cs index f8b6e22ed8..6bb677eae7 100644 --- a/samples/SocketsSample/Hubs/Hub.cs +++ b/samples/SocketsSample/Hubs/Hub.cs @@ -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); + } + } } } diff --git a/samples/SocketsSample/Startup.cs b/samples/SocketsSample/Startup.cs index f9c7f8853b..daef8b93ad 100644 --- a/samples/SocketsSample/Startup.cs +++ b/samples/SocketsSample/Startup.cs @@ -21,8 +21,6 @@ namespace SocketsSample services.AddSingleton(typeof(RpcEndpoint<>), typeof(RpcEndpoint<>)); services.AddSingleton(); - services.AddSingleton(); - services.AddSingleton(); services.AddSingleton(); }