I am implementing an Queue triggered Function App. I want to access the maxDequeueCount
value (defined in the host.json
file). My code is roughly as follows:
// maxDequeueCount = ??
[Function("Function1")]
public async Task Run([QueueTrigger("%QUEUE_NAME%", Connection = "AzureWebJobsStorage")] byte[] queueItem, string id, long dequeueCount)
{
try
{
DoStuff();
}
catch (Exception ex)
{
if (dequeueCount <= maxDequeueCount)
{
HandleError1();
}
else
{
HandleError2();
}
}
}
While I can pass dequeueCount
as a function parameter, I cannot find a way to get the maxDequeueCount
value.
I am aware there is a FunctionContext
which I can pass as a function parameter (as below):
public async Task Run([QueueTrigger("%QUEUE_NAME%", Connection = "AzureWebJobsStorage")] byte[] queueItem, string id, long dequeueCount, FunctionContext context)
However, when I reach the if statement in my catch
block, it is a null object. Do I need to perform some other configuration to be able to use this information?
I have created Queue trigger function with runtime stack .NET 8.0 isolated.
When i send a message from the queue got triggered the function successfully and getting DequeueCount and maxDequeueCount by using below code and configuration.
Function1.cs:
public class Function1
{
private readonly IConfiguration _configuration;
private readonly ILogger<Function1> _logger;
public Function1(IConfiguration configuration, ILogger<Function1> logger)
{
_configuration = configuration;
_logger = logger;
}
[Function("Function1")]
public async Task Run([QueueTrigger("firstqueue", Connection = "AzureWebJobsStorage1")] byte[] queueItem, string id, long dequeueCount)
{
// Retrieve MaxDequeueCount from host.json
int maxDequeueCount = _configuration.GetValue<int>("queues:maxDequeueCount");
_logger.LogInformation($"MaxDequeueCount: {maxDequeueCount}");
_logger.LogInformation($"DequeueCount: {dequeueCount}");
try
{
// Your main logic here
DoStuff();
}
catch (Exception ex)
{
if (dequeueCount < maxDequeueCount)
{
_logger.LogInformation("Handling error, retrying...");
HandleError1();
}
else
{
_logger.LogInformation("Max retries reached, moving to poison queue...");
HandleError2();
}
}
}
private void DoStuff()
{
// Your processing logic here
}
private void HandleError1()
{
// Handle retry logic here
}
private void HandleError2()
{
// Handle poison queue logic here
}
}
Program.cs:
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Configuration;
var host = new HostBuilder()
.ConfigureFunctionsWebApplication()
.ConfigureAppConfiguration(config =>
{
config.AddJsonFile("host.json", optional: true, reloadOnChange: true);
})
.Build();
host.Run();
host.json:
{
"version": "2.0",
"queues": {
"maxDequeueCount": 5
}
}
local.settings.json:
{
"IsEncrypted": false,
"Values": {
"AzureWebJobsStorage": "UseDevelopmentStorage=true",
"AzureWebJobsStorage1": "your-storage-connection-string",
"FUNCTIONS_WORKER_RUNTIME": "dotnet-isolated"
},
}
Output: