0
votes

We are using the Microsoft Bot Framework for our chat bot. Our Message Controller is standard :

public async Task<HttpResponseMessage> Post([FromBody] Activity activity)
{
      HttpResponseMessage response;
      try
      {
        if (activity.Type == ActivityTypes.Message)
        {
            //do some stuff

                await Conversation.SendAsync(activity, () => new RootDialog());
            }
            else
            {
                HandleSystemMessage(activity);
            }

            response = this.Request.CreateResponse(HttpStatusCode.OK);
        }
        catch (Exception ex)
        {
            //do some catching
        }
     return response; 
}

Sometimes, the bot needs to have long monologs. If it takes too long, we receive a 502 Bad Gateway error.

Any solution for that ?

1
Possible Duplicate of: stackoverflow.com/questions/41810912/…OmG

1 Answers

2
votes

Bot Framework calls time out after 15 seconds. Your bot must return a successful HTTP status code within 15 seconds for Direct Line to consider the message successfully sent.

If your bot does a lot of offline work or sends multiple messages, you should move that work to a background thread so the incoming request completes within 15 seconds.

Here's a snippet that correctly handles loads hosted inside ASP.Net and other hosting environments.

if (HostingEnvironment.IsHosted)
{
    HostingEnvironment.QueueBackgroundWorkItem(c => DoWorkAsync());
}
else
{
    Task.Run(() => DoWorkAsync());
}

...

private async Task DoWorkAsync()
{
    // do some stuff
}

Task.Run is simpler but HostingEnvironment.QueueBackgroundWorkItem prevents ASP.Net from tearing down your process before this work is complete.