gpt4 book ai didi

c# - 如何在应用程序关闭/启动时保存/恢复 SignalR Hub、连接、状态

转载 作者:行者123 更新时间:2023-11-30 18:29:37 25 4
gpt4 key购买 nike

我看了这个excellent SO post但它没有给我足够的细节。我想存储有关 SignalR Hub 的所有信息——它是连接、状态、应用程序关闭时的用户和应用程序启动时的检索。我们有一个问题,如果我们通过 SignalR 请求数据,然后我们对网站进行 Web 发布,客户端似乎会失去连接。

这是我现在正在做的。在 OnDisconnected 中:

var connection = db.SignalRConnections.Find(Context.ConnectionId);
connection.Connected = false;
db.SaveChanges();

在 OnConnected 中:

public override Task OnConnected()
{
try
{
var name = Context.User.Identity.Name;

using (savitasEntities2 entities = new savitasEntities2())
{
var user = entities.SignalRUsers
.Include(u => u.SignalRConnections)
.SingleOrDefault(u => u.UserName == name);

if (user == null)
{
user = new SignalRUser
{
UserName = name,
SignalRConnections = new List<SignalRConnection>()
};
entities.SignalRUsers.Add(user);
}

user.SignalRConnections.Add(new SignalRConnection
{
ConnectionID = Context.ConnectionId,
UserAgent = Context.Request.Headers["User-Agent"],
Connected = true
});
entities.SaveChanges();
}

}

这是数据库中的两个表:

 public partial class SignalRConnection
{
public string ConnectionID { get; set; }
public string UserAgent { get; set; }
public bool Connected { get; set; }
public string SignalRUser_UserName { get; set; }

public virtual SignalRUser SignalRUser { get; set; }
}

public partial class SignalRUser
{
public SignalRUser()
{
this.SignalRConnections = new HashSet<SignalRConnection>();
}

public string UserName { get; set; }

public virtual ICollection<SignalRConnection> SignalRConnections { get; set; }
}
}

我是否应该在 Global.asax 中对启动/关闭执行其他操作?我还有一个线程安全的字典来路由我们的数据:

public static ConcurrentDictionary<string, DataSet> publicDataDictionary = new ConcurrentDictionary<string, DataSet>();

那也应该连载吗?谢谢!

最佳答案

var connection = db.SignalRConnections.Find(Context.ConnectionId);
connection.Connected = false;
db.SaveChanges();

和 OnConnected:

public override Task OnConnected()
{
try
{
var name = Context.User.Identity.Name;

using (savitasEntities2 entities = new savitasEntities2())
{
var user = entities.SignalRUsers
.Include(u => u.SignalRConnections)
.SingleOrDefault(u => u.UserName == name);

if (user == null)
{
user = new SignalRUser
{
UserName = name,
SignalRConnections = new List<SignalRConnection>()
};
entities.SignalRUsers.Add(user);
}

user.SignalRConnections.Add(new SignalRConnection
{
ConnectionID = Context.ConnectionId,
UserAgent = Context.Request.Headers["User-Agent"],
Connected = true
});
entities.SaveChanges();
}

}

数据库表是:

 public partial class SignalRConnection
{
public string ConnectionID { get; set; }
public string UserAgent { get; set; }
public bool Connected { get; set; }
public string SignalRUser_UserName { get; set; }

public virtual SignalRUser SignalRUser { get; set; }
}
public partial class SignalRUser
{
public SignalRUser()
{
this.SignalRConnections = new HashSet<SignalRConnection>();
}

public string UserName { get; set; }

public virtual ICollection<SignalRConnection> SignalRConnections { get; set; }
}
}

我可能什么时候在 Global.asax 中对启动/关闭执行其他操作?

关于c# - 如何在应用程序关闭/启动时保存/恢复 SignalR Hub、连接、状态,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22672353/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com