I'm writing my first ServiceBusTrigger
ed Azure function, but am failing to get it to execute successfully.
Connection to the queue is working and the function is attempting to execute when I inject a message (which I'm doing via the Service Bus Explorer in Azure portal). The message is also moved to the dead letter queue after failing for the configured number of retries, as expected.
Here's the code:
using System;
using Microsoft.Azure.WebJobs;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
namespace PlayerProcessing.PendingPlayerCustomId;
public class QueueWorker
{
private readonly ILogger _log;
public QueueWorker(IServiceProvider serviceProvider)
{
_log = serviceProvider.GetService<ILogger<QueueWorker>>();
}
[FunctionName("ServiceBusQueueTriggerCSharp")]
public void Run(
[ServiceBusTrigger("pending-player-custom-id", Connection = "AzureServiceBus")]
string myQueueItem
)
{
_log.LogInformation($"C# ServiceBus queue trigger function processed message: {myQueueItem}");
}
}
(I initially tried using a JSON object and the application/json
content type, but I haven't even been able to get it working with a string and text/plain
content type.)
The error I get:
System.Private.CoreLib: Exception while executing function: ServiceBusQueueTriggerCSharp. Microsoft.Azure.WebJobs.Host: Exception binding parameter 'myQueueItem'.
Thanks!
I created a sample ServiceBusQueueTrigger
function and successfully got the message from Azure Service Bus Queue.
The connection may not be retrieved correctly, or it could have expired due to improper configuration in the local.settings.json
file
Make sure you have added the Azure Service Bus Queue connection string in the local.settings.json
file as below.
local.settings.json :
{
"IsEncrypted": false,
"Values": {
"AzureWebJobsStorage": "<StoragecConneString>",
"FUNCTIONS_INPROC_NET8_ENABLED": "1",
"FUNCTIONS_WORKER_RUNTIME": "dotnet",
"AzureServiceBus": "<ServiceBusQueueConnecString>"
}
}
Code :
using System;
using Microsoft.Azure.WebJobs;
using Microsoft.Extensions.Logging;
namespace FunctionApp2.PendingPlayerCustomId
{
public class QueueWorker
{
[FunctionName("ServiceBusQueueTriggerCSharp")]
public void Run(
[ServiceBusTrigger("pending-player-custom-id", Connection = "AzureServiceBus")]
string myQueueItem,
ILogger log
)
{
log.LogInformation($"C# ServiceBus queue trigger function processed message: {myQueueItem}");
}
}
}
Azure Service Bus Queue :
I sent a message to the Azure Service Bus Queue as shown below.
Output :
The Service Bus Queue trigger function ran successfully as shown below.