问题描述
功能要求
业务上要实现服务器端定时向客户端推送某些最新消息,方案的思路是:当客户端连接上来时记录下它的ConnectionId,使用Timer创建定时任务,定时向该ConnectionId的客户端发送消息。
异常位置
在定时任务中使用Clients.Client(connId) 或其他方法获取客户端连接时冒出ObjectDisposedException。
异常位置代码:
public async void LoopTask(object state)
{
// state是ConnectionId
string connId = state.ToString();
// 任何方式获取客户端连接都会冒ObjectDisposedException
var client = Clients.Client(connId);
string arg = "this is an argument";
await client.SendAsync("Test", arg);
}
有问题的代码总览:
public class TestHub : Hub
{
// 定时任务字典。Dictionary不是线程安全的,实际应用中应该加锁或者使用ConcurrentDictionary
private static Dictionary<string, Timer> Tasks { get; set; }
static TestHub()
{
Tasks = new Dictionary<string, Timer>();
}
public override Task OnDisconnectedAsync(Exception exception)
{
string connId = Context.ConnectionId;
if(Tasks.TryGetValue(connId,out Timer timer))
{
timer.Dispose();
}
return base.OnDisconnectedAsync(exception);
}
public override Task OnConnectedAsync()
{
string connId = Context.ConnectionId;
// 0毫秒后开始每5秒执行一次
Timer timer = new Timer(LoopTask, connId, 0, 5000);
Tasks.Add(connId, timer);
return base.OnConnectedAsync();
}
public async void LoopTask(object state)
{
string connId = state.ToString();
// 任何方式获取客户端连接都会冒ObjectDisposedException
var client = Clients.Client(connId);
string arg = "this is an argument";
await client.SendAsync("Test", arg);
}
}
解决办法
因为Hub是临时性的,请求完成后会dispose掉,所以所有不在请求内的操作都不能直接使用Hub里面的对象(Clients, Groups等)获取客户端连接。
这里要单独开一个客户端连接的类,利用Asp.net core自带依赖注入将IHubContext注入到类中,在类中用IHubContext进行操作。
这里要注意的是,Asp.net 和Asp.net core是不一样的,Asp.net中可以用GlobalHost中获取到IHubContext,而Asp.net core中就不能这样获取。
修改后代码总览:
public class ClientConnection : IDisposable
{
// 连接标识,也可以是userId等,反正能定位到客户端就行
public string ConnectionId { get; set; }
// HubContext,用于获取客户端连接
private IHubContext<TestHub> Context { get; set; }
// 定时任务
private Timer Timer { get; set; }
public ClientConnection(IHubContext<TestHub> hubContext, string connId)
{
this.ConnectionId = connId;
this.Context = hubContext;
// 0毫秒后开始每5秒执行一次
Timer = new Timer(LoopTask, null, 0, 5000);
}
public async void LoopTask(object state)
{
var client = Context.Clients.Client(this.ConnectionId);
string arg = "this is an argument";
await client.SendAsync("Test", arg);
}
public void Dispose()
{
this.Timer.Dispose();
}
}
public class TestHub : Hub
{
// 用于获取依赖注入对象
private IServiceProvider Service { get; set; }
// 这里其实不应该把连接保存在Hub中,demo的话随便了(记得加锁)
private static List<ClientConnection> Connections;
static TestHub()
{
Connections = new List<ClientConnection>();
}
public TestHub(IServiceProvider service)
{
Service = service;
}
public override Task OnDisconnectedAsync(Exception exception)
{
string connId = Context.ConnectionId;
var conn = Connections.Find(s => s.ConnectionId == connId);
if(conn != null)
{
Connections.Remove(conn);
conn.Dispose();
}
return base.OnDisconnectedAsync(exception);
}
public override Task OnConnectedAsync()
{
string connId = Context.ConnectionId;
// 这里不用在StartUp里面AddScoped也可以获取到
IHubContext<TestHub> context = Service.GetService(typeof(IHubContext<TestHub>)) as IHubContext<TestHub>;
ClientConnection conn = new ClientConnection(context, connId);
Connections.Add(conn);
return base.OnConnectedAsync();
}
}
问题原因
问题的原因就在于Hub实例的暂时性,跟Controller一样,一个请求生成一个实例,似乎也合情合理。但是,虽然Hub是临时性的但是它管理的连接是持久的(WebSocket长连接),这就很有迷惑性,容易让人以为Hub也是持久存在的。
最后,建议入坑的过程中还是要多看文档,其实微软的文档确实做得不错,虽然很多都是机器翻译,而且词汇都比较晦涩,但重点都会有特别说明,这点可以给个赞。