Exemple #1
0
                public async Task SendRequestAsync(Message message, TimeoutHelper timeoutHelper)
                {
                    _timeoutHelper = timeoutHelper;
                    _factory.ApplyManualAddressing(ref _to, ref _via, message);
                    _httpRequestMessage = await _channel.GetHttpRequestMessageAsync(_to, _via, _timeoutHelper.CancellationToken);

                    Message request = message;

                    try
                    {
                        if (_channel.State != CommunicationState.Opened)
                        {
                            // if we were aborted while getting our request or doing correlation,
                            // we need to abort the web request and bail
                            Cleanup();
                            _channel.ThrowIfDisposedOrNotOpen();
                        }

                        HttpOutput httpOutput = HttpOutput.CreateHttpOutput(_httpRequestMessage, _factory, request, _factory.IsChannelBindingSupportEnabled, _factory.GetHttpClient(), _factory._authenticationScheme, _timeoutHelper);

                        bool success = false;
                        try
                        {
                            _httpResponseMessageTask = await httpOutput.SendAsync(_httpRequestMessage);

                            //this.channelBinding = httpOutput.TakeChannelBinding();
                            await httpOutput.CloseAsync();

                            success = true;
                        }
                        finally
                        {
                            if (!success)
                            {
                                httpOutput.Abort(HttpAbortReason.Aborted);
                            }
                        }
                    }
                    finally
                    {
                        if (!object.ReferenceEquals(request, message))
                        {
                            request.Close();
                        }
                    }
                }
Exemple #2
0
                public async Task SendRequestAsync(Message message, TimeoutHelper timeoutHelper)
                {
                    _timeoutHelper = timeoutHelper;
                    _factory.ApplyManualAddressing(ref _to, ref _via, message);
                    _httpRequestMessage = await _channel.GetHttpRequestMessageAsync(_to, _via, _timeoutHelper);

                    Message request = message;

                    try
                    {
                        if (_channel.State != CommunicationState.Opened)
                        {
                            // if we were aborted while getting our request or doing correlation,
                            // we need to abort the request and bail
                            Cleanup();
                            _channel.ThrowIfDisposedOrNotOpen();
                        }

                        bool suppressEntityBody = PrepareMessageHeaders(request);

                        if (!suppressEntityBody)
                        {
                            _httpRequestMessage.Content = MessageContent.Create(_factory, request, _timeoutHelper);
                        }

                        try
                        {
                            // There is a possibility that a HEAD pre-auth request might fail when the actual request
                            // will succeed. For example, when the web service refuses HEAD requests. We don't want
                            // to fail the actual request because of some subtlety which causes the HEAD request.
                            await SendPreauthenticationHeadRequestIfNeeded();
                        }
                        catch { /* ignored */ }

                        bool success = false;

                        try
                        {
                            _httpResponseMessage = await _httpClient.SendAsync(_httpRequestMessage, HttpCompletionOption.ResponseHeadersRead, _timeoutHelper.CancellationToken);

                            // As we have the response message and no exceptions have been thrown, the request message has completed it's job.
                            // Calling Dispose() on the request message to free up resources in HttpContent, but keeping the object around
                            // as we can still query properties once dispose'd.
                            _httpRequestMessage.Dispose();
                            success = true;
                        }
                        catch (HttpRequestException requestException)
                        {
                            HttpChannelUtilities.ProcessGetResponseWebException(requestException, _httpRequestMessage,
                                                                                _abortReason);
                        }
                        catch (OperationCanceledException)
                        {
                            if (_timeoutHelper.CancellationToken.IsCancellationRequested)
                            {
                                throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new TimeoutException(SR.Format(
                                                                                                                   SR.HttpRequestTimedOut, _httpRequestMessage.RequestUri, _timeoutHelper.OriginalTimeout)));
                            }
                            else
                            {
                                // Cancellation came from somewhere other than timeoutCts and needs to be handled differently.
                                throw;
                            }
                        }
                        finally
                        {
                            if (!success)
                            {
                                Abort(_channel);
                            }
                        }
                    }
                    finally
                    {
                        if (!ReferenceEquals(request, message))
                        {
                            request.Close();
                        }
                    }
                }