Narek
Narek

Reputation: 449

System.Speech not working on Windows Azure

I have ASP.Net MVC project and I am trying to call some methods from System.Speech. In my local all is working as expected but when I'm publishing it in Windows Azure it throws NullReferenceException. This my code which throws exception(in line 9):

1     public async static Task<byte[]> ToSpeech(string text)
2        {
3            byte[] bytes;
4            var stream = new MemoryStream();
5            await Task.Run(() =>
6                    {
7                        using (var speech = new SpeechSynthesizer())
8                        {
9                             speech.SetOutputToWaveStream(stream);
10                            speech.Speak(text);
11                        }
12                    });
13            bytes = ConvertWavToMP3(stream);
14            return bytes;
15        }

This is the thrown exception: enter image description here

Edit1

The problem is in SpeechSynthesizer , In my local when calling SpeechSynthesizer constructor the fields of speech property initializes normally but when I'm debugging the publish version after calling cosntructor they already thrown exception. enter image description here

Upvotes: 0

Views: 229

Answers (1)

TomTom
TomTom

Reputation: 62157

That has nothing to do with azure - you can get the same on your computer.

The usage of USING with a task makes no sense. You run the possible condition that your task is queued, and be fore it is executed the using statement exits - invalidating the speed variable.

This is simply bad code.

You must pretty much do all the processing in the run method of the task. That includes creating the synthesizer object. Just pass the string into the run method.

Upvotes: 1

Related Questions