SocketException while sending message to bot via connector client

lakshmi 751 Reputation points
2024-09-05T17:50:38.58+00:00

Hi Team,

We are sending messages from an Azure Function trigger to the bot using the ConnectorClient.

Starting today, we are getting a socket exception.

Below is the error message logged when updating an existing card. The same error occurs when sending a new message simultaneously.

type              System.Net.Sockets.SocketException
assembly          AzureFunction, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null
method            AzureFunction.Application.MessageRouter+<UpdateMessageAsync>d__19.MoveNext
outerType         System.Net.Http.HttpRequestException
outerMessage      The SSL connection could not be established, see inner exception.
outerAssembly     System.Net.Http, Version=8.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a
outerMethod       System.Net.Http.ConnectHelper+<EstablishSslConnectionAsync>d__2.MoveNext
innermostType     System.Net.Sockets.SocketException
innermostMessage  An existing connection was forcibly closed by the remote host.


Both methods were working without any exceptions until yesterday, and no socket exceptions were logged in Azure Insights.

ConnectorClient.Conversations.SendToConversationAsync((Activity)bundle.MessageActivity)

ConnectorClient.Conversations.UpdateActivityAsync((Activity)bundle.MessageActivity);

Azure AI Bot Service
Azure AI Bot Service
An Azure service that provides an integrated environment for bot development.
849 questions
Azure Functions
Azure Functions
An Azure service that provides an event-driven serverless compute platform.
5,150 questions
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Sina Salam 12,651 Reputation points
    2024-09-05T20:25:02.2233333+00:00

    Hello lakshmi,

    Welcome to the Microsoft Q&A and thank you for posting your questions here.

    I understand that you are having SocketException while sending message to bot via connector client.

    Regarding the error code, it happens due to many issues. The area to focus are not limited to the following, use the links to review for more steps to resolve them:

    1. SSL/TLS Issues, Service Updates, and Certificate Issues https://github.com/Azure/azure-functions-host/issues/8168 and https://learn.microsoft.com/en-us/answers/questions/789967/azure-function-outgoing-ssl-exceptions
    2. Network configuration, and retry logic implementation, https://github.com/Azure/azure-functions-durable-extension/issues/1069
    3. New connection for every request, might lead to port exhaustion, https://learn.microsoft.com/en-us/azure/azure-functions/functions-diagnostics and https://learn.microsoft.com/en-us/azure/azure-functions/manage-connections?tabs=csharp

    With all of the above, you can create additional code to reuse HttpClient in your Azure Function, this will make sure that the HttpClient instance is reused to reduce the risk of port exhaustion. The below is a sample code snippet for your use:

    public static class Function
    {
        private static readonly HttpClient httpClient = new HttpClient();
        [FunctionName("Function")]
        public static async Task<IActionResult> Run(
            [HttpTrigger(AuthorizationLevel.Function, "get", "post", Route = null)] HttpRequest req,
            ILogger log)
        {
            var response = await httpClient.GetAsync("https://example.com");
            string responseBody = await response.Content.ReadAsStringAsync();
            return new OkObjectResult(responseBody);
        }
    }
    

    I hope this is helpful! Do not hesitate to let me know if you have any other questions.

    Please don't forget to close up the thread here by upvoting and accept it as an answer if it is helpful.

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.