public async Task <IRouteCollection> GenerateRouteCollectionAsync() { if (!_assemblies.Any()) { throw new InvalidOperationException("At least one assembly must be provided."); } if (!_nameMappers.Any()) { throw new InvalidOperationException("At least one name mapper must be provided."); } if (!_idMappers.Any()) { throw new InvalidOperationException("At least one ID mapper must be provided."); } if (_relativeUrlResolverMappers.Any() && _container == null) { throw new InvalidOperationException("Relative URL resolver mappers are configured but no container was provided."); } if (_restrictionMappers.Any() && _container == null) { throw new InvalidOperationException("Restriction mappers are configured but no container was provided."); } if (_customMapperMappers.Any() && _container == null) { throw new InvalidOperationException("Custom mapper mappers are configured but no container was provided."); } var routes = new RouteCollection(_duplicateRouteNamesAllowed); IEnumerable <Type> matchingTypes = await GetMatchingTypesAsync(); foreach (Type matchingType in matchingTypes) { IEnumerable <MethodInfo> matchingMethods = await GetMatchingMethodsAsync(matchingType); foreach (MethodInfo matchingMethod in matchingMethods) { string name = await GetNameAsync(matchingType, matchingMethod); if (name == null) { throw new ApplicationException(String.Format("Unable to determine a route name for '{0}.{1}'.", matchingType.FullName, matchingMethod.Name)); } Guid?id = await GetIdAsync(matchingType, matchingMethod); if (id == null) { throw new ApplicationException(String.Format("Unable to determine a route ID for '{0}.{1}'.", matchingType.FullName, matchingMethod.Name)); } Scheme?scheme = await GetSchemeAsync(matchingType, matchingMethod); if (scheme == null) { throw new ApplicationException(String.Format("Unable to determine a route scheme for '{0}.{1}'.", matchingType.FullName, matchingMethod.Name)); } var route = new Routing.Route(name, id.Value, scheme.Value); foreach (IRelativeUrlResolverMapper relativeUrlResolverMapper in _relativeUrlResolverMappers) { relativeUrlResolverMapper.Map(matchingType, matchingMethod, route, _container); } foreach (IRestrictionMapper restrictionMapper in _restrictionMappers) { restrictionMapper.Map(matchingType, matchingMethod, route, _container); } foreach (ICustomMapperMapper customMapperMapper in _customMapperMappers) { customMapperMapper.Map(matchingType, matchingMethod, route, _container); } await _responseMapper.MapAsync(() => _container, matchingType, matchingMethod, route); if (_authenticationProvider != null) { bool result = false; foreach (IAuthenticationStrategy authenticationStrategy in _authenticationStrategies) { if (await authenticationStrategy.MustAuthenticateAsync(matchingType, matchingMethod)) { result = true; break; } } if (result) { route.AuthenticationProvider(_authenticationProvider); } } routes.Add(route); } } foreach (Func <IRouteCollection, IEnumerable <Routing.Route> > @delegate in _additionalRoutes) { routes.Add(@delegate(routes)); } return(routes); }
public async Task <IRouteCollection> GenerateRouteCollectionAsync() { if (!_assemblies.Any()) { throw new InvalidOperationException("At least one assembly must be provided."); } if (!_nameMappers.Any()) { throw new InvalidOperationException("At least one name mapper must be provided."); } if (!_idMappers.Any()) { throw new InvalidOperationException("At least one ID mapper must be provided."); } if (!_resolvedRelativeUrlMappers.Any()) { throw new InvalidOperationException("At least one resolved relative URL mapper must be provided."); } if (_restrictionMappers.Any() && _restrictionContainer == null) { throw new InvalidOperationException("Restriction mappers are configured but no restriction container was provided."); } var routes = new RouteCollection(_duplicateRouteNamesAllowed); IEnumerable <Type> matchingTypes = await GetMatchingTypesAsync(); foreach (Type matchingType in matchingTypes) { IEnumerable <MethodInfo> matchingMethods = await GetMatchingMethodsAsync(matchingType); foreach (MethodInfo matchingMethod in matchingMethods) { string name = await GetNameAsync(matchingType, matchingMethod); if (name == null) { throw new ApplicationException(String.Format("Unable to determine a route name for '{0}.{1}'.", matchingType.FullName, matchingMethod.Name)); } Guid?id = await GetIdAsync(matchingType, matchingMethod); if (id == null) { throw new ApplicationException(String.Format("Unable to determine a route ID for '{0}.{1}'.", matchingType.FullName, matchingMethod.Name)); } Type[] ignoredResolvedRelativeUrlMapperTypes = matchingMethod .GetCustomAttributes <IgnoreResolvedRelativeUrlMapperTypeAttribute>() .SelectMany(arg => arg.IgnoredTypes) .ToArray(); string resolvedRelativeUrl = await GetResolvedRelativeUrlAsync(ignoredResolvedRelativeUrlMapperTypes, matchingType, matchingMethod); if (resolvedRelativeUrl == null) { throw new ApplicationException(String.Format("Unable to determine a route resolved relative URL for {0}.{1}.", matchingType.FullName, matchingMethod.Name)); } var route = new Routing.Route(name, id.Value, resolvedRelativeUrl); Type[] ignoredRestrictionMapperTypes = matchingMethod .GetCustomAttributes <IgnoreRestrictionMapperTypeAttribute>() .SelectMany(arg => arg.IgnoredTypes) .ToArray(); foreach (IRestrictionMapper restrictionMapper in _restrictionMappers.Where(arg => !ignoredRestrictionMapperTypes.Contains(arg.GetType()))) { await restrictionMapper.MapAsync(matchingType, matchingMethod, route, _restrictionContainer); } await _responseMapper.MapAsync(() => _endpointContainer, matchingType, matchingMethod, route); if (_authenticationProvider != null) { bool result = false; foreach (IAuthenticationStrategy authenticationStrategy in _authenticationStrategies) { if (await authenticationStrategy.MustAuthenticateAsync(matchingType, matchingMethod)) { result = true; break; } } if (result) { route.AuthenticationProvider(_authenticationProvider); } } routes.Add(route); } } foreach (Func <IRouteCollection, IEnumerable <Routing.Route> > @delegate in _additionalRoutes) { routes.Add(@delegate(routes)); } return(routes); }