/// <summary> /// Gets the route prefix from the provided controller. /// </summary> /// <param name="controllerDescriptor">The controller descriptor.</param> /// <returns>The route prefix or null.</returns> protected virtual string GetRoutePrefix(ControllerDescriptor controllerDescriptor) { IRoutePrefix[] attributes = controllerDescriptor .GetCustomAttributes(inherit: false) .OfType <IRoutePrefix>() .ToArray(); if (attributes == null) { return(null); } if (attributes.Length > 1) { string errorMessage = Error.Format( MvcResources.RoutePrefix_CannotSupportMultiRoutePrefix, controllerDescriptor.ControllerType.FullName ); throw new InvalidOperationException(errorMessage); } if (attributes.Length == 1) { IRoutePrefix attribute = attributes[0]; if (attribute != null) { string prefix = attribute.Prefix; if (prefix == null) { string errorMessage = Error.Format( MvcResources.RoutePrefix_PrefixCannotBeNull, controllerDescriptor.ControllerType.FullName ); throw new InvalidOperationException(errorMessage); } if ( prefix.StartsWith("/", StringComparison.Ordinal) || prefix.EndsWith("/", StringComparison.Ordinal) ) { string errorMessage = Error.Format( MvcResources.RoutePrefix_CannotStartOrEnd_WithForwardSlash, prefix, controllerDescriptor.ControllerName ); throw new InvalidOperationException(errorMessage); } return(prefix); } } return(null); }
private static IRoutePrefix GetRoutePrefix(Type interfaceType) { IRoutePrefix routePrefix = interfaceType.GetCustomAttribute <ApiAttribute>() ?? interfaceType.GetInterfaces().FirstOrDefault()?.GetCustomAttribute <ApiAttribute>(); if (routePrefix == null) { routePrefix = interfaceType.GetCustomAttribute <IRoutePrefixAttribute>() ?? interfaceType.GetInterfaces().FirstOrDefault()?.GetCustomAttribute <IRoutePrefixAttribute>(); } return(routePrefix); }
/// <summary> /// Gets the route prefix from the provided controller. /// </summary> /// <param name="controllerDescriptor">The controller descriptor.</param> /// <returns>The route prefix or null.</returns> protected virtual string GetRoutePrefix(HttpControllerDescriptor controllerDescriptor) { Collection <IRoutePrefix> attributes = controllerDescriptor.GetCustomAttributes <IRoutePrefix>(inherit: false); if (attributes == null) { return(null); } if (attributes.Count > 1) { string errorMessage = Error.Format( SRResources.RoutePrefix_CannotSupportMultiRoutePrefix, controllerDescriptor.ControllerType.FullName ); throw new InvalidOperationException(errorMessage); } if (attributes.Count == 1) { IRoutePrefix attribute = attributes[0]; if (attribute != null) { string prefix = attribute.Prefix; if (prefix == null) { string errorMessage = Error.Format( SRResources.RoutePrefix_PrefixCannotBeNull, controllerDescriptor.ControllerType.FullName ); throw new InvalidOperationException(errorMessage); } if (prefix.EndsWith("/", StringComparison.Ordinal)) { throw Error.InvalidOperation( SRResources.AttributeRoutes_InvalidPrefix, prefix, controllerDescriptor.ControllerName ); } return(prefix); } } return(null); }
private static string GetRoutePrefix(ControllerDescriptor controllerDescriptor) { // this only happens once per controller type, for the lifetime of the application, // so we do not need to cache the results object[] attributes = controllerDescriptor.GetCustomAttributes(typeof(IRoutePrefix), inherit: false); if (attributes == null) { return null; } if (attributes.Length > 1) { string errorMessage = Error.Format( MvcResources.RoutePrefix_CannotSupportMultiRoutePrefix, controllerDescriptor.ControllerType.FullName); throw new InvalidOperationException(errorMessage); } if (attributes.Length == 1) { IRoutePrefix attribute = attributes[0] as IRoutePrefix; if (attribute != null) { string prefix = attribute.Prefix; if (prefix == null) { string errorMessage = Error.Format( MvcResources.RoutePrefix_PrefixCannotBeNull, controllerDescriptor.ControllerType.FullName); throw new InvalidOperationException(errorMessage); } if (prefix.StartsWith("/", StringComparison.Ordinal) || prefix.EndsWith("/", StringComparison.Ordinal)) { string errorMessage = Error.Format( MvcResources.RoutePrefix_CannotStartOrEnd_WithForwardSlash, prefix, controllerDescriptor.ControllerName); throw new InvalidOperationException(errorMessage); } return prefix; } } return null; }
public static string GetRouteTemplate(IRoutePrefix templatePrefix, IRoute template, MethodInfo methodInfo, IServiceProvider serviceLocator) { var interfaceType = methodInfo.DeclaringType; var templateResolver = ServiceProviderExtensions.GetService <IRouteTemplateResolver>(serviceLocator); var route = templateResolver?.GetTemplate(methodInfo); if (!String.IsNullOrWhiteSpace(route)) { return(route); } string prefix = ""; if (templatePrefix != null) { prefix = templatePrefix.Prefix; if (templatePrefix.IncludeTypeName) { prefix = prefix + "/" + (interfaceType.GetGenericArguments().Any() ? interfaceType.GetGenericArguments().FirstOrDefault()?.Name.ToLower() : interfaceType.GetInterfaces().FirstOrDefault()?.GetGenericArguments().First().Name.ToLower()); } } return((templatePrefix == null ? "" : (prefix + "/") + template.Template).Replace("//", "/")); }
/// <summary> /// Gets the route prefix from the provided controller. /// </summary> /// <param name="controllerDescriptor">The controller descriptor.</param> /// <returns> /// The route prefix or null. /// </returns> /// <exception cref="System.InvalidOperationException"> /// Cannot support multiple route prefix - + controllerDescriptor.ControllerType.FullName /// or /// Prefix cannot be null - + controllerDescriptor.ControllerType.FullName /// or /// Invalid Prefix - + controllerDescriptor.ControllerType.FullName /// </exception> protected override string GetRoutePrefix(HttpControllerDescriptor controllerDescriptor) { ICollection <IRoutePrefix> attributes = controllerDescriptor.GetCustomAttributes <IRoutePrefix>(inherit: true); if (attributes == null) { return(null); } if (attributes.Count > 1) { throw new InvalidOperationException("Cannot support multiple routeprefix - " + controllerDescriptor.ControllerType.FullName); } if (attributes.Count == 1) { IRoutePrefix attribute = attributes.FirstOrDefault(); if (attribute != null) { string prefix = attribute.Prefix; if (prefix == null) { throw new InvalidOperationException("Prefix cannot be null - " + controllerDescriptor.ControllerType.FullName); } if (prefix.EndsWith("/", StringComparison.Ordinal)) { throw new InvalidOperationException("Invalid Prefix - " + controllerDescriptor.ControllerType.FullName); } return(GetRoutePrefixForController(prefix, controllerDescriptor.ControllerName)); } } return(null); }
private static IRoutePrefix GetRoutePrefix(Type interfaceType) { ApiAttribute apiAttribute = interfaceType.GetCustomAttribute <ApiAttribute>(); if (apiAttribute == null) { Type element = ((IEnumerable <Type>)interfaceType.GetInterfaces()).FirstOrDefault <Type>(); apiAttribute = (object)element != null?element.GetCustomAttribute <ApiAttribute>() : (ApiAttribute)null; } IRoutePrefix routePrefix = (IRoutePrefix)apiAttribute; if (routePrefix == null) { IRoutePrefixAttribute iroutePrefixAttribute = interfaceType.GetCustomAttribute <IRoutePrefixAttribute>(); if (iroutePrefixAttribute == null) { Type element = ((IEnumerable <Type>)interfaceType.GetInterfaces()).FirstOrDefault <Type>(); iroutePrefixAttribute = (object)element != null?element.GetCustomAttribute <IRoutePrefixAttribute>() : (IRoutePrefixAttribute)null; } routePrefix = (IRoutePrefix)iroutePrefixAttribute; } return(routePrefix); }
public void InitializeClient(Type interfaceType) { ConcurrentDictionary <string, ActionWrapper> concurrentDictionary1; if (RestWrapper.cache.TryGetValue(interfaceType, out concurrentDictionary1)) { return; } ConcurrentDictionary <string, ActionWrapper> concurrentDictionary2 = new ConcurrentDictionary <string, ActionWrapper>(); IRoutePrefix routePrefix = RestWrapper.GetRoutePrefix(interfaceType); this._errorInterceptor = interfaceType.GetCustomAttribute <ErrorHandlerAttribute>(); this._serviceLocator.GetService <ILogger>()?.Message("Initializing client " + interfaceType.Name); RetryAttribute customAttribute = interfaceType.GetCustomAttribute <RetryAttribute>(); if (interfaceType.GetCustomAttribute <CircuitBreakerAttribute>() != null) { CircuitBreakerContainer.Register(interfaceType, (ICircuitBreaker) new Stardust.Interstellar.Rest.Client.CircuitBreaker.CircuitBreaker(interfaceType.GetCustomAttribute <CircuitBreakerAttribute>(), this._serviceLocator) { ServiceName = interfaceType.FullName }); } else { CircuitBreakerContainer.Register(interfaceType, (ICircuitBreaker) new NullBreaker()); } foreach (MethodInfo methodInfo in interfaceType.GetMethods().Length == 0 ? ((IEnumerable <Type>)interfaceType.GetInterfaces()).First <Type>().GetMethods() : interfaceType.GetMethods()) { try { RetryAttribute retryAttribute = methodInfo.GetCustomAttribute <RetryAttribute>() ?? customAttribute; this._serviceLocator.GetService <ILogger>()?.Message("Initializing client action " + interfaceType.Name + "." + methodInfo.Name); IRoute template = (IRoute)methodInfo.GetCustomAttribute <IRouteAttribute>() ?? (IRoute)methodInfo.GetCustomAttribute <VerbAttribute>(); string actionName = this.GetActionName(methodInfo); List <VerbAttribute> list = methodInfo.GetCustomAttributes(true).OfType <VerbAttribute>().ToList <VerbAttribute>(); ActionWrapper action = new ActionWrapper() { Name = actionName, ReturnType = methodInfo.ReturnType, RouteTemplate = ExtensionsFactory.GetRouteTemplate(routePrefix, template, methodInfo, this._serviceLocator), Parameters = new List <ParameterWrapper>() }; MethodInfo method = methodInfo; IServiceProvider serviceLocator = this._serviceLocator; List <HttpMethod> httpMethods = ExtensionsFactory.GetHttpMethods(list, method, serviceLocator); List <IHeaderInspector> headerInspectors = ExtensionsFactory.GetHeaderInspectors(methodInfo, this._serviceLocator); action.UseXml = methodInfo.GetCustomAttributes().OfType <UseXmlAttribute>().Any <UseXmlAttribute>(); action.CustomHandlers = headerInspectors.Where <IHeaderInspector>((Func <IHeaderInspector, bool>)(h => this.headerHandlers.All <IHeaderHandler>((Func <IHeaderHandler, bool>)(parent => parent.GetType() != h.GetType())))).ToList <IHeaderInspector>(); action.ErrorHandler = this._errorInterceptor; action.Actions = httpMethods; if (retryAttribute != null) { action.Retry = true; action.Interval = retryAttribute.RetryInterval; action.NumberOfRetries = retryAttribute.NumberOfRetries; action.IncrementalRetry = retryAttribute.IncremetalWait; if (retryAttribute.ErrorCategorizer != (Type)null) { action.ErrorCategorizer = (IErrorCategorizer)Activator.CreateInstance(retryAttribute.ErrorCategorizer, (object)this._serviceLocator); } } ExtensionsFactory.BuildParameterInfo(methodInfo, action, this._serviceLocator); concurrentDictionary2.TryAdd(action.Name, action); } catch (Exception ex) { this._logger?.Error(ex); throw; } } if (RestWrapper.cache.TryGetValue(interfaceType, out concurrentDictionary1)) { return; } RestWrapper.cache.TryAdd(interfaceType, concurrentDictionary2); }