我已经经历过类似的关于httpContext.Current的查询在异步方法中变为null。
根据这篇文章 - > HttpContext.Current is null in an asynchronous Callback,HttpContext在异步函数(子线程)中不可用。
我已经在我的函数中实现了异步轮询,我的被调用函数使用了httpContext对象,该对象变为null。有人可以告诉我如何将HttpContext传递给子线程。
delegate DataSet MethodDelegate(SqlParameter[] paramFilterScreenCreate);
public DataSet Call()
{
dtFilter= ExecutePolling(paramFilterScreenCreate);
}
public DataSet LongRunningMethod( SqlParameter[] paramFilterScreenCreate)
{
DataSet dtFilter = new DataSet();
//call a stored procedure and connection string(Session Manager class - session.cs)
dtFilter = SqlHelper.ExecuteDataset(Utility.GetConnectionString(Constants.ASSET_MANAGEMENT), StoreProcedures.ROD_SCREEN_FILTER_EXECUTE, paramFilterScreenCreate);
return dtFilter;
}
public DataSet ExecutePolling(SqlParameter[] paramFilterScreenCreate)
{
MethodDelegate dlgt = LongRunningMethod;
// Initiate the asynchronous call.
IAsyncResult ar = dlgt.BeginInvoke(paramFilterScreenCreate, null, null);
// Poll IAsyncResult.IsCompleted
while (ar.IsCompleted == false)
{
Thread.Sleep(1000);
}
DataSet result = dlgt.EndInvoke(ar);
return result;
}
Class Session
{
/// <returns>Returns of the value of the sessoin for the specified key</returns>
public static T Get<T>(string key)
{
object objectToReturn = null;
objectToReturn = HttpContext.Current.Session[key] ;//HttpContext goes null here
return objectToReturn == null ? default(T) : (T)objectToReturn;
}
}