Esempio n. 1
0
        /// <summary>
        /// Initializes a new instance of a <see cref="RequestContentHandler"/> with the
        /// given <paramref name="requestContentParameter"/> and <paramref name="formatters"/>.
        /// </summary>
        /// <param name="requestContentParameter">The <see cref="HttpParameter"/> for the content of the request.</param>
        /// <param name="formatters">The collection of <see cref="MediaTypeFormatter"/> instances to use for deserializing the request content.</param>
        public RequestContentHandler(HttpParameter requestContentParameter, IEnumerable <MediaTypeFormatter> formatters)
        {
            if (requestContentParameter == null)
            {
                throw Fx.Exception.ArgumentNull("requestContentParameter");
            }

            if (formatters == null)
            {
                throw Fx.Exception.ArgumentNull("formatters");
            }

            Type paramType = requestContentParameter.Type;

            paramType = HttpTypeHelper.GetHttpRequestOrContentInnerTypeOrNull(paramType) ?? paramType;

            if (HttpTypeHelper.IsHttpResponse(paramType))
            {
                throw Fx.Exception.AsError(
                          new InvalidOperationException(
                              SR.InvalidParameterForContentHandler(
                                  HttpParameter.HttpParameterType.Name,
                                  requestContentParameter.Name,
                                  requestContentParameter.Type.Name,
                                  requestContentHandlerType.Name)));
            }
            else if (HttpTypeHelper.IsHttpRequestOrContent(paramType))
            {
                this.outputParameter         = new HttpParameter(requestContentParameter.Name, HttpParameter.RequestMessage.Type);
                this.requestMessageConverter = simpleHttpRequestMessageConverter;
            }
            else
            {
                Type outputParameterType = HttpTypeHelper.MakeHttpRequestMessageOf(paramType);
                this.outputParameter = new HttpParameter(requestContentParameter.Name, outputParameterType);

                Type            closedConverterType = httpRequestMessageConverterGenericType.MakeGenericType(new Type[] { paramType });
                ConstructorInfo constructor         = closedConverterType.GetConstructor(Type.EmptyTypes);
                this.requestMessageConverter = constructor.Invoke(null) as HttpRequestMessageConverter;
            }

            this.Formatters = new MediaTypeFormatterCollection(formatters);
        }
Esempio n. 2
0
        /// <summary>
        /// Initializes a new instance of a <see cref="RequestContentHandler"/> with the
        /// given <paramref name="requestContentParameter"/> and <paramref name="formatters"/>.
        /// </summary>
        /// <param name="requestContentParameter">The <see cref="HttpParameter"/> for the content of the request.</param>
        /// <param name="formatters">The collection of <see cref="MediaTypeFormatter"/> instances to use for deserializing the request content.</param>
        public RequestContentHandler(HttpParameter requestContentParameter, IEnumerable<MediaTypeFormatter> formatters)
        {
            if (requestContentParameter == null)
            {
                throw Fx.Exception.ArgumentNull("requestContentParameter");
            }

            if (formatters == null)
            {
                throw Fx.Exception.ArgumentNull("formatters");
            }

            Type paramType = requestContentParameter.Type;
            paramType = HttpTypeHelper.GetHttpRequestOrContentInnerTypeOrNull(paramType) ?? paramType;

            if (HttpTypeHelper.IsHttpResponse(paramType))
            {
                throw Fx.Exception.AsError(
                    new InvalidOperationException(
                        SR.InvalidParameterForContentHandler(
                            HttpParameter.HttpParameterType.Name,
                            requestContentParameter.Name,
                            requestContentParameter.Type.Name,
                            requestContentHandlerType.Name)));
            }
            else if (HttpTypeHelper.IsHttpRequestOrContent(paramType))
            {
                this.outputParameter = new HttpParameter(requestContentParameter.Name, HttpParameter.RequestMessage.Type);
                this.requestMessageConverter = simpleHttpRequestMessageConverter;
            }
            else
            {
                Type outputParameterType = HttpTypeHelper.MakeHttpRequestMessageOf(paramType);
                this.outputParameter = new HttpParameter(requestContentParameter.Name, outputParameterType);

                Type closedConverterType = httpRequestMessageConverterGenericType.MakeGenericType(new Type[] { paramType });
                ConstructorInfo constructor = closedConverterType.GetConstructor(Type.EmptyTypes);
                this.requestMessageConverter = constructor.Invoke(null) as HttpRequestMessageConverter;
            }

            this.Formatters = new MediaTypeFormatterCollection(formatters);
        }