title: WebService Call Exception: Thread was being aborted
body: <p>Recently we've been getting System.Threading.ThreadAbortExceptions from an ASP.NET webservice that posts data to a payment server each night.</p>
  
  <p>The webservice is a standard .asmx page that gets called from another client.</p>
  
  <p>Inside the page I have a foreach loop that iterates through many payment rows in a database:</p>
  
  <pre><code>foreach (var obtRow in readyToBeCaptured)
  {    
      try
      {
          status = dibs.GetPagePost(...);  
          // handle status here
      }
      catch (ThreadAbortException tex)
      {
          SingletonLogger.Instance.Error(string.Format("Transactionhandler - Could not Capture, Thread was aborted. {0}", tex.Message), tex);     
      }
      catch (Exception ex)
      {
          SingletonLogger.Instance.Error(string.Format("Transactionhandler - Could not Capture, statuscode: {0}, message: {1}.", status, ex.Message), ex);             
      }
  }
  </code></pre>
  
  <p>The strange thing is I get a log entry from catch(ThreadAbortException tex) block when this error occurs, but then the code breaks out and is caught in another try catch block further up the call tree. Ideally I would have the code inside the foreach loop to continue</p>
  
  <p>This is the GetPagePost method </p>
  
  <pre><code>private bool GetPagePost(string url, NameValueCollection nameValueCollection, string userName, string password)
  {
      WebClient client = new WebClient();
      if (userName != "")
      {
          client.Credentials = new NetworkCredential(userName, password);
      }
      foreach (string str in nameValueCollection.AllKeys)
      {
          this._callCollection.Add(str, nameValueCollection[str]);
      }
      StringBuilder builder = new StringBuilder();
      builder.Append("DIBS.NET/1.2.0 ( ");
      builder.Append("OS ").Append(Environment.OSVersion.Platform).Append(" ").Append(Environment.OSVersion.Version).Append("; ");
      builder.Append(".NET CLR ").Append(Environment.Version).Append("; ");
      builder.Append("User ").Append(Environment.UserName).Append("; ");
      builder.Append("Domain ").Append(Environment.UserDomainName).Append("; ");
      builder.Append(" ) ");
      client.Headers.Add("User-Agent", builder.ToString());
      try
      {
          byte[] bytes = client.UploadValues(url, "POST", nameValueCollection);
          this._httpStatus = 200;
          this._httpBody = Encoding.UTF8.GetString(bytes);
          return true;
      }
      catch (WebException exception)
      {
          this._httpBody = exception.Message;
          this._httpStatus = (int) exception.Status;
      }
      catch (UriFormatException exception2)
      {
          this._httpBody = exception2.Message;
          this._httpStatus = -9999;
      }
      catch (Exception exception3)
      {
          this._httpBody = exception3.Message;
          this._httpStatus = -99999;
      }
      return false;
  }} 
  </code></pre>
  
  <p>Why is this error occuring and how can I prevent the code from breaking out of the foreach loop?
  I've been looking at a few other posts here on StackOverflow but they seem to relate to the usage of Reponse.Redirect which I don't use.</p>
  
  <p>Thanks </p>
  
  <p>/Jens</p>