title: System.Speech.Synthesis hangs with high CPU on 2012 R2
body: <p>I have an asp.net MVC application that has a controller action that takes a string as input and sends a response wav file of the synthesized speech.  Here is a simplified example:</p>
  
  <pre><code>    public async Task&lt;ActionResult&gt; Speak(string text)
      {
          Task&lt;FileContentResult&gt; task = Task.Run(() =&gt;
          {
              using (var synth = new System.Speech.Synthesis.SpeechSynthesizer())
              using (var stream = new MemoryStream())
              {
                  synth.SetOutputToWaveStream(stream);
                  synth.Speak(text);
                  var bytes = stream.GetBuffer();
                  return File(bytes, "audio/x-wav");
              }
          });
          return await task;
      }
  </code></pre>
  
  <p>The application (and this action method in particular) is running fine in a server environment on 2008 R2 servers, 2012 (non-R2) servers, and my 8.1 dev PC.  It is also running fine on a standard Azure 2012 R2 virtual machine.  However, when I deploy it to three 2012 R2 servers (its eventual permanent home), the action method never produces an HTTP response -- the IIS Worker process maxes one of the CPU cores indefinitely.  There is nothing in the event viewer and nothing jumps out at me when watching the server with Procmon.  I've attached to the process with remote debugging, and the <code>synth.Speak(text)</code> never returns.  When the <code>synth.Speak(text)</code> call is executed I immediately see the runaway w3wp.exe process in the server's task manager.</p>
  
  <p>My first inclination was to believe some process was interfering with speech synthesis in general on the servers, but the Windows Narrator works correctly, and a simple console app like this also works correctly:</p>
  
  <pre><code>static void Main(string[] args)
  {
      var synth = new System.Speech.Synthesis.SpeechSynthesizer();
      synth.Speak("hello");
  }
  </code></pre>
  
  <p>So obviously I can't blame the server's speech synthesis in general.  So maybe there is a problem in my code, or something strange in IIS configuration?  How can I make this controller action work correctly on these servers?</p>
  
  <p>This is a simple way to test the action method (just have to get the <code>url</code> value right for the routing):</p>
  
  <pre><code>&lt;div&gt;
      &lt;input type="text" id="txt" autofocus /&gt;
      &lt;button type="button" id="btn"&gt;Speak&lt;/button&gt;
  &lt;/div&gt;
  
  &lt;script&gt;
      document.getElementById('btn').addEventListener('click', function () {
          var text = document.getElementById('txt').value;
          var url = window.location.href + '/speak?text=' + encodeURIComponent(text);
          var audio = document.createElement('audio');
          var canPlayWavFileInAudioElement = audio.canPlayType('audio/wav'); 
          var bgSound = document.createElement('bgsound');
          bgSound.src = url;
          var canPlayBgSoundElement = bgSound.getAttribute('src');
  
          if (canPlayWavFileInAudioElement) {
              // probably Firefox and Chrome
              audio.setAttribute('src', url);
              audio.setAttribute('autoplay', '');
              document.getElementsByTagName('body')[0].appendChild(audio);
          } else if (canPlayBgSoundElement) {
              // internet explorer
              document.getElementsByTagName('body')[0].appendChild(bgSound);
          } else {
              alert('This browser probably can\'t play a wav file');
          }
      });
  &lt;/script&gt;
  </code></pre>