locked
System.Speech.Synthesis.SpeechSynthesizer (SAPY) in ASP.NET Application Not working after publishing in Local IIS RRS feed

  • Question

  • User-12480076 posted

    Hi All,

    I'm using SpeechSynthesizer class in a ASP.NET Web Application with Visual C# for reading text. Earlier at debugging I came across the following error for the following code. 

    The Error:

    "System.InvalidOperationException was unhandled by user code
      HResult=-2146233079
      Message=An asynchronous operation cannot be started at this time. Asynchronous operations may only be started within an asynchronous handler or module or during certain events in the Page lifecycle. If this exception occurred while executing a Page, ensure that the Page is marked <%@ Page Async="true" %>. This exception may also indicate an attempt to call an "async void" method, which is generally unsupported within ASP.NET request processing. Instead, the asynchronous method should return a Task, and the caller should await it."

    The Code I used: 

    *MessageSender is class I used for log debug messages which can be useful to track the execution after publishing in Local IIS.

    public static void startSpeechController()
            {
                MessageSender.sendDebugMessage("Speech Controller initilization Started");
    
                if (synth == null)
                {
                    synth = new SpeechSynthesizer();
                    MessageSender.sendDebugMessage("synth = new SpeechSynthesizer();");
                    synth.SetOutputToDefaultAudioDevice();
                    MessageSender.sendDebugMessage("synth.SetOutputToDefaultAudioDevice();");
                    synth.SelectVoiceByHints(VoiceGender.Female);
                    MessageSender.sendDebugMessage("synth.SelectVoiceByHints(VoiceGender.Female);");
                    synth.Rate = -1;
                    MessageSender.sendDebugMessage("synth.Rate = -1;");
    
                    synth.VisemeReached += synth_VisemeReached;
                    MessageSender.sendDebugMessage("synth.VisemeReached += synth_VisemeReached;");
                    synth.SpeakStarted += synth_SpeakStarted;
                    MessageSender.sendDebugMessage("synth.SpeakStarted += synth_SpeakStarted;");
                    synth.SpeakCompleted += synth_SpeakCompleted;
                    MessageSender.sendDebugMessage("synth.SpeakCompleted += synth_SpeakCompleted;");
    
    
                }
    
                MessageSender.sendDebugMessage("Speech Controller initilization Ended");
            }

    Then I change my code to the following with a use of Task.

    public static void startSpeechController()
            {
                MessageSender.sendDebugMessage("Speech Controller initilization Started");
    
                if (synth == null)
                {
                    synth = new SpeechSynthesizer();
                    MessageSender.sendDebugMessage("synth = new SpeechSynthesizer();");               
    
                    asyncInitilizer();
    
                }
    
                MessageSender.sendDebugMessage("Speech Controller initilization Ended");
            }
    
    
            private static async Task asyncInitilizer()
            {
                await Task.Run(() =>
                {
                    MessageSender.sendDebugMessage("Inside Task.Run()");
    
                    try
                    {
                        synth.SetOutputToDefaultAudioDevice();
                        MessageSender.sendDebugMessage("synth.SetOutputToDefaultAudioDevice();");
                        synth.SelectVoiceByHints(VoiceGender.Female);
                        MessageSender.sendDebugMessage("synth.SelectVoiceByHints(VoiceGender.Female);");
                        synth.Rate = -1;
                        MessageSender.sendDebugMessage("synth.Rate = -1;");
    
                        synth.VisemeReached += synth_VisemeReached;
                        MessageSender.sendDebugMessage("synth.VisemeReached += synth_VisemeReached;");
                        synth.SpeakStarted += synth_SpeakStarted;
                        MessageSender.sendDebugMessage("synth.SpeakStarted += synth_SpeakStarted;");
                        synth.SpeakCompleted += synth_SpeakCompleted;
                        MessageSender.sendDebugMessage("synth.SpeakCompleted += synth_SpeakCompleted;");
    
                        MessageSender.sendDebugMessage("End of Task.Run()");
                    }
                    catch (Exception ex)
                    {
                        MessageSender.sendExceptionMessage(ex.ToString());
                    }
                });
            }

    After that modification it was working fine, and it was reading the text I gave. It was when I was  debugging through Visual Studio 2013 IDE with IIS Express. The application worked fine.

    But after Publishing it as a File System and after publishing it in Local IIS, a problem occurs at 

     synth.SetOutputToDefaultAudioDevice();

    The following exception is logged.

    "System.NullReferenceException: Object reference not set to an instance of an object. at System.Speech.Internal.ObjectTokens.RegistryDataKey..ctor(String fullPath, RegistryDataKey copyKey) at System.Speech.Internal.ObjectTokens.SAPICategories.DefaultDeviceOut() at System.Speech.Internal.Synthesis.VoiceSynthesis..ctor(WeakReference speechSynthesizer) at System.Speech.Synthesis.SpeechSynthesizer.get_VoiceSynthesizer() at System.Speech.Synthesis.SpeechSynthesizer.SetOutputStream(Stream stream, SpeechAudioFormatInfo formatInfo, Boolean headerInfo, Boolean closeStreamOnExit) at System.Speech.Synthesis.SpeechSynthesizer.SetOutputToDefaultAudioDevice() at SapyTestProject2.SpeechController.<asyncInitilizer>b__0() "

    But the initialization line of SAPY (synth = new SpeechSynthesizer();) executes without a problem. So I can't understand why does it give a NullReferenceException at the above fine. So the application doesn't work after publishing it in Local IIS.

    It would be a great help if someone can give a solution to this problem.

    Thanks.

    Wednesday, June 29, 2016 6:52 AM

All replies

  • User753101303 posted

    Hi,

    Likely a permission issue and anyway keep in mind it runs on the server so you won't hear anything client side once deployed on a real web server and used from the browser. Instead I would rather try something such as https://www.microsoft.com/cognitive-services/en-us/speech-api which is intended for a web usage.

    Also if this is to just read your web page, users are using already web page readers and so your job is rather than to ensure that your markup is written to make the page easier to read by this kind of software. Try https://developer.mozilla.org/en-US/docs/Web/Accessibility/ARIA

    Wednesday, June 29, 2016 7:25 AM