How to get the MaxDequeueCount in the .NET function code

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?

6

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:

Assuming you’re using the Azure Functions Isolated Worker Model, you can change the type of your first parameter byte[] queueItem to QueueMessage queueItem

Each message should keep track of their own DequeueCount which is available as property on the QueueMessage: https://learn.microsoft.com/en-us/dotnet/api/azure.storage.queues.models.queuemessage?view=azure-dotnet

The QueueMessageConverter in the Microsoft.Azure.Functions.Worker is taking care of binding the data passed to the function, into the QueueMessage automatically

EDIT:
To get the Value of the maxDequeueCount things will be a little more tricky, but as pointed out in the comments – you could try inject access to the host.json via IConfiguration, as the functionContext does not seem to provide access to those:

var host = new HostBuilder()
.ConfigureFunctionsWorkerDefaults()
.ConfigureAppConfiguration((context, configBuilder) =>
{
    configBuilder.AddJsonFile("host.json", optional: false, reloadOnChange: true); // add this, to get access to host.json values
})
.Build();

and afterwards you could access it by injecting into the function like this:

 public class Function1(IConfiguration _configuration)
 {
     [Function("Function1")]
     public async Task Run([QueueTrigger("%QUEUE_NAME%", Connection = "AzureWebJobsStorage")] byte[] queueItem, string id, long dequeueCount)
     {
         var MaxRetryCount = _configuration.GetValue<int>("extensions:queues:maxDequeueCount");
         try
         {
             DoStuff();
         }
         catch (Exception ex)
         {
             if (dequeueCount <= MaxRetryCount)
             {
                 HandleError1();
             }
             else
             {
                 HandleError2();
             }
         }
     }
 }

I’ll go through the SDK code, to see if there’s a other/easier way – but above should actually work.

2

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật