Beispiel #1
0
        ActionSelectorCacheItem GetInternalSelector(HttpControllerDescriptor controllerDescriptor)
        {
            Contract.Requires(controllerDescriptor != null);
            Contract.Ensures(Contract.Result <ActionSelectorCacheItem>() != null);

            if (fastCache == null)
            {
                var selector = new ActionSelectorCacheItem(controllerDescriptor);
                CompareExchange(ref fastCache, selector, null);
                return(selector);
            }
            else if (fastCache.HttpControllerDescriptor == controllerDescriptor)
            {
                return(fastCache);
            }
            else
            {
                if (controllerDescriptor.Properties.TryGetValue(cacheKey, out var cacheValue))
                {
                    return((ActionSelectorCacheItem)cacheValue);
                }

                var selector = new ActionSelectorCacheItem(controllerDescriptor);
                controllerDescriptor.Properties.TryAdd(cacheKey, selector);
                return(selector);
            }
        }
        private ActionSelectorCacheItem GetInternalSelector(HttpControllerDescriptor controllerDescriptor)
        {
            // Performance-sensitive

            // First check in the local fast cache and if not a match then look in the broader
            // HttpControllerDescriptor.Properties cache
            if (_fastCache == null)
            {
                ActionSelectorCacheItem selector = new ActionSelectorCacheItem(controllerDescriptor);
                Interlocked.CompareExchange(ref _fastCache, selector, null);
                return(selector);
            }
            else if (_fastCache.HttpControllerDescriptor == controllerDescriptor)
            {
                // If the key matches and we already have the delegate for creating an instance then just execute it
                return(_fastCache);
            }
            else
            {
                // If the key doesn't match then lookup/create delegate in the HttpControllerDescriptor.Properties for
                // that HttpControllerDescriptor instance
                object cacheValue;
                if (controllerDescriptor.Properties.TryGetValue(_cacheKey, out cacheValue))
                {
                    return((ActionSelectorCacheItem)cacheValue);
                }
                // Race condition on initialization has no side effects
                ActionSelectorCacheItem selector = new ActionSelectorCacheItem(controllerDescriptor);
                controllerDescriptor.Properties.TryAdd(_cacheKey, selector);
                return(selector);
            }
        }
        ActionSelectorCacheItem GetInternalSelector(HttpControllerDescriptor controllerDescriptor)
        {
            controllerDescriptor = Decorator.GetInner(controllerDescriptor);

            if (fastCache == null)
            {
                var selector = new ActionSelectorCacheItem(controllerDescriptor);
                CompareExchange(ref fastCache, selector, null);
                return(selector);
            }
            else if (fastCache.HttpControllerDescriptor == controllerDescriptor)
            {
                return(fastCache);
            }
            else
            {
                if (controllerDescriptor.Properties.TryGetValue(cacheKey, out var cacheValue))
                {
                    return((ActionSelectorCacheItem)cacheValue);
                }

                var selector = new ActionSelectorCacheItem(controllerDescriptor);
                controllerDescriptor.Properties.TryAdd(cacheKey, selector);
                return(selector);
            }
        }
        private ActionSelectorCacheItem GetInternalSelector(HttpControllerDescriptor controllerDescriptor)
        {
            // Performance-sensitive

            // First check in the local fast cache and if not a match then look in the broader 
            // HttpControllerDescriptor.Properties cache
            if (_fastCache == null)
            {
                ActionSelectorCacheItem selector = new ActionSelectorCacheItem(controllerDescriptor);
                Interlocked.CompareExchange(ref _fastCache, selector, null);
                return selector;
            }
            else if (_fastCache.HttpControllerDescriptor == controllerDescriptor)
            {
                // If the key matches and we already have the delegate for creating an instance then just execute it
                return _fastCache;
            }
            else
            {
                // If the key doesn't match then lookup/create delegate in the HttpControllerDescriptor.Properties for
                // that HttpControllerDescriptor instance
                object cacheValue;
                if (controllerDescriptor.Properties.TryGetValue(_cacheKey, out cacheValue))
                {
                    return (ActionSelectorCacheItem)cacheValue;
                }
                // Race condition on initialization has no side effects
                ActionSelectorCacheItem selector = new ActionSelectorCacheItem(controllerDescriptor);
                controllerDescriptor.Properties.TryAdd(_cacheKey, selector);
                return selector;
            }
        }
 private ActionSelectorCacheItem GetInternalSelector(HttpControllerDescriptor controllerDescriptor)
 {
     // First check in the local fast cache and if not a match then look in the broader
     // HttpControllerDescriptor.Properties cache
     if (_fastCache == null)
     {
         ActionSelectorCacheItem selector = new ActionSelectorCacheItem(controllerDescriptor);
         Interlocked.CompareExchange(ref _fastCache, selector, null);
         return(selector);
     }
     else if (_fastCache.HttpControllerDescriptor == controllerDescriptor)
     {
         // If the key matches and we already have the delegate for creating an instance then just execute it
         return(_fastCache);
     }
     else
     {
         // If the key doesn't match then lookup/create delegate in the HttpControllerDescriptor.Properties for
         // that HttpControllerDescriptor instance
         ActionSelectorCacheItem selector = (ActionSelectorCacheItem)controllerDescriptor.Properties.GetOrAdd(
             _cacheKey,
             _ => new ActionSelectorCacheItem(controllerDescriptor));
         return(selector);
     }
 }
        public virtual ILookup <string, HttpActionDescriptor> GetActionMapping(HttpControllerDescriptor controllerDescriptor)
        {
            if (controllerDescriptor == null)
            {
                throw Error.ArgumentNull("controllerDescriptor");
            }

            ActionSelectorCacheItem internalSelector = GetInternalSelector(controllerDescriptor);

            return(internalSelector.GetActionMapping());
        }
        public virtual HttpActionDescriptor SelectAction(HttpControllerContext controllerContext)
        {
            if (controllerContext == null)
            {
                throw Error.ArgumentNull("controllerContext");
            }

            ActionSelectorCacheItem internalSelector = GetInternalSelector(controllerContext.ControllerDescriptor);

            return(internalSelector.SelectAction(controllerContext));
        }
        public override HttpActionDescriptor SelectAction(HttpControllerContext controllerContext)
        {
            if (controllerContext == null)
            {
                throw
                    new Exception
                    (
                        @"Error.ArgumentNull(""controllerContext"")"
                    );
            }

            ActionSelectorCacheItem internalSelector = GetInternalSelector(controllerContext.ControllerDescriptor);

            return(internalSelector.SelectAction(controllerContext));
        }
        public virtual ActionDescriptor SelectAction(ControllerContext controllerContext)
        {
            if (controllerContext == null)
            {
                throw Error.ArgumentNull("controllerContext");
            }
            var controller = controllerContext.Controller as Controller;
            if (controller == null)
            {
                throw new InvalidOperationException("Controller must inherit from System.Web.Mvc.Controller");
            }
            ApiControllerActionInvoker invoker = controller.ActionInvoker as ApiControllerActionInvoker;
            if (invoker == null)
            {
                throw new InvalidOperationException("ActionInvoker must inherit from ApiControllerActionInvoker.");
            }

            var controllerDescriptor = invoker.GetControllerDescriptor(controller.GetType());
            Func<object, object> valueFactory = null;

            if (this.cachedActionSelector == null)
            {
                ActionSelectorCacheItem actionSelectorItem = new ActionSelectorCacheItem(controllerDescriptor);
                Interlocked.CompareExchange<ActionSelectorCacheItem>(ref this.cachedActionSelector, actionSelectorItem, null);
                return actionSelectorItem.SelectAction(controllerContext);
            }
            if (this.cachedActionSelector.ControllerDescriptor == controllerDescriptor)
            {
                return this.cachedActionSelector.SelectAction(controllerContext);
            }
            if (valueFactory == null)
            {
                valueFactory = _ => new ActionSelectorCacheItem(controllerDescriptor);
            }
            var properties = ((ApiControllerDescriptor)controllerDescriptor).Properties;
            ActionSelectorCacheItem orAdd = (ActionSelectorCacheItem)properties.GetOrAdd(this._cacheKey, valueFactory);
            return orAdd.SelectAction(controllerContext);
        }
        private ActionSelectorCacheItem GetInternalSelector(HttpControllerDescriptor controllerDescriptor)
        {
            // First check in the local fast cache and if not a match then look in the broader
            // HttpControllerDescriptor.Properties cache
            if (this.fastCache == null)
            {
                var selector = new ActionSelectorCacheItem(controllerDescriptor);
                Interlocked.CompareExchange(ref this.fastCache, selector, null);
                return selector;
            }

            if (this.fastCache.HttpControllerDescriptor == controllerDescriptor)
            {
                // If the key matches and we already have the delegate for creating an instance then just execute it
                return this.fastCache;
            }

            // If the key doesn't match then lookup/create delegate in the HttpControllerDescriptor.Properties for
            // that HttpControllerDescriptor instance
            return (ActionSelectorCacheItem)controllerDescriptor.Properties.GetOrAdd(
               this.cacheKey,
               _ => new ActionSelectorCacheItem(controllerDescriptor));
        }
Beispiel #11
0
        Exception CreateAmbiguousActionException(IEnumerable <HttpActionDescriptor> matches)
        {
            var ambiguityList = ActionSelectorCacheItem.CreateAmbiguousMatchList(matches);

            return(new InvalidOperationException(SR.ApiControllerActionSelector_AmbiguousMatch.FormatDefault(ambiguityList)));
        }