在执行代码之前,任何验证服务总线触发器SendGridMessage的方法都成功了吗?

时间:2018-05-15 19:43:52

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

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

我目前正试图将需要清理的消息推送到清理队列并在try catch之后处理它,但我认为我仍然遇到同样的问题。该函数可能会成功,然后在SendGrid输出上失败,并且消息将被清除,但会被抛回队列以在此函数上重新处理并失败。的Bleh。

队列触发器和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();
}

1 个答案:

答案 0 :(得分:1)

您应该可以使用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();