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.
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.
AmqpWebSockets
transport type often helps. That is specified as part of thehost.json
settings, described here: learn.microsoft.com/azure/azure-functions/…