【发布时间】:2021-01-20 02:43:11
【问题描述】:
我正在使用具有[ThreadStatic] 属性的变量来存储每个套接字的当前活动Session(我使用WebSocket 作为连接类型)。在套接字connect 的开头,我将其值分配给一个新的Session 对象,然后执行Task 方法以获取输出。 Task完成后,Session的值变成null。
ThreadStatic 声明:
public class Session
{
...
[ThreadStatic] public static Session _session;
}
要调用的任务方法:
public async Task<string> Connect()
{
var path = Path.Combine(Directory.GetCurrentDirectory(), "storyboard", "1.json");
var json = await File.ReadAllTextAsync(path, Encoding.UTF8);
return $"\"i\":{this.id},\"m\":\"build\",\"a\":{json}";
}
任务执行部分:
// Session._session == null ? --> false (here, the value still exists)
var res = await obj.Item1.Execute(met, args); // execute a method with async call
await Session._session.Send(res); // Session._session == null ? --> true
这里有什么问题?
【问题讨论】:
-
为什么不在最终代码示例中将其保存在局部变量中?
var session = new Session();仍应在最后一行正确设置session。 -
这只是一个例子,我在另一个地方(不是本地)初始化了
Session._session值,我需要在很多地方使用它。 -
那是因为在 ASP.NET Core 中有no synchronization context。继续在随机线程上恢复。即使在 ASP.NET 非 Core 中,方法也是 flawed。
-
还有其他方法可以解决这个限制吗?
标签: c# asp.net-core threadstatic