Exemplo n.º 1
0
        /// <summary>
        /// Initializes a new instance of the <see cref="PSMediaTypeFormatter"/> class.
        /// </summary>
        /// <param name="psConfiguration">PSConfiguration</param>
        public PSMediaTypeFormatter(PSConfiguration psConfiguration = null)
        {
            _psConfiguration = psConfiguration ?? new PSConfiguration();

            foreach (var converter in _psConfiguration.SupportedConverters)
            {
                foreach (var mediaType in converter.MediaTypes)
                {
                    if (!SupportedMediaTypes.Any(t => t.MediaType.Equals(mediaType.MediaType)))
                    {
                        SupportedMediaTypes.Add(mediaType);
                    }

                    if (!string.IsNullOrWhiteSpace(converter.UriPathExtension))
                    {
                        this.AddUriPathExtensionMapping(converter.UriPathExtension, mediaType);
                    }
                }
            }

            // Solving the 415 issue for GET request without Content-Type and Content-Length: 0
            SupportedMediaTypes.Add(new MediaTypeHeaderValue("application/octet-stream"));
        }
Exemplo n.º 2
0
        /// <summary>
        /// Registers necessary MediaTypeFormatters to HttpConfiguration for running a PS-WebAPI endpoint.
        /// </summary>
        /// <param name="config">The <see cref="HttpConfiguration"/>.  This is an extension method to HttpConfiguration, when you use instance method syntax to call this method, omit this parameter.</param>
        /// <param name="psConfiguration">The configuration for initializing a instance PSMediaTypeFormatter, which contains all currently supported PSConverterRegistry add-ins.</param>
        /// <param name="mediaTypes">A list of supported request Content-Types for redirecting StandardInput.</param>
        public static PSConfiguration RegisterPsWebApi(this HttpConfiguration config, PSConfiguration psConfiguration = null, IEnumerable <MediaTypeHeaderValue> mediaTypes = null)
        {
            if (config.Properties.ContainsKey(_RegisteredPropertyKey))
            {
                throw new InvalidOperationException("Registered PSWebApi Repeatedly");
            }

            PSMediaTypeFormatter psMediaTypeFormatter = new PSMediaTypeFormatter(psConfiguration);

            config.Formatters.Insert(0, psMediaTypeFormatter);
            config.Formatters.Insert(1, new CmdMediaTypeFormatter(mediaTypes));

            config.Properties.TryAdd(_RegisteredPropertyKey, true);

            return(psMediaTypeFormatter.Configuration);
        }