0

I’ve been having an issue with a long running Azure Function App. It’s basically reads in a pipe delimeted file, creates an Employee object from the values, get the corresponding employee database record, and do a database insert or update.

Since it’s taking so long and spiking the CPU, some suggested that I read in each line of the file and send it to a Service Bus. Then have another function inside of my Function App to read from the queue and do my record comparison.

I’ve never used Service Bus before but went through and set one up in Azure. Now I’m trying to use the ServiceBus output binding in my Function App to send the message but I’m not getting it to work. I’ve been following along with some different articles I found including the following ones from Microsoft.

https://learn.microsoft.com/en-us/azure/azure-functions/functions-bindings-service-bus-output?tabs=in-process%2Cextensionv5&pivots=programming-language-csharp#example

https://learn.microsoft.com/en-us/azure/azure-functions/functions-bindings-service-bus?tabs=in-process%2Cextensionv5%2Cextensionv3&pivots=programming-language-csharp

I have the ServiceBusConnection added to my local.settings.json file. I pulled the connection string from the Azure Service Bus "Shared Access policies" section. I double checked the settings and made sure that the property name is correct. However, since the connection string is in the return section I don't know how to debug and confirm it's actually pulling the connection string and that it is correct.

This is what I’ve come up with so far but I’m not seeing any messages in my queue.

    [FunctionName("ProcessEmployeeInput")]
    [return: ServiceBus("myQueueName", Connection = "ServiceBusConnection")]
    public string Run(
       [BlobTrigger("%InputContainer%/%InputFolder%/{name}.txt", Connection = "StorageConnection")] Stream fileBlob, string name, ILogger log)
    {
        log.LogInformation($"Azure Function START.");
        log.LogInformation($"Processing file {name}.");
    
        string jsonEmployee = string.Empty;
        try
        {
            using (var srFile = new StreamReader(fileBlob))
            {
                srFile.ReadLine();   //first line is a header. Just call ReadLine to skip past it. 
    
                while (!srFile.EndOfStream)
                {
                    var record = srFile.ReadLine();

                    //Create an Employee object and map the properties.                 

Employee employee = MapEmployeeProperties(record, log);
    
                    //Serialize into text before sending to service bus                         

jsonEmployee = JsonConvert.SerializeObject(employee);
                }
            }
        }
        catch (Exception ex)
        {
            log.LogError("Error processing file. {0} | {1} | {2}", ex.Message, ex.StackTrace, ex.InnerException);
        }
    
        return jsonEmployee;
    }

Error Message

A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond.

I didn't see this at first but from this message it sounds like it can't find the Service Bus endpoint. Since the connection is in the output binding, I can confirm it can find it and that it is correct.

6
  • do you have any error message ?
    – Thomas
    Commented Jul 29, 2022 at 22:48
  • @Thomas I updated my original post.
    – Caverman
    Commented Jul 29, 2022 at 23:55
  • 1
    Is this running locally or in Azure? Are there any restrictions applied to the Service Bus endpoint, such as running in a VNET? Generally, that category of error is due to network issues connecting via the AMQP ports (5671, 5672) and using the AmqpWebSockets transport type often helps. That is specified as part of the host.json settings, described here: learn.microsoft.com/azure/azure-functions/… Commented Aug 1, 2022 at 15:11
  • @JesseSquire I'm running the Function App locally for development but I'm connecting to a service bus in Azure. I have a feeling it's Network related but I would have thought that since I can connect to a Blob Storage in the same Resource Group that I would be okay network wise. Maybe it has to do with the ports. I'll take a look at the link you sent.
    – Caverman
    Commented Aug 1, 2022 at 16:51
  • 1
    @JesseSquire Adding the info to the hosts file worked! I had found that article on Friday but hadn't actually added it until you sent the link again. Thanks!
    – Caverman
    Commented Aug 1, 2022 at 17:08

1 Answer 1

1

Glad that @Caverman for solved the issue. Thank you @Jesse Squire for your suggestions that helped to fix the issue. Posting this on behalf of your discussion so that it will be beneficial for other community members.

Workaround:-

To resolve the above issue make sure that we have following in our host.json file and the nuget package Azure.Messaging.ServiceBus is installed in the environment.

host.json file example:-

{
    "version": "2.0",
    "extensions": {
        "serviceBus": {
            "clientRetryOptions":{
                "mode": "exponential",
                "tryTimeout": "00:01:00",
                "delay": "00:00:00.80",
                "maxDelay": "00:01:00",
                "maxRetries": 3
            },
            "prefetchCount": 0,
            "transportType": "amqpWebSockets",
            "webProxy": "https://proxyserver:8080",
            "autoCompleteMessages": true,
            "maxAutoLockRenewalDuration": "00:05:00",
            "maxConcurrentCalls": 16,
            "maxConcurrentSessions": 8,
            "maxMessageBatchSize": 1000,
            "sessionIdleTimeout": "00:01:00",
            "enableCrossEntityTransactions": false
        }
    }
}

For more information please refer this MICROSOFT DOCUMENTATION FAQ .

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.