/// <summary> /// 通过中间件后执行路由请求 /// </summary> /// <param name="route">路由条目</param> /// <param name="request">请求</param> /// <param name="response">响应</param> /// <returns>响应</returns> private IResponse RunRouteWithMiddleware(Route route, Request request, Response response) { try { var i = 0; foreach (var r in routeStack) { if (r.Equals(route) && ++i >= 2) { throw new RuntimeException("A circular dependency call was detected , uri [" + request.Uri + "]."); } } eventHub.Trigger(RouterEvents.OnDispatcher, this, new DispatchEventArgs(route, request)); if (middleware != null) { middleware.Do(request, response, (req, res) => { route.Run(request, response); }); } else { route.Run(request, response); } return(response); } catch (NotFoundRouteException) { throw; } catch (Exception ex) { var chain = route.GatherOnError(); var throwBubble = (chain == null); if (!throwBubble) { // 触发局部异常冒泡(如果局部被捕获则不会向全局冒泡) chain.Do(request, response, ex, (req, res, error) => { throwBubble = true; }); } if (throwBubble) { if (ThrowOnError(request, response, ex)) { return(null); } throw; } return(null); } }
/// <summary> /// 通过中间件后执行路由请求 /// </summary> /// <param name="route"></param> /// <param name="request"></param> /// <returns></returns> protected IResponse RunRouteWithMiddleware(Route route, Request request) { var response = new Response(); try { container.Instance(typeof(IResponse).ToString(), response); responseStack.Push(response); var middleware = route.GatherMiddleware(); if (middleware != null) { middleware.Do(request, response, (req, res) => { PrepareResponse(req, route.Run(req as Request, res as Response)); }); } else { PrepareResponse(request, route.Run(request, response)); } return(response); }catch (Exception ex) { var chain = route.GatherOnError(); if (chain != null) { chain.Do(request, response, ex, (req, res, error) => { ThrowOnError(request, response, ex); }); } else { ThrowOnError(request, response, ex); } return(null); } finally { responseStack.Pop(); if (responseStack.Count > 0) { container.Instance(typeof(IResponse).ToString(), responseStack.Peek()); } else { container.Instance(typeof(IResponse).ToString(), null); } } }
/// <summary> /// 触发异常冒泡 /// </summary> /// <param name="route">路由条目</param> /// <param name="request">当前请求</param> /// <param name="response">当前响应</param> /// <param name="ex">异常</param> /// <returns>冒泡是否已经被拦截</returns> private bool ThrowException(Route route, IRequest request, IResponse response, Exception ex) { var chain = route != null?route.GatherOnError() : null; var throwBubble = (chain == null); if (!throwBubble) { // 触发局部异常冒泡(如果局部被捕获则不会向全局冒泡) chain.Do(request, response, ex, (req, res, error) => { throwBubble = true; }); } return(!throwBubble || ThrowOnError(request, response, ex)); }
/// <summary> /// 通过中间件后执行路由请求 /// </summary> /// <param name="route">路由条目</param> /// <param name="request">请求</param> /// <returns>响应</returns> private IResponse RunRouteWithMiddleware(Route route, Request request) { var response = new Response(); try { container.Instance(typeof(IResponse).ToString(), response); responseStack.Push(response); if (middleware != null) { middleware.Do(request, response, (req, res) => { RunInRoute(route, request, response); }); } else { RunInRoute(route, request, response); } return(response); } catch (Exception ex) { var chain = route.GatherOnError(); if (chain != null) { chain.Do(request, response, ex, (req, res, error) => { ThrowOnError(request, response, ex); }); } else { ThrowOnError(request, response, ex); } return(null); } finally { responseStack.Pop(); container.Instance(typeof(IResponse).ToString(), responseStack.Count > 0 ? responseStack.Peek() : null); } }
/// <summary> /// 通过中间件后执行路由请求 /// </summary> /// <param name="route"></param> /// <param name="request"></param> /// <returns></returns> protected IResponse RunRouteWithMiddleware(Route route, Request request) { try { var response = new Response(); container.Instance(typeof(IResponse).ToString(), response); var middleware = route.GatherMiddleware(); if (middleware != null) { middleware.Then((req, res) => { PrepareResponse(req, route.Run(req as Request, res as Response)); }).Do(request, response); } else { PrepareResponse(request, route.Run(request, response)); } return(response); }catch (Exception ex) { var chain = route.GatherOnError(); if (chain != null) { chain.Then((req, error) => { ThrowOnError(request, ex); }).Do(request, ex); } else { ThrowOnError(request, ex); } return(null); } }