Bot Channels注册SocketException-现有连接被远程主机强行关闭

时间:2019-01-22 16:06:44

标签: c# .net azure botframework

我有一个带有ASP.NET REST API应用程序的Azure Web应用程序,可以充当Bot Channels Registration的服务。

在此应用中,我有一个端点

 public async Task<IHttpActionResult> Post(string userToken, string botName, [FromBody] Activity activity)

我从用户那里收到一条消息,对其进行处理并发回。

当我尝试与用户交流时(通过键入活动或实际答案)

private static async Task ShowTyping(Activity activity, ConnectorClient client)
{
    var typingResponse = new Activity
    {
        Conversation = activity.Conversation,
        From = activity.Recipient,
        Locale = activity.Locale,
        Recipient = activity.From,
        ReplyToId = activity.Id,
        Id = activity.Id,
        Type = "typing"
    };
    await client.Conversations.UpdateActivityAsync(typingResponse);
}

...

foreach (var reply in replies)
{
      Trace.TraceInformation($"[{userToken}:{botName}] Sending reply {reply.Id}:{reply.Text}");
      await client.Conversations.ReplyToActivityAsync(reply);
}

通过REST API我收到HttpRequestException - An existing connection was forcibly closed by the remote host

2019-01-22T15:49:54  PID[17864] Error       System.Net.Http.HttpRequestException: An error occurred while sending the request. ---> System.Net.WebException: The underlying connection was closed: An unexpected error occurred on a send. ---> System.IO.IOException: Unable to read data from the transport connection: An existing connection was forcibly closed by the remote host. ---> System.Net.Sockets.SocketException: An existing connection was forcibly closed by the remote host    at System.Net.Sockets.Socket.EndReceive(IAsyncResult asyncResult)    at System.Net.Sockets.NetworkStream.EndRead(IAsyncResult asyncResult)   
--- End of inner exception stack trace ---    at System.Net.TlsStream.EndWrite(IAsyncResult asyncResult)    at System.Net.PooledStream.EndWrite(IAsyncResult asyncResult)    at System.Net.ConnectStream.WriteHeadersCallback(IAsyncResult ar)    --- End of inner exception stack trace ---    at System.Net.HttpWebRequest.EndGetRequestStream(IAsyncResult asyncResult, TransportContext& context)    at System.Net.Http.HttpClientHandler.GetRequestStreamCallback(IAsyncResult ar)    --- End of inner exception stack trace ---    at Microsoft.Rest.RetryDelegatingHandler.<SendAsync>d__15.MoveNext()
--- End of stack trace from previous location where exception was thrown ---    at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)    at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)    at System.Runtime.CompilerServices.TaskAwaiter.ValidateEnd(Task task)    at Microsoft.Bot.Connector.Conversations.<UpdateActivityWithHttpMessagesAsync>d__9.MoveNext()
--- End of stack trace from previous location where exception was thrown ---    at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)    at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)    at Microsoft.Bot.Connector.ConversationsExtensions.<UpdateActivityAsync>d__16.MoveNext()
--- End of stack trace from previous location where exception was thrown ---    at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)    at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)    at ChatFirst.Channels.BotFramework.Controllers.WebhookController.<ShowTyping>d__5.MoveNext()

一切在模拟器上都可以正常运行,并且在Web服务中根本不起作用。 我尝试自己实现REST API客户端,但结果相同。

我想念什么?


有关此问题的更多信息

  1. 我使用v4.2 Bot Framework软件包

    <package id="Microsoft.Bot.Builder" version="4.2.0" targetFramework="net472" />   <package id="Microsoft.Bot.Connector" version="4.2.0" targetFramework="net472" />   <package id="Microsoft.Bot.Schema" version="4.2.0" targetFramework="net472" />

  2. 整个帖子方法,userTokenbotName是我的内部凭据

    /// <summary>
    ///     POST: api/v1/webhook/{userToken}/{botName}
    ///     Receive a message from a user and reply to it
    /// </summary>
    [HttpPost]
    [Route("api/v1/webhook/{userToken}/{botName}")]
    public async Task<IHttpActionResult> Post(string userToken, string botName, [FromBody] Activity activity)
    {
        try
        {
            if (activity.Type == ActivityTypes.Message)
            {
                Trace.TraceInformation($"[{userToken}:{botName}] Message from {activity.From.Id}");
                MicrosoftAppCredentials credentials = _channelGateway.GetCredentials(userToken, botName);
    
                if (credentials == null)
                    return Ok();
    
                var serviceUri = new Uri(activity.ServiceUrl);
                var client = new ConnectorClient(serviceUri, credentials);
    
                await ShowTyping(activity, client);
    
                IEnumerable<Activity> replies = await _coreTalkService.GetRepliesAsync(new BotToken(userToken, botName), activity);
    
                foreach (var reply in replies)
                {
                    Trace.TraceInformation($"[{userToken}:{botName}] Sending reply {reply.Id}:{reply.Text}");
                    await client.Conversations.ReplyToActivityAsync(reply);
                }
            }
            else
            {
                HandleSystemMessage(activity);
            }
    
            return Ok();
        }
        catch (Exception e)
        {
            Trace.TraceError(e.ToString());
            return InternalServerError(e);
        }
    }
    
  3. Bot Channels Registrations设置如下所示 Settings screenshot

  4. 如果我通过ngrok将Bot Channel重新路由到我的开发PC上的本地实例,则会收到相同的错误。

  5. 我简化了执行简单回显答案的整个方法,但错误仍然存​​在

public async Task<IHttpActionResult> Post(string userToken, string botName, [FromBody] Activity activity)
{
    Trace.TraceInformation($"[{userToken}:{botName}] Message from {activity.From.Id}");
    var client = new ConnectorClient(new Uri(activity.ServiceUrl), 
        new MicrosoftAppCredentials("<secret>", "<secret>"));
    var answer = activity.CreateReply(activity.Text, activity.Locale);
    await client.Conversations.ReplyToActivityAsync(answer);
    return Ok();
}
  1. activity.ServiceUrlhttps://webchat.botframework.com/
  2. 如果通过网络聊天发送的消息发生错误,我没有检查其他渠道 enter image description here

2 个答案:

答案 0 :(得分:0)

我唯一想到的就是必须有一些引用的库干扰您项目中的传出消息。

我没有尝试调试您的项目,而是创建了一个新的类似项目,该项目可以按预期工作:https://github.com/xakpc/BotFrameworkChannel.Test/pull/1

答案 1 :(得分:0)

您是否将TLS 1.2用作安全协议?

查看此答案:https://stackoverflow.com/a/53647879/3673445

“在2018年12月4日,Azure Bot服务将要求使用传输层安全性(TLS)1.2保护所有连接。此强制实施对于为您的数据提供最佳安全性至关重要。”