public ResolvedService(ResolvedService service, IDictionary<string, object> routeValues) { if (service == null) { throw new ArgumentNullException("service", "service cannot be null."); } this.AfterActions = new List<FilterAction>(service.AfterActions); this.BeforeActions = new List<FilterAction>(service.BeforeActions); this.Encodings = new List<IEncoding>(service.Encodings); this.ErrorActions = new List<FilterAction>(service.ErrorActions); this.Formats = new List<IFormat>(service.Formats); this.Method = service.Method; this.Name = service.Name; this.RequestType = service.RequestType; this.RouteValues = routeValues ?? new Dictionary<string, object>(); }
private static bool RoutePatternsMatch(ResolvedService service, IDictionary<string, object> routeValues) { bool success = true; foreach (KeyValuePair<string, object> pair in routeValues) { if (service.Method.Endpoint.ParameterPatterns.ContainsKey(pair.Key)) { if (!service.Method.Endpoint.ParameterPatterns[pair.Key].IsMatch(pair.Value as string ?? string.Empty)) { success = false; break; } } } return success; }
private static IDictionary<MethodType, IEnumerable<ResolvedService>> ResolveAllServices(ServiceCollection serviceCollection) { const string MultipleMethodsMessage = "There are multiple {0} methods registered for endpoint {1}. You may only register one method type per endpoint."; List<ResolvedService> deleteServices = new List<ResolvedService>(); List<ResolvedService> getServices = new List<ResolvedService>(); List<ResolvedService> postServices = new List<ResolvedService>(); List<ResolvedService> putServices = new List<ResolvedService>(); if (serviceCollection != null) { foreach (Service service in serviceCollection) { foreach (Endpoint endpoint in service.Endpoints as EndpointCollection) { bool hasDelete = false, hasGet = false, hasPost = false, hasPut = false; foreach (Method method in endpoint.Methods as MethodCollection) { IEnumerable<FilterAction> beforeActions = ServiceResolver.ResolveBeforeActions(serviceCollection.Pipeline, service.Pipeline, endpoint.Pipeline, method.Pipeline); IEnumerable<FilterAction> afterActions = ServiceResolver.ResolveAfterActions(serviceCollection.Pipeline, service.Pipeline, endpoint.Pipeline, method.Pipeline); IEnumerable<FilterAction> errorActions = ServiceResolver.ResolveErrorActions(serviceCollection.Pipeline, service.Pipeline, endpoint.Pipeline, method.Pipeline); ResolvedService resolvedService = new ResolvedService( service.Name, method, afterActions, beforeActions, ServiceResolver.ResolveEncodings(serviceCollection.Pipeline, service.Pipeline, endpoint.Pipeline, method.Pipeline), errorActions, ServiceResolver.ResolveFormats(serviceCollection.Pipeline, service.Pipeline, endpoint.Pipeline, method.Pipeline), ServiceResolver.ResolveRequestType(method, beforeActions, afterActions, errorActions)); bool throwMultipleMethods = false; switch (method.MethodType) { case MethodType.Delete: deleteServices.Add(resolvedService); throwMultipleMethods = hasDelete; hasDelete = true; break; case MethodType.Get: getServices.Add(resolvedService); throwMultipleMethods = hasGet; hasGet = true; break; case MethodType.Post: postServices.Add(resolvedService); throwMultipleMethods = hasPost; hasPost = true; break; case MethodType.Put: putServices.Add(resolvedService); throwMultipleMethods = hasPut; hasPut = true; break; default: throw new NotImplementedException(); } if (throwMultipleMethods) { throw new InvalidOperationException(string.Format(CultureInfo.InvariantCulture, MultipleMethodsMessage, method.MethodType, endpoint.Route)); } } } } } Dictionary<MethodType, IEnumerable<ResolvedService>> result = new Dictionary<MethodType, IEnumerable<ResolvedService>>(); result.Add(MethodType.Delete, deleteServices); result.Add(MethodType.Get, getServices); result.Add(MethodType.Post, postServices); result.Add(MethodType.Put, putServices); return result; }