c# - 如何将 'Microsoft.CognitiveServices.Speech' 命名空间与 Azure Functions 一起使用?

标签 c# azure azure-functions text-to-speech

我正在测试 Microsoft Azure 语音服务,特别是尝试使用文本转语音。因此,我使用 Azure 的免费层,并创建了一个 TimeTrigger Azure 函数来读取电子邮件、遍历 HTML,然后使用 SDK Microsoft.CognitiveServices.Speech 调用语音服务。 。我正在使用function.proj加载 nuget 包,加载 S22.ImapHtmlAgilityPack没有任何问题。但是语音包触发了异常: Unable to load DLL 'Microsoft.CognitiveServices.Speech.core.dll' or one of its dependencies: The specified module could not be found. (Exception from HRESULT: 0x8007007E) 。 我可以在 Azure 函数中使用此包吗?如果是这样,我做错了什么?

我尝试删除 <PackageReference Include="Microsoft.CognitiveServices.Speech" Version="1.6.0" />来自 function.proj 的线路并删除project.assets.json重新加载包但没有成功。

这是我的function.proj :

<Project Sdk="Microsoft.NET.Sdk">
    <PropertyGroup>
        <TargetFramework>netstandard2.0</TargetFramework>
    </PropertyGroup>

    <ItemGroup>
        <PackageReference Include="S22.Imap" Version="3.6.0" />
        <PackageReference Include="HtmlAgilityPack" Version="1.11.9" />
        <PackageReference Include="Microsoft.CognitiveServices.Speech" Version="1.6.0" />
    </ItemGroup>
</Project>

这是我的 run.csx :

using System;
using S22.Imap;
using System.Net.Mail;
using HtmlAgilityPack;
using System.Threading.Tasks;
using Microsoft.CognitiveServices.Speech;
using System.Diagnostics;

public static void Run(TimerInfo myTimer, ILogger log)
{
    var username = "<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="5a293b372a363f1a3d373b333674393537" rel="noreferrer noopener nofollow">[email protected]</a>";
    var password = "sample";
    var subsKey = "sample";

    using(ImapClient client = new ImapClient("imap.gmail.com", 993, username, password, AuthMethod.Login, true))
    {
        IEnumerable<uint> uids = client.Search(SearchCondition.From("<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="e192808c918d84a192808c918d84cf828e8c" rel="noreferrer noopener nofollow">[email protected]</a>"));
        IEnumerable<MailMessage> messages = client.GetMessages(uids);
        log.LogInformation($"Count: {messages.Count()}.");

        var msg = messages.FirstOrDefault();
        if(msg != null)
        {
            var doc = new HtmlDocument();
            doc.LoadHtml(msg.Body);

            var paragraphs = doc.DocumentNode.Descendants()
                .Where(x => x.Name == "p" && !string.IsNullOrEmpty(x.InnerText.Trim()))
                .ToList();

            var mailText = string.Empty;
            foreach(var par in paragraphs)
                mailText += par.InnerText;

            if(!string.IsNullOrEmpty(mailText))
            {
                var config = SpeechConfig.FromSubscription(subsKey, "myregion");
                config.SetSpeechSynthesisOutputFormat(SpeechSynthesisOutputFormat.Audio24Khz160KBitRateMonoMp3);
                config.SpeechSynthesisLanguage = "pt-BR";

                using (var synthesizer = new SpeechSynthesizer(config))
                {
                    using (var result = synthesizer.SpeakTextAsync(mailText).Result)
                    {
                        if (result.Reason == ResultReason.SynthesizingAudioCompleted)
                        {
                            //Do something with it
                        }
                        else if (result.Reason == ResultReason.Canceled)
                        {
                            var cancellation = SpeechSynthesisCancellationDetails.FromResult(result);
                            log.LogError($"CANCELED: Reason={cancellation.Reason}");

                            if (cancellation.Reason == CancellationReason.Error)
                            {
                                log.LogError($"CANCELED: ErrorCode={cancellation.ErrorCode}");
                                log.LogError($"CANCELED: ErrorDetails=[{cancellation.ErrorDetails}]");
                            }
                        }
                    }
                }
            }
        }
    }
}

最佳答案

您可以尝试删除function.proj,然后重新创建一个并首先添加Microsoft.CognitiveServices.Speech

确保 Microsoft.CognitiveServices.Speech.core.dll 已安装在 win-x86win-x64 中。请引用这个issue .

enter image description here

关于c# - 如何将 'Microsoft.CognitiveServices.Speech' 命名空间与 Azure Functions 一起使用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56996516/

相关文章:

c# - 无法在遥测中找到 hangfire.Name

c# - 我如何在 C# 中向 Outlook 2010 发送电子邮件?

AZURE PIPELINE 将变量从一个脚本传递到另一个脚本

c# - Azure Functions 不断返回 'The credentials supplied to the package were not recognized'

azure - 如何让Azure Event Grid触发Azure Function?

c# - 更新 xamarin nuget 后构建时 XamarinAndroidBuildResourceRestore 错误

c# - 以编程方式转到不同 View 的按钮? C#(xamarin)

entity-framework - Azure:在暂存环境中使用 Code First 迁移进行测试的最佳实践

azure - 即使 Azure AD 租户为空,也无法删除

azure - 当 azure 函数从队列中拉出消息后抛出异常时会发生什么?