public async Task <IHttpResponse> HandleExceptionAsync(Exception ex, MethodInfo method, KeyValuePair <ParameterInfo, object>[] queryParameters, IApplication httpApp, IHttpRequest routeData, HandleExceptionDelegate continueExecution) { if (deactivated) { return(await continueExecution(ex, method, queryParameters, httpApp, routeData)); } var message = await CreateMessageCardAsync(ex, method, httpApp, routeData); try { var response = await message.SendAsync(teamsHookUrl); } catch (HttpRequestException) { } return(await continueExecution(ex, method, queryParameters, httpApp, routeData)); }
private static bool TryGetInvoke(Type handlerType, out HandleExceptionDelegate handler) { if (_handlers.TryGetValue(handlerType, out handler)) { return(true); } lock (_syncHelper) { if (_handlers.TryGetValue(handlerType, out handler)) { return(true); } var search = from it in handlerType.GetTypeInfo().GetMethods() let parameters = it.GetParameters() where it.Name == "HandleExceptionAsync" && it.ReturnType == typeof(Task) && parameters.FirstOrDefault()?.ParameterType == typeof(ExceptionContext) select it; MethodInfo handleExceptionAsyncMethod = search.FirstOrDefault(); if (null == handleExceptionAsyncMethod) { return(false); } ParameterExpression handlerParameter = Expression.Parameter(typeof(object), "handler"); ParameterExpression contextParameter = Expression.Parameter(typeof(ExceptionContext), "exceptionContext"); ParameterExpression serviceProviderParameter = Expression.Parameter(typeof(IServiceProvider), "serviceProvider"); var arguments = handleExceptionAsyncMethod.GetParameters().Select(it => GetArgument(contextParameter, serviceProviderParameter, it.ParameterType)); Expression instance = Expression.Convert(handlerParameter, handlerType); var handleExceptionAsync = Expression.Call(instance, handleExceptionAsyncMethod, arguments); handler = Expression.Lambda <HandleExceptionDelegate>(handleExceptionAsync, handlerParameter, contextParameter, serviceProviderParameter).Compile(); _handlers[handlerType] = handler; } return(true); }