#StackBounty: #c# #.net #azure #botframework Bot Channels Registration SocketException – An existing connection was forcibly closed by …

Bounty: 50

I have an Azure Web App with ASP.NET REST API application to behave as service for Bot Channels Registration.

In this app, I have one endpoint

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

where I receive a message from the user, process it and send back.

When I trying to communicate back to the user (either with typing activity or with an actual answer)

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);
}

through REST API I receive 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()

Everything works fine on emulator and doesn’t work at all in the web service.
I tried to implement REST API client by myself but got the same result.

What am I missing?


More information on the issue

  1. I use v4.2 Bot Framework packages

    <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. Entire Post method, userToken and botName are my internal credentials
    /// <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 looks like this
    Settings screenshot

  4. If I reroute Bot Channel to the local instance on my dev PC through ngrok I receive the same errors.

  5. I reduced entire method to perform simple echo answer, but error still remains

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.ServiceUrl is https://webchat.botframework.com/
  2. Error happens if message sent through webchat, i didn’t check another channels
    enter image description here


Get this bounty!!!

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.