【问题标题】:Any way to verify a service bus trigger to SendGridMessage succeeded before executing code?在执行代码之前,有什么方法可以验证 SendGridMessage 的服务总线触发器成功吗?
【发布时间】:2018-10-25 18:02:14
【问题描述】:

我现在有一个在服务总线触发器(队列触发器)上运行并输出 SendGridMessage 的天蓝色函数。诀窍是在函数成功发送 sendgrid 消息后,我需要在我的 blob 存储中进行一些清理,但似乎我无法确定函数是否成功,直到它超出范围。

我目前正在尝试将需要清理的消息推送到清理队列并在尝试捕获后处理它,但我认为我仍然遇到同样的问题。该函数可能成功,然后在 SendGrid 输出上失败,消息将被清理但被扔回队列以在此函数上重新处理并失败。呵呵。

队列触发和 Sendgrid 输出

[FunctionName("ProcessEmail")]
public static void Run([ServiceBusTrigger("email-queue-jobs", AccessRights.Manage, 
    Connection = "MicroServicesServiceBus")]OutgoingEmail outgoingEmail, TraceWriter log,
    [ServiceBus("email-queue-cleanup", Connection = "MicroServicesServiceBus", 
    EntityType = Microsoft.Azure.WebJobs.ServiceBus.EntityType.Queue)] IAsyncCollector<OutgoingEmail> cleanupEmailQueue,
    [SendGrid] out SendGridMessage message)
{
    try
    {
        log.Info($"Attempting to send the email {outgoingEmail.Id}");
        message = SendgridHelper.ConvertToSendgridMessage(outgoingEmail);

        log.Info("Successfully sent email:");
        log.Info(JsonConvert.SerializeObject(outgoingEmail));
    }
    catch (Exception ex)
    {
        message = null;
        throw ex;
    }

    // Add email to the cleanup queue
    log.Info("Sending email to the cleanup queue.");
    cleanupEmailQueue.AddAsync(outgoingEmail).Wait();
}

【问题讨论】:

    标签: c# azure message-queue azure-functions sendgrid


    【解决方案1】:

    您应该可以通过使用ICollector 或IAsyncCollector 来实现此目的

    [SendGrid] ICollector<SendGridMessage> messageCollector)
    

    然后

    var message = SendgridHelper.ConvertToSendgridMessage(outgoingEmail);
    messageCollector.Add(message);
    

    应该同步调用SendGrid,如果失败则抛出异常。

    如果您想使用IAsyncCollector(就像您已经为另一个绑定所做的那样),请务必也调用FlushAsync 方法:

    [SendGrid] IAsyncCollector<SendGridMessage> messageCollector)
    

    然后

    var message = SendgridHelper.ConvertToSendgridMessage(outgoingEmail);
    await messageCollector.AddAsync(message);
    await messageCollector.FlushAsync();
    

    【讨论】:

    • 感谢您的回复。 FlushAsync 的目的是什么?
    • FlushAsync 强制绑定发送数据
    猜你喜欢
    • 2014-02-02
    • 2016-05-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-01-05
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多