/// <summary>Adds the route.</summary> /// <param name="template">The template.</param> /// <param name="httpMethods">The HTTP methods.</param> /// <param name="controller">The controller.</param> /// <param name="endpoint">The endpoint.</param> /// <param name="config">The configuration.</param> /// <returns></returns> public StaticRouteDiscoveryStrategy AddRoute(string template, IList <string> httpMethods, Type controller, string endpoint, IDeepSleepRequestConfiguration config) { var registration = new DeepSleepRouteRegistration( template: template, httpMethods: httpMethods, controller: controller, endpoint: endpoint, config: config); registrations.Add(registration); return(this); }
/// <summary>Initializes a new instance of the <see cref="DeepSleepSingleRouteRegistrationProvider"/> class.</summary> /// <param name="registration">The registration.</param> /// <exception cref="System.ArgumentNullException">registration</exception> public DeepSleepSingleRouteRegistrationProvider(DeepSleepRouteRegistration registration) { this.registration = registration ?? throw new ArgumentNullException(nameof(registration)); }
/// <summary>Discovers the routes.</summary> /// <param name="serviceProvider">The service provider.</param> /// <param name="assemblyDirectoryPath">The assembly directory path.</param> /// <param name="assemblyMatchPattern">The assembly match pattern.</param> /// <returns></returns> protected virtual Task<IList<DeepSleepRouteRegistration>> DiscoverRoutes( IServiceProvider serviceProvider, string assemblyDirectoryPath, string assemblyMatchPattern) { var registrations = new List<DeepSleepRouteRegistration>(); var files = Directory.GetFiles(assemblyDirectoryPath, assemblyMatchPattern, this.searchOption); foreach (var file in files) { Assembly assembly = null; try { assembly = Assembly.LoadFile(file); } catch { } if (assembly != null) { IEnumerable<MethodInfo> methods = new List<MethodInfo>(); try { methods = assembly .GetTypes() .SelectMany(t => t.GetMethods(bindingAttr: BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.InvokeMethod)) .Where(m => m.GetCustomAttribute<ApiRouteAttribute>() != null); } catch { } foreach (var method in methods) { var apiRoute = method.GetCustomAttribute<ApiRouteAttribute>(false); DeepSleepRequestConfiguration configuration = null; configuration = this.AssignRouteAllowAnonymousAttribute(configuration, method.GetCustomAttribute<ApiRouteAllowAnonymousAttribute>()); configuration = this.AssignRouteCacheDirectiveAttribute(configuration, method.GetCustomAttribute<ApiRouteCacheDirectiveAttribute>()); configuration = this.AssignRouteCrossOriginAttribute(configuration, method.GetCustomAttribute<ApiRouteCrossOriginAttribute>()); configuration = this.AssignRouteEnableHeadAttribute(configuration, method.GetCustomAttribute<ApiRouteEnableHeadAttribute>()); configuration = this.AssignRouteLanguageAttribute(configuration, method.GetCustomAttribute<ApiRouteLanguageSupportAttribute>()); configuration = this.AssignRouteRequestValidationAttribute(configuration, method.GetCustomAttribute<ApiRouteRequestValidationAttribute>()); configuration = this.AssignRouteValidationErrorConfigurationAttribute(configuration, method.GetCustomAttribute<ApiRouteValidationErrorConfigurationAttribute>()); configuration = this.AssignRouteErrorResponseProviderAttribute(configuration, method.GetCustomAttribute<ApiRouteErrorResponseProviderAttribute>()); configuration = this.AssignRouteMediaSerializerConfigurationAttribute(configuration, method.GetCustomAttribute<ApiRouteMediaSerializerConfigurationAttribute>()); var registration = new DeepSleepRouteRegistration( template: apiRoute.Template, httpMethods: apiRoute.HttpMethods, controller: Type.GetType(method.DeclaringType.AssemblyQualifiedName), methodInfo: method, config: configuration); registrations.Add(registration); } } } return Task.FromResult(registrations as IList<DeepSleepRouteRegistration>); }