在执行代码之前,有什么方法可以验证对 SendGridMessage 的服务总线触发器是否成功?

Any way to verify a service bus trigger to SendGridMessage succeeded before executing code?

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

我目前正在尝试将需要清理的消息推送到清理队列并在 try catch 之后处理它,但我认为我仍然 运行 遇到同样的问题。该函数可能成功,然后在 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();
}

您应该可以通过使用 ICollectorIAsyncCollector

来实现
[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();