Example #1
0
        private static Type GetLetterHandlerTypeFromDirectRoute(AddressData addressData)
        {
            Contract.Assert(addressData != null);

            var matchingAddressDatas = addressData.GetDirectAddressMatches();

            List <Type> letterHandlerTypes = new List <Type>();

            foreach (var directAddressData in matchingAddressDatas)
            {
                if (directAddressData != null)
                {
                    Type letterHandlerType = directAddressData.GetTargetLetterHandlerType();
                    if (letterHandlerType == null)
                    {
                        // We don't expect this to happen, but it could happen if some code messes with the
                        // address data tokens and removes the key we're looking for.
                        throw new InvalidOperationException("Missing Letter Handler Type");
                    }

                    if (!letterHandlerTypes.Contains(letterHandlerType))
                    {
                        letterHandlerTypes.Add(letterHandlerType);
                    }
                }
            }

            // We only want to handle the case where all matched direct addresses refer to the same letter handler.
            // Handling the multiple-letter handlers case would put attribute routing down a totally different
            // path than traditional addressing.
            if (letterHandlerTypes.Count == 0)
            {
                return(null);
            }
            else if (letterHandlerTypes.Count == 1)
            {
                return(letterHandlerTypes[0]);
            }
            else
            {
                throw CreateDirectAddressAmbiguousLetterHandlerException(letterHandlerTypes);
            }
        }