0

我注册了 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 包:

  1. 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.).

我在这里错过了什么吗?

4

1 回答 1

-1

似乎有一个命名问题。将参数绑定message到类型ServiceBusReceivedMessage似乎有问题。

每条消息触发器

要在每次将消息发送到服务总线队列或订阅时运行函数,请将ServiceBusTrigger属性应用于stringbyte[]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

于 2021-11-20T23:02:11.753 回答