title: making asynchronous calls from generic handler (.ashx)
body: <p>I have a form in my website which posts json to the async handler which validates the data and return back the resonse OK or error and i will make the redirect on the client based on the response give by my handler.</p>
  
  <p>But when the response is ok, i want to perform some tasks asynchronously. But the asynchronous calls are not working inside the .ashx code. it is always synchronous.</p>
  
  <p>Could you please give me an advice on this.?</p>
  
  <p>code: </p>
  
  <pre><code>public class ValidateHandler : IHttpHandler, IRequiresSessionState
      {
  
          public void ProcessRequest(HttpContext context)
          {
              NameValueCollection nvcForm = context.Request.Form;
              Dictionary&lt;string, string&gt; errorFieldsDict = new Dictionary&lt;string, string&gt;();
  
  
              foreach (string nameValueKey in nvcForm.AllKeys)
              {
                  regExpToTest = string.Empty;
                  switch (nameValueKey)
                  {
                      case "firstName":
                        //validate
                          break;
                      case "lastName":
                         //validate
                          break;
                      case "email":
                          //validate
                          break;
                      case "phoneNumber":
                      //validate
                          break;
                      case "phoneCountryCode":
                          //validate
                          break;
                      case "country":
                       //validate
                          break;
                      case "addressLine1":
                          //validate
                          break;
                      case "addressLine2":
                         //validate
                          break;
                      case "city":
                          //validate
                          break;
                      case "postalCode":
                          //validate
                          break;
                      default:
                          //validate
                          break;
                  }
                  if (!string.IsNullOrEmpty(regExpToTest) &amp;&amp; !Regex.IsMatch(nvcForm[nameValueKey], regExpToTest) &amp;&amp; !string.IsNullOrEmpty(nvcForm[nameValueKey]))
                  {
                      errorFieldsDict.Add(nameValueKey, "Please Enter Correct Value");
                      isSuccess = false;
                  }
              }
  
              //Do your business logic here and finally
  
              if (isSuccess)
              {
                  JavaScriptSerializer serializer = new JavaScriptSerializer();
                  try
                  {
                      Dictionary&lt;string, object&gt; formValues = GetDictionaryForJson(nvcForm);
                      string previoiusUrl = GetRequestedURL(context);
                      string partner = string.Empty;
                      if (System.Web.HttpContext.Current.Session["yourpartner"] != null)
                          partner = System.Web.HttpContext.Current.Session["yourpartner"].ToString();
                      else if (System.Web.HttpContext.Current.Request.QueryString["utm_source"] != null)
                          partner = System.Web.HttpContext.Current.Request.QueryString["utm_source"];
                      else
                          partner = "company";
                      formValues.Add("partnerCode", partner);
                      string brochureType = string.Empty;
                      if (!string.IsNullOrEmpty(nvcForm["addressLine1"]) || !string.IsNullOrEmpty(nvcForm["addressLine2"]))
                          brochureType = "FBRO";
                      else
                          brochureType = "EBRO";
                      //Create a row in database
                      Item programItem = Sitecore.Context.Database.Items.GetItem(programRootpath + nvcForm["selectYourProgram"]); ;
                      AsyncMailSender caller = new AsyncMailSender(SendEmail);
                      IAsyncResult result = caller.BeginInvoke(programItem, nvcForm["email"], null, null);
                  }
                  catch (Exception ex)
                  {
                      isSuccess = false;
                      Log.Error("Enquiry handler failure: " + ex.Message, ex);
                      response.response = "error";
                      response.message = ex.Message;
                      context.Response.ContentType = "application/json";
                      context.Response.Write(JsonConvert.SerializeObject(response));
                  }
                  if (isSuccess)
                  {
                      response.response = "ok";
                      context.Response.ContentType = "application/json";
                      context.Response.Write(JsonConvert.SerializeObject(response));
                  }
  
              }
              else
              {
                  response.response = "errorFields";
                  response.errorFields = errorFieldsDict;
                  context.Response.ContentType = "application/json";
                  string responseJson = JsonConvert.SerializeObject(response);
                  context.Response.Write(JsonConvert.SerializeObject(response, Newtonsoft.Json.Formatting.None));
              }
  
          }
          private string GetRequestedURL(HttpContext context)
          {
              string previousURL = string.Empty;
              try
              {
                  previousURL = context.Request.ServerVariables["HTTP_REFERER"];
              }
              catch
              {
                  previousURL = context.Request.Url.AbsolutePath;
              }
              return previousURL;
          }
          public bool IsReusable
          {
              get
              {
                  return false;
              }
          }
          private void SendEmail(Item programItem, string toEmail)
          {
  
              if (programItem != null)
              {
                 SendEmail()
  
              }
          }
          private Dictionary&lt;string, object&gt; GetDictionaryForJson(NameValueCollection formValues)
          {
              Dictionary&lt;string, object&gt; formDictionary = new Dictionary&lt;string, object&gt;();
              foreach (string key in formValues.AllKeys)
              {
                  formDictionary.Add(key, formValues[key]);
              }
  
              return formDictionary;
          }
  
      }
      public delegate void AsyncMailSender(Item program, string toAddress);
  </code></pre>
  
  <p>PS: I did hide some code which is just our business.But Would be great if you can comment on that.</p>
  
  <p>thanks guys</p>