0

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;
        }
}     
Community
  • 1
  • 1
Neer
  • 31
  • 7
  • `while (ar.IsCompleted == false)` can be switched to `while (ar.IsCompleted)` – VMAtm Apr 28 '15 at 12:11
  • Why to wait the result in a `while` loop? `EndInvoke` will do that for you. – VMAtm Apr 28 '15 at 12:12
  • Thanks VMAtm , I tried your solution but changing it to while (ar.IsComplete) does not solve the HttpContext issue. My longRuningMethod is running on an async child thread which needs HttpContext object (Session class code), which is going null. – Neer Apr 29 '15 at 06:14
  • You need to add this parameter for the signature for the `Invoke` and pass the `Session` via the parameters. as you need to provide a reference for your session to your code. – VMAtm Apr 29 '15 at 06:29

0 Answers0