Task.Result not working for method with out parameters

  • Thread starter Thread starter williamj8
  • Start date Start date
W

williamj8

Guest
Hello,

My Task.Result call inside a method with out parameters is not working.

My code is:


try
{
System.Console.WriteLine("Calling Speech To Text service.");

retVal = SpeechToText.GetResultAsync(strWavFileName).Result;
}
catch (Exception ex)
{
throw ex;
}



The exception thrown is:

"The type initializer for 'Microsoft.CognitiveServices.Speech.Internal.carbon_csharpPINVOKE' threw an exception."

I'm calling the sample code for the Azure Speech to Text service.

Here is the sample code:

public static async Task<STT_RetValues> GetResultAsync(string strFileName)
{
// Creates an instance of a speech config with specified subscription key and service region.
// Replace with your own subscription key and service region (e.g., "westus").
var config = SpeechConfig.FromSubscription("<not included>", "westus");

var stopRecognition = new TaskCompletionSource<int>();

var retVal = new STT_RetValues();

// Creates a speech recognizer using file as audio input.
// Replace with your own audio file name.
using (var audioInput = AudioConfig.FromWavFileInput(strFileName))
{
using (var recognizer = new SpeechRecognizer(config, audioInput))
{
// Subscribes to events.
recognizer.Recognizing += (s, e) =>
{
Console.WriteLine($"RECOGNIZING: Text={e.Result.Text}");
};

recognizer.Recognized += (s, e) =>
{
if (e.Result.Reason == ResultReason.RecognizedSpeech)
{
Console.WriteLine($"RECOGNIZED: Text={e.Result.Text}");
Console.WriteLine($"RECOGNIZED: Offset={e.Result.OffsetInTicks}");
Console.WriteLine($"RECOGNIZED: Duration={e.Result.Duration.ToString()}");

retVal = new STT_RetValues(e.Result.Text, e.Result.Duration, e.Result.OffsetInTicks);
}
else if (e.Result.Reason == ResultReason.NoMatch)
{
Console.WriteLine($"NOMATCH: Speech could not be recognized.");
}
};

recognizer.Canceled += (s, e) =>
{
Console.WriteLine($"CANCELED: Reason={e.Reason}");

if (e.Reason == CancellationReason.Error)
{
Console.WriteLine($"CANCELED: ErrorCode={e.ErrorCode}");
Console.WriteLine($"CANCELED: ErrorDetails={e.ErrorDetails}");
Console.WriteLine($"CANCELED: Did you update the subscription info?");
}

stopRecognition.TrySetResult(0);
};

recognizer.SessionStarted += (s, e) =>
{
Console.WriteLine("\n Session started event.");
};

recognizer.SessionStopped += (s, e) =>
{
Console.WriteLine("\n Session stopped event.");
Console.WriteLine("\nStop recognition.");
stopRecognition.TrySetResult(0);
};


// Starts continuous recognition. Uses StopContinuousRecognitionAsync() to stop recognition.
await recognizer.StartContinuousRecognitionAsync().ConfigureAwait(false);

// Waits for completion.
// Use Task.WaitAny to keep the task rooted.
Task.WaitAny(new[] { stopRecognition.Task });

// Stops recognition.
await recognizer.StopContinuousRecognitionAsync().ConfigureAwait(false);
}
}

return retVal;
}
}


Any suggestions?




williamj

Continue reading...
 
Back
Top