Exemplo n.º 1
0
        public HttpMessageInvoker(HttpMessageHandler handler, bool disposeHandler)
        {
            if (NetEventSource.Log.IsEnabled())
            {
                NetEventSource.Enter(NetEventSource.ComponentType.Http, this, ".ctor", handler);
            }

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

            if (HttpEventSource.Log.IsEnabled())
            {
                HttpEventSource.Associate(this, handler);
            }

            _handler        = handler;
            _disposeHandler = disposeHandler;

            if (NetEventSource.Log.IsEnabled())
            {
                NetEventSource.Exit(NetEventSource.ComponentType.Http, this, ".ctor", null);
            }
        }
Exemplo n.º 2
0
 private void SetTaskCompleted(HttpRequestMessage request, CancellationTokenSource cancellationTokenSource,
                               TaskCompletionSource <HttpResponseMessage> tcs, HttpResponseMessage response)
 {
     if (HttpEventSource.Log.IsEnabled())
     {
         HttpEventSource.ClientSendCompleted(this, response, request);
     }
     tcs.TrySetResult(response);
     cancellationTokenSource.Dispose();
 }
Exemplo n.º 3
0
        public StreamContent(Stream content, int bufferSize)
        {
            if (content == null)
            {
                throw new ArgumentNullException(nameof(content));
            }
            if (bufferSize <= 0)
            {
                throw new ArgumentOutOfRangeException(nameof(bufferSize));
            }

            _content = content;
            _bufferSize = bufferSize;
            if (content.CanSeek)
            {
                _start = content.Position;
            }
            if (HttpEventSource.Log.IsEnabled()) HttpEventSource.Associate(this, content);
        }
Exemplo n.º 4
0
 /// <summary>
 /// Event add user
 /// </summary>
 /// <param name="source_"></param>
 /// <param name="type_"></param>
 /// <param name="owner_"></param>
 /// <param name="data_"></param>
 public TriggerReactionEvent(HttpEventSource source_, HttpEventType type_, NetTools.User owner_, Object data_)
     : base(source_, type_, owner_, data_)
 {
 }
Exemplo n.º 5
0
 /// <summary>
 /// Event add user
 /// </summary>
 /// <param name="source_"></param>
 /// <param name="type_"></param>
 /// <param name="owner_"></param>
 /// <param name="data_"></param>
 public AddUserEvent(HttpEventSource source_, HttpEventType type_, NetTools.User owner_, Object data_)
     : base(source_, type_, owner_, data_)
 {
 }
Exemplo n.º 6
0
 /// <summary>
 /// Get available services
 /// </summary>
 /// <param name="source_"></param>
 /// <param name="type_"></param>
 /// <param name="owner_"></param>
 /// <param name="data_"></param>
 public GetAvailableServicesEvent(HttpEventSource source_, HttpEventType type_, NetTools.User owner_, Object data_)
     : base(source_, type_, owner_, data_)
 {
 }
Exemplo n.º 7
0
        private async Task <HttpResponseMessage> FinishSendAsync(
            Task <HttpResponseMessage> sendTask, HttpRequestMessage request, CancellationTokenSource linkedCts, bool bufferResponseContent)
        {
            HttpResponseMessage response = null;

            try
            {
                try
                {
                    // Wait for the send request to complete, getting back the response.
                    response = await sendTask.ConfigureAwait(false);
                }
                finally
                {
                    // When a request completes, dispose the request content so the user doesn't have to. This also
                    // ensures that a HttpContent object is only sent once using HttpClient (similar to HttpRequestMessages
                    // that can also be sent only once).
                    request.Content?.Dispose();
                }

                if (response == null)
                {
                    throw new InvalidOperationException(SR.net_http_handler_noresponse);
                }

                // Buffer the response content if we've been asked to and we have a Content to buffer.
                if (bufferResponseContent && response.Content != null)
                {
                    await response.Content.LoadIntoBufferAsync(_maxResponseContentBufferSize).ConfigureAwait(false);
                }

                if (HttpEventSource.Log.IsEnabled())
                {
                    HttpEventSource.ClientSendCompleted(this, response, request);
                }
                return(response);
            }
            catch (Exception e)
            {
                response?.Dispose();

                // If the cancellation token was canceled, we consider the exception to be caused by the
                // cancellation (e.g. WebException when reading from canceled response stream).
                if (linkedCts.IsCancellationRequested && e is HttpRequestException)
                {
                    LogSendError(request, linkedCts, nameof(SendAsync), null);
                    throw new OperationCanceledException(linkedCts.Token);
                }
                else
                {
                    LogSendError(request, linkedCts, nameof(SendAsync), e);
                    if (NetEventSource.Log.IsEnabled())
                    {
                        NetEventSource.Exception(NetEventSource.ComponentType.Http, this, nameof(SendAsync), e);
                    }
                    throw;
                }
            }
            finally
            {
                linkedCts.Dispose();
            }
        }