I have gone through similar queries about httpContext.Current becoming null in async methods.
According to this post -> HttpContext.Current is null in an asynchronous Callback, HttpContext is not available in async functions(child threads).
I have implemented asynchronous polling in my function, my called function uses httpContext object, which goes null. Can someone please show me how can I pass HttpContext to the child thread.
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;
}
}