Esempio n. 1
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);
        }
Esempio n. 2
0
        public PSContentNegotiator(HttpRequestMessage request)
        {
            HttpConfiguration  configuration            = request.GetConfiguration();
            IContentNegotiator contentNegotiator        = configuration.Services.GetContentNegotiator();
            IEnumerable <MediaTypeFormatter> formatters = configuration.Formatters;

            ContentNegotiationResult contentNegotiationResult = contentNegotiator.Negotiate(typeof(string), request, formatters);

            _negotiatedMediaType = contentNegotiationResult.MediaType;

            MediaTypeFormatter resultformatter = contentNegotiationResult.Formatter;

            if (resultformatter != null)
            {
                _negotiatedEncoding = _psDefaultContentNegotiator.NegotiateEncoding(request, resultformatter);
            }

            PSMediaTypeFormatter negotiatedMediaTypeFormatter = resultformatter as PSMediaTypeFormatter;

            if (negotiatedMediaTypeFormatter != null)
            {
                _negotiatedPsConverter = negotiatedMediaTypeFormatter.Configuration.Lookup(_negotiatedMediaType);
            }
        }