代码之家  ›  专栏  ›  技术社区  ›  Pankaj Rawat

捕获Azure函数中的异步方法异常

  •  0
  • Pankaj Rawat  · 技术社区  · 7 年前

    Catch an exception thrown by an async method

    我有上面讨论过的相同问题,然而,Azure函数运行方法是异步任务,其异常处理与异步void相同。不确定哪里有问题?假设这是函数SDK问题。

    Azure函数

    public static class PingFunction
    {
        [LoggerAspect]
        [FunctionName("PingFunction")]
        public static async Task<HttpResponseMessage> Run([HttpTrigger(AuthorizationLevel.Anonymous, "get", "post", Route = null)]HttpRequestMessage req, TraceWriter log)
        {
            string name = string.Empty;
            log.Info("C# HTTP trigger function processed a request.");
    
                SomeService someService = new SomeService();
                await someService.DoSomething();
    
            return req.CreateResponse(HttpStatusCode.OK, "Hello " + name);
        }
    }
    
    public class SomeService
    {
        public async Task DoSomething()
        {
            await Task.Delay(1000);
            throw new Exception("Exception from Service");
        }
    }
    

    记录器方面(MrAdvise)

    public class LoggerAspectAttribute : Attribute, IMethodAdvice
    {
        public void Advise(MethodAdviceContext context)
        {
            //Logger initilizer here
            Console.WriteLine($"{context.TargetType.Name} started...");
            try
            {
                context.Proceed(); // this calls the original method
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
            finally
            {
                Console.WriteLine($"{context.TargetType.Name} completed...");
            }
        }
    }
    

    GetAwaiter().GetResult() ”“那么它就行了。

    public static HttpResponseMessage Run([HttpTrigger(AuthorizationLevel.Anonymous, "get", "post", Route = null)]HttpRequestMessage req, TraceWriter log)
        {
            string name = string.Empty;
            log.Info("C# HTTP trigger function processed a request.");
    
            SomeService someService = new SomeService();
            someService.DoSomething().GetAwaiter().GetResult();
    
            return req.CreateResponse(HttpStatusCode.OK, "Hello " + name);
        }
    

    Task.GetAwaiter().GetResult() methods cause the potential for deadlock issues and should be avoided in favor of async/await .

    我的函数每天处理数百万个事件。如果这是FunctionSDK问题或其他问题,这是正确的解决方案吗?

    1 回复  |  直到 7 年前
        1
  •  0
  •   picrap    7 年前

    您需要编写异步建议,例如:

    public class LoggerAspectAttribute : Attribute, IMethodAsyncAdvice
    {
        public async Task Advise(MethodAsyncAdviceContext context)
        {
            //Logger initilizer here
            Console.WriteLine($"{context.TargetType.Name} started...");
            try
            {
                await context.ProceedAsync(); // this calls the original method
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
            finally
            {
                Console.WriteLine($"{context.TargetType.Name} completed...");
            }
        }
    }
    

    编辑 :是的,它与Mr Advice一起工作:)