0
votes

We use an Azure Service Bus to post all of our requests from our Xamarin mobile app. The Azure Service Bus is bound to an Azure Function which is triggered each time a requests hits the Azure Service Bus.

We have found that we are getting errors from this Azure Function when we send data above a certain size. We can send up to 800 records without a problem but when we send >=850 records we get the following error:

[Error] Exception while executing function: Functions.ServiceBusQueueTrigger. mscorlib: Exception has been thrown by the target of an invocation. mscorlib: One or more errors occurred. A task was canceled.

The service that is being invoked is an ASP.NET Web API RESTful service that saves the data records into a database. This doesn't generate any errors at all.

Here is my Azure Function code.

#r "JWT.dll"
#r "Common.dll"

using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Text;
using Microsoft.ServiceBus.Messaging;

public static void Run(BrokeredMessage message, TraceWriter log)
{
    log.Info($"C# ServiceBus queue trigger function processed message: {message.MessageId}");

    if (message != null)
    {
        Common.Entities.MessageObjectEntity messageObject = message?.GetBody<Common.Entities.MessageObjectEntity>();
        string msgType = messageObject?.MessageType;
        var msgContent = messageObject?.MessageContent;
        log.Info($"Message type: {msgType}");

        double timestamp = (DateTime.UtcNow - new DateTime(1970, 1, 1)).TotalSeconds;
        string subscriber = "MYSUBSCRIBER";
        string privatekey = "MYPRIVATEKEY";
        Dictionary<string, object> payload = new Dictionary<string, object>()
        {
            {"iat", timestamp},
            {"subscriber", subscriber}
        };
        string token = JWT.JsonWebToken.Encode(payload, privatekey, JWT.JwtHashAlgorithm.HS256);

        using (var client = new HttpClient())
        {
            string url = $"http://myexamplewebservices.azurewebsites.net/api/routingtasks?formname={msgType}";
            client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue(subscriber, token);
            HttpContent content = new StringContent((string)msgContent, Encoding.UTF8, "application/json");
            client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json")); 
            var response = client.PostAsync(new Uri(url), content);

            if (response == null)
            {
                log.Info("Null response returned from request.");
            }
            else
            {
                if (response.Result.IsSuccessStatusCode && response.Result.StatusCode == System.Net.HttpStatusCode.OK)
                {
                    log.Info("Successful response returned from request.");
                }
                else
                {
                    log.Info($"Unsuccessful response returned from request: {response.Result.StatusCode}.");
                }
            }
        }
        log.Info("Completing message.");
    }
}

This code has been working for several years and works across all our other apps / web sites.

Any ideas why we're getting errors wehen we post large amounts of data to our Azure Service Bus / Azure Function?

2
would you like to share your AZFunction code ? - Imran Arshad
@ImranArshad updated to include code example - DomBurf
I have posted my answer with explanation below. Probably httpclient is the issue - Imran Arshad

2 Answers

1
votes

It may caused by "new httpclient", there is a limit to how quickly system can open new sockets so if you exhaust the connection pool, you may get some errors. You can refer to this link: https://aspnetmonsters.com/2016/08/2016-08-27-httpclientwrong/

And could you please share some more error message ?

1
votes

I can see that you are creating httpclient connection on each request which possibly be causing this issue. Httpclient creates a socket connection underneath it and has hard limit on it. Even when you dispose it it remains there for couple of mins that can't be used. A good practice is to create single static httpclient connection and reuse it. I am attaching some documents for you to go through. AzFunction Static HttpClient , Http Client Working , Improper instantiation