Beispiel #1
0
        /// <summary>
        /// Registers the controller.
        /// </summary>
        /// <param name="controllerType">Type of the controller.</param>
        /// <param name="controllerFactory">The controller factory method.</param>
        public void RegisterController(Type controllerType, Func <object> controllerFactory)
        {
            if (_controllerTypes.Contains(controllerType))
            {
                throw new ArgumentException("Controller types must be unique within the module");
            }

            var protoDelegate      = new ResponseHandler((server, context) => true);
            var protoAsyncDelegate = new AsyncResponseHandler((server, context) => Task.FromResult(true));
            var methods            = controllerType.GetMethods(BindingFlags.Instance | BindingFlags.Public)
                                     .Where(
                m => (m.ReturnType == protoDelegate.GetMethodInfo().ReturnType ||
                      m.ReturnType == protoAsyncDelegate.GetMethodInfo().ReturnType) &&
                m.GetParameters()
                .Select(pi => pi.ParameterType)
                .Take(2)
                .SequenceEqual(protoDelegate.GetMethodInfo().GetParameters()
                               .Select(pi => pi.ParameterType)));

            foreach (var method in methods)
            {
                var attribute =
                    method.GetCustomAttributes(typeof(WebApiHandlerAttribute), true).FirstOrDefault() as
                    WebApiHandlerAttribute;
                if (attribute == null)
                {
                    continue;
                }

                foreach (var path in attribute.Paths)
                {
                    if (_delegateMap.ContainsKey(path) == false)
                    {
                        _delegateMap.Add(path, new Dictionary <HttpVerbs, MethodCacheInstance>()); // add
                    }

                    var delegatePair = new MethodCacheInstance(controllerFactory, new MethodCache(method));

                    if (_delegateMap[path].ContainsKey(attribute.Verb))
                    {
                        _delegateMap[path][attribute.Verb] = delegatePair; // update
                    }
                    else
                    {
                        _delegateMap[path].Add(attribute.Verb, delegatePair); // add
                    }
                }
            }

            _controllerTypes.Add(controllerType);
        }
Beispiel #2
0
        /// <summary>
        /// Registers the controller.
        /// </summary>
        /// <param name="controllerType">Type of the controller.</param>
        /// <param name="controllerFactory">The controller factory method.</param>
        public void RegisterController(Type controllerType, Func <IHttpContext, object> controllerFactory)
        {
            if (_controllerTypes.Contains(controllerType))
            {
                throw new ArgumentException("Controller types must be unique within the module");
            }

            var methods = controllerType.GetMethods(BindingFlags.Instance | BindingFlags.Public)
                          .Where(m => m.ReturnType == typeof(bool) ||
                                 m.ReturnType == typeof(Task <bool>));

            foreach (var method in methods)
            {
                var attribute = method.GetCustomAttributes(typeof(WebApiHandlerAttribute), true).FirstOrDefault() as WebApiHandlerAttribute;
                if (attribute == null)
                {
                    continue;
                }

                foreach (var path in attribute.Paths)
                {
                    if (_delegateMap.ContainsKey(path) == false)
                    {
                        _delegateMap.Add(path, new Dictionary <HttpVerbs, MethodCacheInstance>()); // add
                    }

                    var delegatePair = new MethodCacheInstance(controllerFactory, new MethodCache(method));

                    if (_delegateMap[path].ContainsKey(attribute.Verb))
                    {
                        _delegateMap[path][attribute.Verb] = delegatePair; // update
                    }
                    else
                    {
                        _delegateMap[path].Add(attribute.Verb, delegatePair); // add
                    }
                }
            }

            _controllerTypes.Add(controllerType);
        }