排查Azure语音转文本代理或防火墙相关问题
Azure语音转文本Demo程序企业网络排查问题
我基于Azure开发了一款语音转文本的概念验证程序,Azure端配置已完成,代码如下:
using System; using System.IO; using System.Threading.Tasks; using Microsoft.CognitiveServices.Speech; using Microsoft.CognitiveServices.Speech.Audio; class Program { static SpeechRecognizer recognizer; async static Task FromMic(SpeechConfig speechConfig) { using var audioConfig = AudioConfig.FromDefaultMicrophoneInput(); recognizer = new SpeechRecognizer(speechConfig, audioConfig); var stopRecognition = new TaskCompletionSource<int>(); recognizer.SessionStarted += (s, e) => { Console.WriteLine("\n Session started event: " + e); }; 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}"); } 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 set the speech resource key and region values?"); } stopRecognition.TrySetResult(0); }; recognizer.SessionStopped += (s, e) => { Console.WriteLine("\n Session stopped event."); stopRecognition.TrySetResult(0); }; await recognizer.StartContinuousRecognitionAsync(); // Waits for completion. Use Task.WaitAny to keep the task rooted. Task.WaitAny(new[] { stopRecognition.Task }); } async static Task Main(string[] args) { var speechConfig = SpeechConfig.FromSubscription("xxxxxxxxxxxxxxxxxxxx", "xxxx"); // Clearly I don't really know how to stop the recognition properly before exiting, but that's an issue for another day... AppDomain.CurrentDomain.ProcessExit += delegate { EndRecognition(); }; Console.CancelKeyPress += delegate { EndRecognition(); }; await FromMic(speechConfig); Console.WriteLine("Exiting"); } static void EndRecognition() { Console.WriteLine("Ending recognition..."); recognizer.StopContinuousRecognitionAsync(); recognizer.Dispose(); Console.WriteLine("Done."); } }
程序在家用电脑运行正常,但在公司电脑上仅能收到会话启动消息,无法完成语音识别。公司网络为严格防火墙规则下的代理路由环境,已完成以下排查:
- 确认麦克风正常可用
- 设置
HTTP_PROXY和HTTPS_PROXY环境变量指向公司代理 - 查看杀毒防火墙日志,无相关拦截记录
- 查看Azure端“总调用数”指标,无数据
预期无法连接Azure语音资源时会抛出异常,但实际无报错信息,问题静默发生。
下一步排查步骤
- 验证代理配置是否被Speech SDK正确读取:在代码中添加日志输出
Environment.GetEnvironmentVariable("HTTP_PROXY")和HTTPS_PROXY的值,确认程序能获取到代理地址;同时需确保代理支持HTTPS,并允许访问对应区域的Azure语音服务端点(格式为https://<region>.stt.speech.microsoft.com) - 手动测试端点连通性:在公司电脑上用PowerShell或
curl测试访问Azure语音服务端点,比如执行Invoke-WebRequest -Uri https://<your-region>.stt.speech.microsoft.com/ -Proxy http://your-proxy-address:port,检查是否能正常获取响应,排查网络层连通性 - 启用Speech SDK详细日志:在初始化
SpeechConfig后添加以下代码开启日志,日志会记录详细的网络交互和潜在错误,帮助定位问题:speechConfig.SetProperty(PropertyId.Speech_LogFilename, @"C:\temp\speech-sdk.log"); speechConfig.SetProperty(PropertyId.Speech_LogLevel, "Detailed"); - 配置代理身份验证(若需):如果公司代理需要用户名密码,仅设置环境变量可能无效,需在代码中显式配置:
speechConfig.SetProperty(PropertyId.Proxy_Host, "your-proxy-host"); speechConfig.SetProperty(PropertyId.Proxy_Port, "your-proxy-port"); speechConfig.SetProperty(PropertyId.Proxy_Username, "proxy-username"); speechConfig.SetProperty(PropertyId.Proxy_Password, "proxy-password"); - 检查WebSocket流量是否被拦截:Azure语音转文本使用WebSocket传输音频数据,部分企业防火墙可能拦截此类连接,可尝试用工具测试WebSocket连通性,比如用
wscat执行wscat -w 0 -x wss://<your-region>.stt.speech.microsoft.com/speech/recognition/conversation/cognitiveservices/v1 -H "Ocp-Apim-Subscription-Key: your-azure-key" - 统一SDK版本:确认公司电脑使用的Speech SDK版本与家用电脑一致,旧版本可能存在代理兼容性问题,尝试更新到最新稳定版
- 验证本地音频处理:临时使用Speech SDK的离线识别功能测试,确认问题是否出在网络层面而非本地音频采集或处理环节
内容的提问来源于stack exchange,提问作者user2686455
相关产品推荐
相关产品推荐

