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

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

问题描述

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

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?

下面是代码的缩小版本:

Here's a minified version of the code:

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);

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

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

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

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