/// <summary>Discovers the routes.</summary> /// <param name="builder">The builder.</param> /// <param name="routingTable">The routing table.</param> /// <param name="config">The configuration.</param> private static void DiscoverRoutes(IApplicationBuilder builder, IApiRoutingTable routingTable, IDeepSleepServiceConfiguration config) { var discoveryStrategies = config?.DiscoveryStrategies == null || config.DiscoveryStrategies.Count == 0 ? DiscoveryStrategies.Default() : config.DiscoveryStrategies.Where(d => d != null).ToList(); foreach (var strategy in discoveryStrategies) { if (strategy == null) { continue; } var task = Task.Run(async() => { using (var scope = builder.ApplicationServices.CreateScope()) { return(await strategy.DiscoverRoutes(scope.ServiceProvider).ConfigureAwait(false)); } }); var registrations = task.Result; foreach (var registration in registrations) { if (registration == null) { continue; } routingTable.AddRoute(registration); } } }
/// <summary>Processes the HTTP response unhandled exception.</summary> /// <param name="context">The context.</param> /// <param name="contextResolver">The context resolver.</param> /// <param name="exception">The exception.</param> /// <param name="config">The configuration.</param> /// <returns></returns> public static async Task <bool> ProcessHttpResponseUnhandledException(this ApiRequestContext context, IApiRequestContextResolver contextResolver, Exception exception, IDeepSleepServiceConfiguration config) { if (exception != null) { var code = context.HandleException(exception); if (config?.OnException != null && exception as ApiException == null) { try { await config.OnException(contextResolver, exception).ConfigureAwait(false); } catch { } } context.Response.StatusCode = code; } return(true); }
/// <summary>Invokes the specified httpcontext.</summary> /// <param name="httpcontext">The httpcontext.</param> /// <param name="contextResolver">The context resolver.</param> /// <param name="requestPipeline">The request pipeline.</param> /// <param name="config">The config.</param> /// <returns></returns> public async Task Invoke(HttpContext httpcontext, IApiRequestContextResolver contextResolver, IApiRequestPipeline requestPipeline, IDeepSleepServiceConfiguration config) { var path = httpcontext?.Request?.Path.ToString() ?? string.Empty; if (!IsIncludedPath(path, config.IncludePaths)) { await apinext.Invoke(httpcontext); return; } if (IsExcludedPath(path, config.ExcludePaths)) { await apinext.Invoke(httpcontext); return; } contextResolver.SetContext(await BuildApiRequestContext(httpcontext)); var context = contextResolver.GetContext(); var defaultRequestConfiguration = context.RequestServices.GetService(typeof(IDeepSleepRequestConfiguration)) as IDeepSleepRequestConfiguration; await context.ProcessApiRequest(httpcontext, contextResolver, requestPipeline, defaultRequestConfiguration); }