【发布时间】:2017-08-03 23:07:46
【问题描述】:
所以,首先声明一下——我是这个异步编程的新手。因此,在我开始实施我当前的项目之前,我一直在运行一系列测试以消除问题并更好地了解整个事情。
我今天遇到了一个与 HttpContext.Current.Session 为 null 相关的问题,这恰好遍布我当前的代码库。
这是我的小测试应用:
public partial class RandomTests : System.Web.UI.Page
{
protected async void Page_Load(object sender, EventArgs e)
{
Debug.WriteLine("----------------------------------------------------------------------------------");
Debug.WriteLine("Blend dual call start");
Debug.WriteLine("Operation complete. Returned: " + await BlendDualCall() + " at " + DateTime.Now.ToString("mm':'ss':'fff"));
Debug.WriteLine("Blend dual call complete");
Debug.WriteLine("----------------------------------------------------------------------------------");
}
public async Task<string> BlendDualCall()
{
var cTask = Task.Run(() => YLogin(1000));
var fTask = Task.Run(() => FLogin(2000));
Debug.WriteLine("Awaiting results.");
var yResult = await cTask;
Debug.WriteLine("YLogin result returned at " + DateTime.Now.ToString("mm':'ss':'fff"));
var fResult = await fTask;
Debug.WriteLine("FLogin result returned at " + DateTime.Now.ToString("mm':'ss':'fff"));
return yResult + " " + fResult;
}
#region Non-Async Methods
public string FLogin(int waitTime)
{
Debug.WriteLine("FLogin process executed on thread id: " + Thread.CurrentThread.ManagedThreadId);
Thread.Sleep(waitTime);
//Session is null here
return HttpContext.Current.Session.SessionID + "_" + Thread.CurrentThread.ManagedThreadId;
}
public string YLogin(int waitTime)
{
Debug.WriteLine("YLogin process executed on thread id: " + Thread.CurrentThread.ManagedThreadId);
Thread.Sleep(waitTime);
//Session is not null here (System.Web.SessionState.HttpSessionState). Whats the difference?
return Session.SessionID + "_" + Thread.CurrentThread.ManagedThreadId;
}
#endregion
}
web.config 中的自定义 SessionState 提供程序:
<sessionState mode="Custom" customProvider="RedisSessionProvider">
<providers>
<add name="RedisSessionProvider" type="Microsoft.Web.Redis.RedisSessionStateProvider" host="******" port="***" accessKey="********" ssl="true" />
</providers>
</sessionState>
我正在寻找一些关于 SessionState 发生了什么以及为什么它在异步调用中为空的理解。有趣的是 HttpContext.Current.Session.SessionID 为空但 Session.SessionID 不为空,我怀疑它使用的是不同的 SessionState 提供程序?关于该主题,重要的是要注意我使用 RedisSessionProvider 作为自定义 SessionState 提供程序(用于 Azure)。我需要对 RedisSessionProvider 做些什么来让它处理异步调用吗?有人能指出我正确的方向吗?也许可以解释一下 SessionState 如何处理异步调用?
TIA
【问题讨论】:
标签: c# azure asynchronous redis