实时会话转录遇IOException/SocketException错误,求控制台程序解决方案
排查实时会话转录中的连接强制关闭错误
调试实时会话转录功能时遇到以下错误:
IOException: Unable to write data to the transport connection: An existing connection was forcibly closed by the remote host.
SocketException: An existing connection was forcibly closed by the remote host.
提供的C#控制台程序代码如下:
public static async Task TranscribeConversationsAsync(string voiceSignatureStringUser1, string voiceSignatureStringUser2) { var filepath = "Tech.wav"; var config = SpeechConfig.FromSubscription(VoiceGenerator.subscriptionKey, VoiceGenerator.region); config.SetProperty("ConversationTranscriptionInRoomAndOnline", "true"); // en-us by default. Adding this code to specify other languages, like zh-cn. // config.SpeechRecognitionLanguage = "zh-cn"; var stopRecognition = new TaskCompletionSource<int>(); using (var audioInput = AudioConfig.FromWavFileInput(filepath)) { var meetingID = Guid.NewGuid().ToString(); using (var conversation = await Conversation.CreateConversationAsync(config, meetingID)) { // create a conversation transcriber using audio stream input using (var conversationTranscriber = new ConversationTranscriber(audioInput)) { conversationTranscriber.Transcribing += (s, e) => { Console.WriteLine($"TRANSCRIBING: Text={e.Result.Text} SpeakerId={e.Result.UserId}"); }; conversationTranscriber.Transcribed += (s, e) => { if (e.Result.Reason == ResultReason.RecognizedSpeech) { Console.WriteLine($"TRANSCRIBED: Text={e.Result.Text} SpeakerId={e.Result.UserId}"); } else if (e.Result.Reason == ResultReason.NoMatch) { Console.WriteLine("NOMATCH: Speech could not be recognized."); } }; conversationTranscriber.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); } }; conversationTranscriber.SessionStarted += (s, e) => { Console.WriteLine($"\nSession started event. SessionId={e.SessionId}"); }; conversationTranscriber.SessionStopped += (s, e) => { Console.WriteLine($"\nSession stopped event. SessionId={e.SessionId}"); Console.WriteLine("\nStop recognition."); stopRecognition.TrySetResult(0); }; // Add participants to the conversation. var speaker1 = Participant.From("User1", "en-US", voiceSignatureStringUser1); var speaker2 = Participant.From("User2", "en-US", voiceSignatureStringUser2); await conversation.AddParticipantAsync(speaker1); await conversation.AddParticipantAsync(speaker2); // Join to the conversation and start transcribing await conversationTranscriber.JoinConversationAsync(conversation); await conversationTranscriber.StartTranscribingAsync().ConfigureAwait(false); // waits for completion, then stop transcription Task.WaitAny(new[] { stopRecognition.Task }); await conversationTranscriber.StopTranscribingAsync().ConfigureAwait(false); } } } }
排查方向与解决建议
- 验证语音签名有效性:确认传入的两个语音签名是通过官方流程生成的有效字符串,签名对应的语言必须和代码中指定的
en-US完全匹配,无效或不匹配的签名会触发服务端主动断开连接。 - 检查音频文件格式:确保
Tech.wav符合Azure语音服务的要求:PCM 16位编码、采样率16kHz、单声道或立体声,格式不兼容会导致服务端无法处理音频流,进而强制关闭连接。 - 核对区域与网络配置:确认
VoiceGenerator.region与你的Azure语音资源所在区域完全一致;检查网络环境是否允许WebSocket连接(语音服务依赖WebSocket传输实时数据),防火墙、代理或VPN可能拦截连接导致中断。 - 修复异步代码阻塞问题:代码中使用
Task.WaitAny属于同步阻塞操作,可能破坏异步流程的上下文,建议替换为await stopRecognition.Task,避免因线程阻塞引发的连接异常。 - 检查服务配额与限制:查看Azure语音资源的配额是否耗尽,会话时长是否超过服务端限制(默认单会话有时间限制),超出限制会被强制断开。
- 更新SDK版本:确保使用的Azure Speech SDK是最新稳定版,旧版本可能存在已知的连接兼容性Bug,更新后可解决部分连接问题。
内容的提问来源于stack exchange,提问作者edu
相关产品推荐
相关产品推荐

