Exemple #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)
        {
            var protoDelegate      = new ResponseHandler((server, context) => true);
            var protoAsyncDelegate = new AsyncResponseHandler((server, context) => Task.FromResult(true));

            var methods = controllerType
                          .GetMethods(BindingFlags.Instance | BindingFlags.Public)
#if NET452
                          .Where(
                m => (m.ReturnType == protoDelegate.Method.ReturnType ||
                      m.ReturnType == protoAsyncDelegate.Method.ReturnType) &&
                m.GetParameters()
                .Select(pi => pi.ParameterType)
                .Take(2)
                .SequenceEqual(protoDelegate.Method.GetParameters()
#else
                          .Where(
                m => (m.ReturnType == protoDelegate.GetMethodInfo().ReturnType ||
                      m.ReturnType == protoAsyncDelegate.GetMethodInfo().ReturnType) &&
                      m.GetParameters()
                      .Select(pi => pi.ParameterType)
                      .Take(2)
                      .SequenceEqual(protoDelegate.GetMethodInfo().GetParameters()
#endif
                               .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, Tuple <Func <object>, MethodInfo> >()); // add
                    }

                    var delegatePair = new Tuple <Func <object>, MethodInfo>(controllerFactory, method);

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

            _controllerTypes.Add(controllerType);
        }
Exemple #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 protoDelegate      = new ResponseHandler(() => true);
            var protoAsyncDelegate = new AsyncResponseHandler(() => Task.FromResult(true));
            var methods            = controllerType.GetMethods(BindingFlags.Instance | BindingFlags.Public)
                                     .Where(m => m.ReturnType == protoDelegate.GetMethodInfo().ReturnType ||
                                            m.ReturnType == protoAsyncDelegate.GetMethodInfo().ReturnType);

            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);
        }