Dispose the ConnectionManager on application shutdown

This commit is contained in:
David Fowler 2016-11-02 23:14:12 -07:00
parent 8d03c014fb
commit f51fcadeb1
3 changed files with 44 additions and 12 deletions

View File

@ -5,7 +5,7 @@ using Channels;
namespace Microsoft.AspNetCore.Sockets
{
public class ConnectionManager
public class ConnectionManager : IDisposable
{
private ConcurrentDictionary<string, ConnectionState> _connections = new ConcurrentDictionary<string, ConnectionState>();
private Timer _timer;
@ -92,5 +92,28 @@ namespace Microsoft.AspNetCore.Sockets
}
}
}
public void Dispose()
{
// Stop firing the timer
_timer.Dispose();
foreach (var c in _connections)
{
ConnectionState s;
if (_connections.TryRemove(c.Key, out s))
{
// Longpolling connections should do this
if (s.Close != null)
{
s.Close();
}
else
{
s.Connection.Channel.Dispose();
}
}
}
}
}
}

View File

@ -1,8 +1,10 @@
using System;
using Channels;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Routing;
using Microsoft.AspNetCore.Sockets;
using Microsoft.AspNetCore.Sockets.Routing;
using Microsoft.Extensions.DependencyInjection;
namespace Microsoft.AspNetCore.Builder
{
@ -12,7 +14,13 @@ namespace Microsoft.AspNetCore.Builder
{
var manager = new ConnectionManager();
var factory = new ChannelFactory();
var dispatcher = new HttpConnectionDispatcher(manager, factory);
// Dispose the connection manager when application shutdown is triggered
var lifetime = app.ApplicationServices.GetRequiredService<IApplicationLifetime>();
lifetime.ApplicationStopping.Register(state => ((IDisposable)state).Dispose(), manager);
var routes = new RouteBuilder(app);
callback(new SocketRouteBuilder(routes, dispatcher));

View File

@ -1,13 +1,14 @@
{
"version": "0.1.0-*",
"dependencies": {
"Channels": "0.2.0-beta-*",
"Microsoft.AspNetCore.Routing": "1.1.0-*",
"Microsoft.AspNetCore.WebSockets": "0.2.0-*",
"NETStandard.Library": "1.6.1-*"
},
"frameworks": {
"netstandard1.3": {},
"net46": {}
}
"version": "0.1.0-*",
"dependencies": {
"Channels": "0.2.0-beta-*",
"Microsoft.AspNetCore.Hosting.Abstractions": "1.1.0-*",
"Microsoft.AspNetCore.Routing": "1.1.0-*",
"Microsoft.AspNetCore.WebSockets": "0.2.0-*",
"NETStandard.Library": "1.6.1-*"
},
"frameworks": {
"netstandard1.3": {},
"net46": {}
}
}