c# - Azure Function App Azure 服务总线触发器触发两次

标签 c# azure servicebus azure-function-app

我使用带有服务总线触发器的 Azure Function Apps 来读取服务总线并处理服务总线消息的内容。服务总线接收 JSON 序列化对象,然后将 JSON 消息反序列化回 Function App 中的对象。然而,由于某种原因,每当有东西发送到服务总线时,触发器就会触发两次,一次是 JSON 消息,另一次是包含文本“服务总线消息”的消息。

我不知道是什么导致了第二条消息,所以我想到的只是反序列化服务总线消息,当它因“服务总线消息”而失败时,我将捕获异常并忽略它。但是,我认为这不是处理此问题的正确方法,并且 JSON 反序列化中的实际错误将被忽略。处理第二条消息的正确方法是什么?

示例代码:

using Newtonsoft.Json;

public static void Run(string myQueueItem, ILogger log, out SendGridMessage mailMessage)
{
    MyClass myObject = null;
    try
    {
        myObject = JsonConvert.DeserializeObject<MyClass>(myQueueItem);
        // do stuff
    }
    catch (JsonException je)
    {
        // ignore this exception
    }
    catch (Exception e)
    {
        // handle the other exceptions and send an alert
    }
}

编辑:我在原始帖子中遗漏的一件事是我也有 SendGrid 输出。我不明白为什么这会对触发器产生任何影响,这就是我将其排除在外的原因。不过,我现在添加它,以防万一它可能是由于某些奇怪的原因引起的。

编辑 2:添加了突出显示问题的完整工作代码

// Sender
using System;
using System.Text;
using System.Threading.Tasks;
using Microsoft.Azure.ServiceBus;
using Newtonsoft.Json;

namespace myapp
{
    class Program
    {
        static void Main(string[] args) => MainAsync().GetAwaiter().GetResult();

        static async Task MainAsync()
        {
            string serviceBusConnectionString = "<your servicebus connectionstring>";
            string queueName = "<your queue name>";
            IQueueClient queueClient = new QueueClient(serviceBusConnectionString, queueName);
            string messageBody = JsonConvert.SerializeObject(new Test());
            var message = new Message(Encoding.UTF8.GetBytes(messageBody));
            await queueClient.SendAsync(message);
            await queueClient.CloseAsync();
        }

        class Test
        {
            public string Text { get; set; } = "test";
        }
    }
}
// Receiver
#r "Newtonsoft.Json"

using System;
using System.Threading.Tasks;
using Newtonsoft.Json;

public static void Run(string myQueueItem, ILogger log)
{
    try
    {
        Test test = JsonConvert.DeserializeObject<Test>(myQueueItem);
        log.LogInformation($"C# ServiceBus queue trigger function processed message: {test.Text}");
    }
    catch (Exception e)
    {
        log.LogInformation($"Exception: {e.Message}");
    }
}

class Test
{
    public string Text { get; set; } = "test";
}

结果:

2019-11-04T08:38:10.544 [Information] Executing 'Functions.test' (Reason='This function was programmatically called via the host APIs.', Id=06e10006-baa6-430f-b5db-9a5eae5c154c)
2019-11-04T08:38:10.659 [Information] Exception: Unexpected character encountered while parsing value: S. Path '', line 0, position 0. // This here is caused by "Service bus message"
2019-11-04T08:38:10.659 [Information] Executed 'Functions.test' (Succeeded, Id=06e10006-baa6-430f-b5db-9a5eae5c154c)
2019-11-04T08:39:02.582 [Information] Executing 'Functions.test' (Reason='New ServiceBus message detected on 'test'.', Id=5d7622a7-cc8d-44c9-8720-626fdbb8cabb)
2019-11-04T08:39:02.583 [Information] Trigger Details: MessageId: 13bba779fd524fa1a0098acd6634aded, DeliveryCount: 1, EnqueuedTime: 11/4/2019 8:39:02 AM, LockedUntil: 11/4/2019 8:39:07 AM
2019-11-04T08:39:02.584 [Information] C# ServiceBus queue trigger function processed message: test // This here is the actual message
2019-11-04T08:39:02.584 [Information] Executed 'Functions.test' (Succeeded, Id=5d7622a7-cc8d-44c9-8720-626fdbb8cabb)

最佳答案

根据我的测试,触发器不会被触发两次。

namespace ServiceBusTrigger
{
    public static class Function1
    {
        [FunctionName("Function1")]
        public static void Run([ServiceBusTrigger("myqueue", Connection = "ServiceBusConnection")]string myQueueItem, ILogger log)
        {
            log.LogInformation($"C# ServiceBus queue trigger function processed message: {myQueueItem}");

            var myMessage = JsonConvert.DeserializeObject<MyMessage>(myQueueItem);

            log.LogInformation($"ID: {myMessage.id}\tContent:{myMessage.content}");
        }
    }
}

手动发送消息,并得到以下输出:


Pause Clear Expand

2019-11-04T08:18:56.837 [Information] Executing 'Function1' (Reason='New ServiceBus message detected on 'myqueue'.', Id=7fb37483-b9a3-4c19-baa3-235b419bb585)
2019-11-04T08:18:56.837 [Information] Trigger Details: MessageId: c00a8e41-d883-4620-992f-6b9b4fdae320, DeliveryCount: 1, EnqueuedTime: 11/4/2019 8:18:56 AM, LockedUntil: 11/4/2019 8:19:26 AM
2019-11-04T08:18:56.838 [Information] C# ServiceBus queue trigger function processed message: {"id":"1","content":"abc"}
2019-11-04T08:18:56.838 [Information] ID: 1 Content:abc
2019-11-04T08:18:56.839 [Information] Executed 'Function1' (Succeeded, Id=7fb37483-b9a3-4c19-baa3-235b419bb585)
<小时/>

那么,请检查您的服务总线生产者,看看是否发送了任何其他消息。

您可以停止函数应用,手动发送消息并检查门户:

enter image description here

如果一切正常,那么应该只有 1 条事件消息。

<小时/>

更新

enter image description here

enter image description here

关于c# - Azure Function App Azure 服务总线触发器触发两次,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58656425/

相关文章:

c# - 使用 Linq 从集合中获取组

azure - 如何使用 Azure Key-Vault 检索连接字符串,然后将其设置为 AzureWebJobsServiceBus for ServiceBusTrigger

azure - 在 Azure ServiceBus 中使用大量实体是否有缺点

c# - DirectX/SharpDX 中类似 "glReadPixels"的函数

c# - 如何使用Environment.NewLine作为可选参数默认值

c# - 使用 FakeItEasy 的 Winrt 测试项目

azure - 为什么大多数工作项目从 Azure DevOps 中消失了?

azure - 微软云防御者给出尚未收到VM消息的调查结果

nservicebus - 什么是服务总线?

c# - 用于处理 azure 服务总线队列中的消息处理的选项