title: Generic .ashx for concurrent HTTP requests
body: <p>I have created a .ashx file that is able to handle concurrent http requests.</p>
  
  <p>Works great, but now I'd like to create a base class that does the common work in order to not repeat myself each time I need to create a new .ashx of the same kind.</p>
  
  <p>Here is my current code for the http handler:</p>
  
  <pre><code>public class BaseAsyncHandler : IHttpAsyncHandler, IRequiresSessionState
  {
      public bool IsReusable
      {
          get { return false; }
      }
  
      public BaseAsyncHandler()
      {
      }
  
      public IAsyncResult BeginProcessRequest(HttpContext context, AsyncCallback cb, Object extraData)
      {
          BaseAsynchOperation asynch = new BaseAsynchOperation(cb, context, extraData);
          asynch.StartAsyncWork();
          return asynch;
      }
  
      public void EndProcessRequest(IAsyncResult result)
      {
      }
  
      public void ProcessRequest(HttpContext context)
      {
          throw new InvalidOperationException();
      }
  }
  </code></pre>
  
  
  
  <pre><code>public class BaseAsynchOperation : IRequiresSessionState, IAsyncResult
  {
      private bool _completed;
      private Object _state;
      private AsyncCallback _callback;
      public HttpContext _httpContext;
  
      bool IAsyncResult.IsCompleted
      {
          get { return _completed; }
      }
  
      WaitHandle IAsyncResult.AsyncWaitHandle
      {
          get { return null; }
      }
  
      Object IAsyncResult.AsyncState
      {
          get { return _state; }
      }
  
      bool IAsyncResult.CompletedSynchronously
      {
          get { return false; }
      }
  
      public BaseAsynchOperation(AsyncCallback callback, HttpContext context, Object state)
      {
          _callback = callback;
          _httpContext = context;
          _state = state;
          _completed = false;
      }
  
      public void StartAsyncWork()
      {
          ThreadPool.QueueUserWorkItem(new WaitCallback(StartAsyncTask), null);
      }
  
      protected virtual void OnBeforeStartAsyncTask()
      {  
      }
  
      public void StartAsyncTask(Object workItemState)
      {
          OnBeforeStartAsyncTask();
  
          _httpContext.Response.ContentType = "application/json";
  
  
          _completed = true;
          _callback(this);
      }
  }
  </code></pre>
  
  <p>My new test that inherits from the base class:</p>
  
  <pre><code>public class TestAsyncHandler : BaseAsyncHandler
  {
      public TestAsyncHandler()
      {
  
      } 
  }
  
  public class TestAsyncOperation : BaseAsynchOperation
  {
      public TestAsyncOperation(AsyncCallback callback, HttpContext context, object state) : base(callback, context, state)
      {
      }
  
      protected override void OnBeforeStartAsyncTask()
      {
          var test = _httpContext.Request["test"]; // code never ends up here...
      }
  }
  </code></pre>
  
  <p>Since all my work is done in the <code>StartAsyncTask()</code> method (similar to <code>ProcessRequest()</code> in a regular generic handler .ashx), I thought that I add a method there which can be overridden. However, it never seems to get called. </p>
  
  <p>What did I miss? Do you have any other suggestions on improvments?</p>