我有一个 Azure Function HTTP 触发函数,它写入可能以重复条目结尾的 Azure 表。我注意到,即使我尝试/捕获了整个函数,仍然会有一个异常“泄漏”给函数运行器,从而返回 HTTP 500。有没有办法捕捉这种异常?
这是代码的缩小版本:
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Azure.WebJobs;
using Microsoft.Azure.WebJobs.Extensions.Http;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.Logging;
using Microsoft.WindowsAzure.Storage;
namespace FunctionTest
{
public class Entry
{
public string PartitionKey { get; set; }
public string RowKey { get; set; }
}
public static class Debug
{
[FunctionName("Debug")]
public static async Task<IActionResult> Run(
[HttpTrigger(AuthorizationLevel.Anonymous, "get", "post", Route = null)]
HttpRequest req,
[Table("Debug")]
IAsyncCollector<Entry> tableBinding,
ILogger log)
{
try
{
await tableBinding.AddAsync(new Entry()
{
PartitionKey = "1111",
RowKey = "1111",
});
await tableBinding.FlushAsync();
}
catch (StorageException)
{
// we expect an Exception "The specified entity already exists"
return new OkObjectResult("This passes test");
}
return new OkObjectResult("This passes test too");
}
}
}
该代码是在 Azure Function runtime 2.0(.NET Core 之一)下编写的。
触发/api/debug
两次或更多次,您将看到:
- HTTP 500
- 输入
catch{}
代码,但仍返回 HTTP 500(!) - 在 Application Insights 中,每个请求调用两个表依赖项(不应该发生,文档说表没有自动重试)