【问题标题】:Azure Speech API gets into an endless loop when called inside a custom WebAPi在自定义 WebAPi 中调用 Azure Speech API 时进入无限循环
【发布时间】:2021-03-13 16:52:59
【问题描述】:

让我解释一下场景-

我为具有多个参数的应用程序创建了一个自定义 Web API,用户通过传递基于某些逻辑连接的多个参数来调用此 api,然后在此自定义 api 中调用 Azure 语音 API 将文本转换为语音(分两步 - 1. 我调用 Azure 语音服务获取令牌,2. 我调用 Azure 语音服务将文本转换为语音)

问题 - 当我调用 Azure 语音服务来获取令牌时,它一直处于无限循环中。

以下是示例代码,任何正确方向的指针都会有所帮助-

public class ConverterController : ApiController
{
    

    /// <summary>
    /// default constructor
    /// </summary>
    public ConverterController()
    {
    }

    
    [HttpGet]
    public HttpResponseMessage ConvertUsingSpeech([FromUri] OutReachMessage outReachMessage)
    {

        try
        {
            outReachMessage = new OutReachMessage();
            var result = ConvertUsingSpeechApi(outReachMessage);
            var response = new HttpResponseMessage(HttpStatusCode.OK);
            response.Content = new StringContent(OutputMp3FilePath);
            return response;
        }
        catch (Exception ex)
        {
            return new HttpResponseMessage(HttpStatusCode.InternalServerError);
        }
    }


    async Task ConvertUsingSpeechApi(OutReachMessage outReachMessage)
    {
        var authObj = new Authentication("key");
        var token = authObj.GetAccessToken();


        using (var client = new HttpClient())
        {
            client.DefaultRequestHeaders.Add("Authorization", token);
            client.DefaultRequestHeaders.Add("Content-Type", contentType);
            client.DefaultRequestHeaders.Add("X-Microsoft-OutputFormat", outputFormat);
            UriBuilder uriBuilder = new UriBuilder(fetchSpeechUri);

            var content = new StringContent("<speak version='1.0' xml:lang='hi-IN'><voice xml:lang='hi-IN' xml:gender='Female' name = 'hi-IN-SwaraNeural'>" +
    "Welcome mahesh to the world of Azure - feeling great. </voice></speak>",
                            Encoding.UTF8, "text/xml");

            var result = await client.PostAsync(uriBuilder.Uri.AbsoluteUri, content);

            using (var response = client.PostAsync(uriBuilder.Uri.AbsoluteUri, content).Result)
            using (Stream streamToReadFrom = await response.Content.ReadAsStreamAsync())
            {
                string fileToWriteTo = OutputMp3FilePath;
                using (Stream streamToWriteTo = File.Open(fileToWriteTo, FileMode.Create))
                {
                    await streamToReadFrom.CopyToAsync(streamToWriteTo);
                }
            }
        }
    }

}

身份验证类 - 获取令牌

public class Authentication
{
    public static readonly string FetchTokenUri =
    "https://centralindia.api.cognitive.microsoft.com/sts/v1.0/issueToken";
    private string subscriptionKey;
    private string token;

    public Authentication(string subscriptionKey)
    {
        this.subscriptionKey = subscriptionKey;
        this.token = FetchTokenAsync(FetchTokenUri, subscriptionKey).Result;
    }

    public string GetAccessToken()
    {
        return this.token;
    }

    private async Task<string> FetchTokenAsync(string fetchUri, string subscriptionKey)
    {
        using (var client = new HttpClient())
        {
            client.DefaultRequestHeaders.Add("Ocp-Apim-Subscription-Key", subscriptionKey);
            UriBuilder uriBuilder = new UriBuilder(fetchUri);

            var result = await client.PostAsync(uriBuilder.Uri.AbsoluteUri, null);
            Console.WriteLine("Token Uri: {0}", uriBuilder.Uri.AbsoluteUri);
            return await result.Content.ReadAsStringAsync();
        }
    }
}

注意 - 我可以使用邮递员和控制台应用程序调用 Azure Speech api,但只有当我在我的自定义 webapi 中调用 azure 服务时它才不起作用。

【问题讨论】:

  • 如果您需要进一步的帮助,请告诉我。
  • 如果我的回复有帮助,请采纳为答案(点击回复旁边的标记选项将其从灰色切换为填写。),请参阅meta.stackexchange.com/questions/5234/…

标签: c# azure nested text-to-speech webapi


【解决方案1】:

如下更改代码,删除string subscriptionKey。它会起作用的。

public Authentication()
{
    this.subscriptionKey = subscriptionKey;
    this.token = FetchTokenAsync(FetchTokenUri, subscriptionKey).Result;
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-06-28
    • 1970-01-01
    相关资源
    最近更新 更多