【发布时间】:2020-07-19 20:52:00
【问题描述】:
我正试图围绕如何管理由事件网格事件触发的 Azure Functions 中的受控和不受控结果/失败。我确定我遗漏了一些基本方面,但我无法真正理解可用的 Microsoft 文档。
使用当前的 Visual Studio 2019 Azure 函数模板用于事件网格触发器,我们得到类似于以下内容的 C# 方法:
[FunctionName("UserCreated")]
public static void UserCreated([EventGridTrigger]EventGridEvent evt, ILogger log)
{
...
}
第一季度。从这些方法返回状态和错误代码以便可以重试或传递给死信 blob 的正确方法是什么?假设我想返回一个状态 400 错误请求,因为传入的事件中的自定义数据有效负载没有跟上速度。我试过这个:
[FunctionName("UserCreated")]
public static async Task<IActionResult> UserCreated([EventGridTrigger]EventGridEvent evt, ILogger log)
{
...
return new BadRequestObjectResult("ouch");
}
...在本地运行函数时只会导致此错误:
Cannot bind parameter '$return' to type IActionResult&. Make sure the parameter Type is supported by the binding.
我不明白这个错误,也不知道如何解决它。
第二季度。我们如何正确地捕获异常并以有序的方式返回它们?假设上面的 UserCreated 方法要求传入的事件具有一些自定义数据点,并且其中一个数据点丢失。例如。我们有这个:
[JsonObject(ItemRequired = Required.Always)]
private class CustomerAndContactIds
{
public int CustomerId { get; set; }
public int ContactId { get; set; }
}
... 当我们转换一些丢失的事件数据时,例如ContactID 字段,如下所示:
private static T ExtractCustomPayloadFromEvent<T>(EventGridEvent evt)
{
return JObject.FromObject(evt.Data).ToObject<T>();
}
...我们在日志中得到这个:
System.Private.CoreLib: Exception while executing function: UserCreated. Newtonsoft.Json: Required property 'ContactPersonId' not found in JSON. Path ''.
但是,我们得到的只是例如邮递员是:
Exception while executing function: UserCreated
对于不知情的消费者来说,让这个更易读的正确方法是什么? Azure 日志流或 Azure Insights?
【问题讨论】:
标签: error-handling azure-functions azure-eventgrid