如何在异步 Azure 函数 HTTP 触发函数中捕获 Azure 表抛出的异常 [英] How to catch an Exception throw by Azure Table in an async Azure Function HTTP Triggered function

查看:31
本文介绍了如何在异步 Azure 函数 HTTP 触发函数中捕获 Azure 表抛出的异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 Azure 函数 HTTP 触发函数,它写入可能以重复条目结尾的 Azure 表.我注意到即使我尝试/捕获了整个函数,仍然会有一个异常泄漏"到函数运行器,从而返回 HTTP 500.有什么办法可以捕获这种异常?

I have an Azure Function HTTP triggered function which writes to Azure Table that may end in duplicated entries. I noticed that even if I try/catch'd the whole function, there will still be an Exception "leaked" to the function runner thus returning HTTP 500. Is there any way to catch this kind of exception?

这是代码的缩小版本:

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 运行时 2.0(.NET Core 之一)下编写的.

The code is written under Azure Function runtime 2.0 (the .NET Core one).

触发 /api/debug 两次或更多次,你会看到:

Trigger /api/debug twice or more and you will see:

  • HTTP 500
  • 输入了 catch{} 代码,但仍返回 HTTP 500(!)
  • 在 Application Insights 中,每个请求有两个表依赖项调用(不应该发生,文档说表没有自动重试)
  • HTTP 500
  • The catch{} code is entered, and still returns an HTTP 500(!)
  • In Application Insights, two table dependency call per request (shouldn't happen, the documentation says table do not have auto retry)

推荐答案

我想,使用 IAsyncCollector<> 会破坏这里的事情.如果你想避免此类问题,请尝试交换以下绑定:

I guess, that using IAsyncCollector<> breaks things here. If you want to avoid such problems, try to exchange the following binding:

[Table("Debug")] IAsyncCollector<Entry> tableBinding

到:

[Table("Debug")] CloudTable tableBinding

然后,不要使用 tableBinding.AddAsync() 使用以下代码段:

Then, instead of using tableBinding.AddAsync() use the following snippet:

var op = TableOperation.Insert(new Entry());
await tableBinding.ExecuteAsync(op);

使用这种方法,您应该能够捕获异常,而不会将其泄漏到 Functions 运行时.

With that approach, you should be able to catch the exception, without leaking it to the Functions runtime.

这篇关于如何在异步 Azure 函数 HTTP 触发函数中捕获 Azure 表抛出的异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆