Example #1
0
        public override void Validate(object value)
        {
            HttpMessageHandlerFactoryElement configElement = (HttpMessageHandlerFactoryElement)value;

            if (!string.IsNullOrWhiteSpace(configElement.Type) && configElement.Handlers != null && configElement.Handlers.Count > 0)
            {
                throw FxTrace.Exception.AsError(new ConfigurationErrorsException(SR.GetString(SR.HttpMessageHandlerFactoryConfigInvalid_WithBothTypeAndHandlerList, ConfigurationStrings.MessageHandlerFactory, ConfigurationStrings.Type, ConfigurationStrings.Handlers)));
            }
        }
        internal static HttpMessageHandlerFactory CreateFromConfigurationElement(HttpMessageHandlerFactoryElement configElement)
        {
            Fx.Assert(configElement != null, "configElement should not be null.");

            if (!string.IsNullOrWhiteSpace(configElement.Type))
            {
                if (configElement.Handlers != null && configElement.Handlers.Count > 0)
                {
                    throw FxTrace.Exception.AsError(new ConfigurationErrorsException(SR.GetString(SR.HttpMessageHandlerFactoryConfigInvalid_WithBothTypeAndHandlerList, ConfigurationStrings.MessageHandlerFactory, ConfigurationStrings.Type, ConfigurationStrings.Handlers)));
                }

                Type factoryType = HttpChannelUtilities.GetTypeFromAssembliesInCurrentDomain(configElement.Type);
                if (factoryType == null)
                {
                    throw FxTrace.Exception.AsError(new ConfigurationErrorsException(SR.GetString(SR.CanNotLoadTypeGotFromConfig, configElement.Type)));
                }

                if (!typeof(HttpMessageHandlerFactory).IsAssignableFrom(factoryType) || factoryType.IsAbstract)
                {
                    throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ConfigurationErrorsException(
                        SR.GetString(
                        SR.WebSocketElementConfigInvalidHttpMessageHandlerFactoryType,
                        typeof(HttpMessageHandlerFactory).Name,
                        factoryType,
                        typeof(HttpMessageHandlerFactory).AssemblyQualifiedName)));
                }

                return Activator.CreateInstance(factoryType) as HttpMessageHandlerFactory;
            }
            else
            {
                if (configElement.Handlers == null || configElement.Handlers.Count == 0)
                {
                    return null;
                }

                Type[] handlerList = new Type[configElement.Handlers.Count];
                for (int i = 0; i < configElement.Handlers.Count; i++)
                {
                    Type handlerType = HttpChannelUtilities.GetTypeFromAssembliesInCurrentDomain(configElement.Handlers[i].Type);
                    if (handlerType == null)
                    {
                        throw FxTrace.Exception.AsError(new ConfigurationErrorsException(SR.GetString(SR.CanNotLoadTypeGotFromConfig, configElement.Handlers[i].Type)));
                    }

                    handlerList[i] = handlerType;
                }

                try
                {
                    return new HttpMessageHandlerFactory(handlerList);
                }
                catch (ArgumentException ex)
                {
                    throw FxTrace.Exception.AsError(new ConfigurationErrorsException(ex.Message, ex));
                }
            }
        }