title: SpeechSynthesizer in ASP.NET - async error
body: <p>I would like to be able to generate speech in my ASP.NET app by calling <code>speak.aspx?text=Hello%20world</code>. This would give a response in <code>.wav</code> format.</p>
  
  <p>So far I have a blank page with code behind:</p>
  
  <pre><code>protected void Page_PreRender(object sender, EventArgs e)
  {
    using (var ss = new SpeechSynthesizer()) {
      MemoryStream str = new MemoryStream();
      ss.SetOutputToWaveStream(str);
      ss.Speak(Server.UrlDecode(Request.QueryString["text"]));
      Response.AddHeader("Content-Type", "audio/wav");
      str.WriteTo(Response.OutputStream);
      str.Close();
    }
  }
  </code></pre>
  
  <p>However this fails with message:</p>
  
  <blockquote>
    <p><strong>InvalidOperationException</strong>: Asynchronous operations are not allowed in this context. Page starting an asynchronous operation has to have the Async attribute set to true and an asynchronous operation can only be started on a page prior to PreRenderComplete event.</p>
  </blockquote>
  
  <p>If I add <code>Async="true"</code> to the <code>@Page</code> directive, the code runs but a request for the page hangs indefinitely. Please could you let me know what's wrong, and show the correct code/approach to use?</p>
  
  <p>Note I can't just use the Google text-to-speech API since it only allows strings of 100 characters or less.</p>
  
  <p>Thank you.</p>