无法将参数“消息”绑定到类型 ServiceBusReceivedMessage
Posted
技术标签:
【中文标题】无法将参数“消息”绑定到类型 ServiceBusReceivedMessage【英文标题】:Cannot bind parameter 'message' to type ServiceBusReceivedMessage 【发布时间】:2021-12-31 03:29:01 【问题描述】:我有如下注册的 Azure WebJobs 方法
Program.cs
hostBuilder.ConfigureWebJobs(webJobsBuilder =>
webJobsBuilder
.AddTimers()
.AddAzureStorageCoreServices()
.AddAzureStorageQueues()
.AddAzureStorageBlobs()
.AddServiceBus();
);
UserFunctions.cs
[FunctionName("SendRegisteredUserVerificationEmailsServiceBusTrigger")]
public async Task SendRegisteredUserVerificationEmailsServiceBusTrigger([ServiceBusTrigger(BookStoreServiceBusQueueNames.RegisteredUsers, Connection = "AzureWebJobsServiceBusConnectionString", IsSessionsEnabled = false)] Int32 deliveryCount,
DateTime enqueuedTimeUtc, ServiceBusReceivedMessage message, ServiceBusMessageActions messageActions)
...
我检查了以下代码示例: https://docs.microsoft.com/en-us/dotnet/api/overview/azure/microsoft.azure.webjobs.extensions.servicebus-readme-pre
而且找不到区别。
我正在使用以下 nuget 包:
-
https://www.nuget.org/packages/Microsoft.Azure.WebJobs.Extensions.ServiceBus/
https://www.nuget.org/packages/Azure.Messaging.ServiceBus/
我收到以下错误:
InvalidOperationException: Cannot bind parameter 'message' to type
ServiceBusReceivedMessage. Make sure the parameter Type is supported by the binding.
If you're using binding extensions (e.g. Azure Storage, ServiceBus, Timers, etc.) make
sure you've called the registration method for the extension(s) in your startup code (e.g.
builder.AddAzureStorage(), builder.AddServiceBus(), builder.AddTimers(), etc.).
我错过了什么吗?
【问题讨论】:
【参考方案1】:似乎存在命名问题。将message
参数绑定到ServiceBusReceivedMessage
类型似乎有问题。
每条消息触发器
要在每次将消息发送到服务总线队列或订阅时运行函数,请将ServiceBusTrigger
属性应用于string
、byte[]
或ServiceBusReceivedMessage
参数。
[FunctionName("TriggerSingle")]
public static void Run(
[ServiceBusTrigger("<queue_name>", Connection = "<connection_name>")] string messageBodyAsString,
ILogger logger)
logger.LogInformation($"C# function triggered to process a message: messageBodyAsString");
名为messageBodyAsString
的参数
批量触发器
要为一批收到的消息运行函数,请将ServiceBusTrigger
属性应用于string[]
或ServiceBusReceivedMessage[]
参数。
[FunctionName("TriggerBatch")]
public static void Run(
[ServiceBusTrigger("<queue_name>", Connection = "<connection_name>")] ServiceBusReceivedMessage[] messages,
ILogger logger)
foreach (ServiceBusReceivedMessage message in messages)
logger.LogInformation($"C# function triggered to process a message: message.Body");
logger.LogInformation($"EnqueuedTime=message.EnqueuedTime");
参数命名为messages
文档链接:https://docs.microsoft.com/en-us/dotnet/api/overview/azure/microsoft.azure.webjobs.extensions.servicebus-readme-pre
【讨论】:
ServiceBusReceivedMessage
的参数名呢?这就是我正在使用的。
我也试过ServiceBusReceivedMessage[]
,但效果不佳。
您是否尝试过完全像ServiceBusReceivedMessage[] messages
(消息的复数)?以上是关于无法将参数“消息”绑定到类型 ServiceBusReceivedMessage的主要内容,如果未能解决你的问题,请参考以下文章
使用 Windows.Azure.ServiceBus (5.2.0) 的 ServiceBus 消息处理程序无法使用 DataContractSerializer NET 4.6.1 反序列化正文流
错误索引方法“Class.Method”无法将参数“log”绑定到类型 TraceWriter
Azure 逻辑应用向 ServiceBus 发送 JSON 消息