You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用C#编写Azure函数向队列添加消息遇异常问题

Let's tackle your two questions about Azure Functions and Service Bus queues step by step:

1. Is using ICollector<string> feasible, and if so, what's causing the missing messages?

Absolutely, ICollector<string> is a valid and straightforward way to send string messages to a Service Bus queue via Azure Functions bindings. If your local run shows no errors but messages aren't appearing in the queue, here are the most likely issues to check:

  • Incorrect connection string: Double-check your local.settings.json file to ensure the AzureWebJobsServiceBus setting uses the correct connection string for your Service Bus namespace. Make sure the connection has Send or Manage permissions enabled for the queue.
  • Function execution failure: ICollector batches messages and only commits them to the queue when the function completes successfully. If your code throws an unhandled exception after calling queue.Add(msg), or returns early before finishing execution, the messages won't be sent. Add logging like log.Info($"Added message to collector: {msg}") to confirm the Add method runs, and verify the rest of your function logic works without issues.
  • Queue name mismatch: Ensure the queue name in your binding ([Queue("myqueuename")]) exactly matches the name of the queue in your Service Bus namespace—names are case-sensitive!
2. Is the ServiceBus v1.1 NuGet package (with BrokeredMessage) suitable for this scenario?

No, the legacy WindowsAzure.ServiceBus (v1.1) package and its BrokeredMessage class are not compatible with Azure Functions' modern Service Bus bindings. Here's why:

Azure Functions uses the newer Microsoft.Azure.ServiceBus SDK under the hood for its Service Bus integrations. The two SDKs are completely separate and don't share compatible types—passing a BrokeredMessage from v1.1 to an ICollector<string> causes serialization errors (like the "unable to read DeliveryCount" problem you encountered) because the binding expects a string, not a legacy message object.

If you need to send a custom object (like your test class), use one of these compatible approaches instead:

  • Use ICollector<test> directly as your function parameter. The Functions runtime will automatically serialize the object to JSON and send it as a queue message.
  • Manually serialize your object to a string (e.g., with JsonConvert.SerializeObject() from Newtonsoft.Json) and send that string via ICollector<string>.

内容的提问来源于stack exchange,提问作者Paul Michaels

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.15 07:12:46