/// <summary>Adds a component to the OWIN pipeline for running a Web API endpoint.</summary>
        /// <param name="builder">The application builder.</param>
        /// <param name="configuration">The <see cref="HttpConfiguration"/> used to configure the endpoint.</param>
        /// <param name="bufferRequests">
        /// The default WebAPI formatters perform synchronous I/O when reading to the request.
        /// This has poor performance characteristics with ASP.NET Core apps and may result
        /// in application deadlocks. This option forces ASP.NET Core to buffer requests preventing this scenario.
        /// </param>
        /// <returns>The application builder.</returns>
        public static IApplicationBuilder UseWebApi(this IApplicationBuilder builder, HttpConfiguration configuration, bool bufferRequests = true)
        {
            if (builder == null)
            {
                throw new ArgumentNullException("builder");
            }

            if (configuration == null)
            {
                throw new ArgumentNullException("configuration");
            }

            HttpServer server = new HttpServer(configuration);

            try
            {
                HttpMessageHandlerOptions options = CreateOptions(builder, server, configuration, bufferRequests);
                return(UseMessageHandler(builder, options));
            }
            catch
            {
                server.Dispose();
                throw;
            }
        }
        /// <summary>Adds a component to the OWIN pipeline for running a Web API endpoint.</summary>
        /// <param name="builder">The application builder.</param>
        /// <param name="httpServer">The http server.</param>
        /// <param name="bufferRequests">
        /// The default WebAPI formatters perform synchronous I/O when reading to the request.
        /// This has poor performance characteristics with ASP.NET Core apps and may result
        /// in application deadlocks. This option forces ASP.NET Core to buffer requests preventing this scenario.
        /// </param>
        /// <returns>The application builder.</returns>
        public static IApplicationBuilder UseWebApi(this IApplicationBuilder builder, HttpServer httpServer, bool bufferRequests = true)
        {
            if (builder == null)
            {
                throw new ArgumentNullException("builder");
            }

            if (httpServer == null)
            {
                throw new ArgumentNullException("httpServer");
            }

            HttpConfiguration configuration = httpServer.Configuration;

            Contract.Assert(configuration != null);

            HttpMessageHandlerOptions options = CreateOptions(builder, httpServer, configuration, bufferRequests);

            return(UseMessageHandler(builder, options));
        }
        public HttpMessageHandlerAdapter(RequestDelegate next, HttpMessageHandlerOptions options, IHostApplicationLifetime applicationLifetime)
        {
            if (options == null)
            {
                throw new ArgumentNullException("options");
            }

            _next                 = next;
            _messageHandler       = options.MessageHandler ?? throw new ArgumentNullException(nameof(options.MessageHandler));
            _messageInvoker       = new HttpMessageInvoker(_messageHandler);
            _bufferPolicySelector = options.BufferPolicySelector ?? throw new ArgumentNullException(nameof(options.BufferPolicySelector));

            _exceptionLogger  = options.ExceptionLogger;
            _exceptionHandler = options.ExceptionHandler;

            if (applicationLifetime.ApplicationStopping.CanBeCanceled)
            {
                applicationLifetime.ApplicationStopping.Register(OnAppDisposing);
            }

            _canceledTask.SetCanceled();
        }
        private static IApplicationBuilder UseMessageHandler(this IApplicationBuilder builder, HttpMessageHandlerOptions options)
        {
            Contract.Assert(builder != null);
            Contract.Assert(options != null);

            return(builder.UseMiddleware <HttpMessageHandlerAdapter>(options));
        }