示例#1
0
        public void InitializeClient(Type interfaceType)
        {
            ConcurrentDictionary <string, ActionWrapper> wrapper;

            if (cache.TryGetValue(interfaceType, out wrapper))
            {
                return;
            }
            var newWrapper     = new ConcurrentDictionary <string, ActionWrapper>();
            var templatePrefix = interfaceType.GetCustomAttribute <IRoutePrefixAttribute>();

            foreach (var methodInfo in interfaceType.GetMethods())
            {
                var template   = methodInfo.GetCustomAttribute <RouteAttribute>();
                var actionName = GetActionName(methodInfo);
                var action     = new ActionWrapper {
                    Name = actionName, ReturnType = methodInfo.ReturnType, RouteTemplate = ExtensionsFactory.GetRouteTemplate(templatePrefix, template, methodInfo), Parameters = new List <ParameterWrapper>()
                };
                var actions  = methodInfo.GetCustomAttributes(true).OfType <IActionHttpMethodProvider>().ToList();
                var methods  = ExtensionsFactory.GetHttpMethods(actions, methodInfo);
                var handlers = ExtensionsFactory.GetHeaderInspectors(methodInfo);
                action.CustomHandlers = handlers;
                action.Actions        = methods;
                ExtensionsFactory.BuildParameterInfo(methodInfo, action);
                newWrapper.TryAdd(action.Name, action);
            }
            if (cache.TryGetValue(interfaceType, out wrapper))
            {
                return;
            }
            cache.TryAdd(interfaceType, newWrapper);
        }
 internal static List<HttpMethod> GetHttpMethods(List<IActionHttpMethodProvider> actions, MethodInfo method)
 {
     var methodResolver = ExtensionsFactory.GetService<IWebMethodConverter>();
     var methods = new List<HttpMethod>();
     if (methodResolver != null) methods.AddRange(methodResolver.GetHttpMethods(method));
     foreach (var actionHttpMethodProvider in actions)
     {
         methods.AddRange(actionHttpMethodProvider.HttpMethods);
     }
     if (methods.Count == 0) methods.Add(HttpMethod.Get);
     return methods;
 }
 internal static List<IHeaderHandler> GetHeaderInspectors(MethodInfo methodInfo)
 {
     var inspectors = methodInfo.GetCustomAttributes().OfType<IHeaderInspector>().ToList();
     var headerInspectors = ExtensionsFactory.GetServices<IHeaderHandler>();
     var handlers = new List<IHeaderHandler>();
     if (headerInspectors != null) handlers.AddRange(headerInspectors);
     foreach (var inspector in inspectors)
     {
         handlers.AddRange(inspector.GetHandlers());
     }
     return handlers;
 }