我正在开发一个需要异步 Web 服务调用的 Asp.Net 3.5 Web 应用程序。这是一些代码。我已经编写了使用委托进行异步调用的代码,但不知何故我的 HttpContext.Current.Session 为空。为了确保,我什至尝试传递 HttpContext。
static HttpContext httpContext;
public void AsyncGetUser()
{
httpContext = HttpContext.Current;//not getting Session as null here
GetUserDelegate delegate = new GetUserDelegate(InvokeWrapperMethod);
IAsyncResult async = delegate.BeginInvoke(httpContext,new AsyncCallback(CallbackMethod), null);
}
static void CallbackMethod(IAsyncResult ar)
{
AsyncResult result = (AsyncResult)ar;
GetUserDelegate caller = (GetUserDelegate)result.AsyncDelegate;
caller.EndInvoke(ar);
}
private static void InvokeWrapperMethod(HttpContext hc)
{
HttpContext.Current = hc; //getting Session as null here
UserInfo userInfo = Service.GetUserInfo();
SetInSession(USER_INFO, userInfo);
}
我已经尝试<modules runAllManagedModulesForAllRequests="true">
过
<system.webServer>
<modules>
<remove name="Session"/>
<add name="Session" type="System.Web.SessionState.SessionStateModule"/>
</modules>
正如这个SO question所建议的那样,但没有奏效。如果你们能给一些指点,我将不胜感激。谢谢。