private void AppResolve(IApplicationBuilder app) { app.UseStaticFiles(); app.UseMvc(); _moduleProvider.Initialize(new ApplicationInitializationContext(app, _moduleProvider.Modules, _moduleProvider.VirtualPaths, AppConfig.Configuration)); app.Run(async(context) => { var messageId = Guid.NewGuid().ToString("N"); var sender = new HttpServerMessageSender(_serializer, context); try { var filters = app.ApplicationServices.GetServices <IAuthorizationFilter>().OrderByDescending(p => p.Order); var isSuccess = await OnAuthorization(context, sender, messageId, filters); if (isSuccess) { var actionFilters = app.ApplicationServices.GetServices <IActionFilter>().OrderByDescending(p => p.Order); await OnReceived(sender, messageId, context, actionFilters); } } catch (Exception ex) { var filters = app.ApplicationServices.GetServices <IExceptionFilter>(); WirteDiagnosticError(messageId, ex); await OnException(context, sender, messageId, ex, filters); } }); }
public async Task <bool> OnAuthorization(HttpContext context, HttpServerMessageSender sender, string messageId, IEnumerable <IAuthorizationFilter> filters) { foreach (var filter in filters) { var path = HttpUtility.UrlDecode(GetRoutePath(context.Request.Path.ToString())); path = AppConfig.MapRoutePathOptions.GetRoutePath(path, context.Request.Method); var serviceRoute = await _serviceRouteProvider.GetRouteByPathOrRegexPath(path, context.Request.Method); if (serviceRoute == null) { throw new CPlatformException($"未能找到:{path}-{context.Request.Method}的路由信息", StatusCode.Http404EndpointStatusCode); } context.Items.Add("route", serviceRoute); var filterContext = new AuthorizationFilterContext { Path = path, Context = context, Route = serviceRoute }; await filter.OnAuthorization(filterContext); if (filterContext.Result != null) { await sender.SendAndFlushAsync(new TransportMessage(messageId, filterContext.Result)); return(false); } } return(true); }
public async Task <bool> OnException(HttpContext context, HttpServerMessageSender sender, string messageId, Exception exception, IEnumerable <IExceptionFilter> filters) { foreach (var filter in filters) { var path = HttpUtility.UrlDecode(GetRoutePath(context.Request.Path.ToString())); var filterContext = new ExceptionContext { RoutePath = path, Context = context, Exception = exception }; await filter.OnException(filterContext); if (filterContext.Result != null) { await sender.SendAndFlushAsync(new TransportMessage(messageId, filterContext.Result)); return(false); } } return(true); }