【发布时间】:2019-10-31 14:27:02
【问题描述】:
我采用了一种方法,将signalr 与用户的连接映射到我的aspnet core 3.0 应用程序。
我所指的方法在Mapping SignalR Users to Connections,Permanent, external storage 部分中进行了概述。我知道这篇文章是为不同版本的 Asp.Net 编写的,但它派上了用场。
这是 Hub 的代码:
public class SomeHub : Hub
{
private readonly UserManager _userManager;
private readonly AppDbContext _dbContext;
protected BaseHub(UserManager userManager, AppDbContext dbContext)
{
_userManager = userManager;
_dbContext = dbContext;
}
public override async Task OnConnectedAsync()
{
var user = await _userManager.GetMe();
user.Connections.Add(new Connection { ConnectionId = Context.ConnectionId });
await _dbContext.SaveChangesAsync();
await base.OnConnectedAsync();
}
public override async Task OnDisconnectedAsync(Exception ex)
{
var user = await _userManager.GetMe();
if (await _dbContext.Connections.FindAsync(Context.ConnectionId) is {} connection)
{
user.Connections.Remove(connection);
await _dbContext.SaveChangesAsync();
}
await base.OnDisconnectedAsync(ex);
}
}
问题
如果我拆除我的应用程序,Connection 数据库条目将保留在我的数据库中,因为未调用 OnDisconnectedAsync 方法。
是否有可能在应用程序启动后删除这些条目?
【问题讨论】:
-
该文档适用于旧的 ASP.NET SignalR,而不是 ASP.NET Core SignalR。文章顶部的彩色面板中有一个非常强烈的警告:
This documentation isn't for the latest version of SignalR. Take a look at ASP.NET Core SignalR. -
这是真的,请查看问题的更新版本。似乎仍然没有最新版本的匹配文章。至少我没有找到。
标签: c# asp.net-core entity-framework-core signalr c#-8.0