static readonly TimeSpan TimeoutDefault = TimeSpan.FromSeconds (100);
Uri base_address;
- CancellationTokenSource cancellation_token;
+ CancellationTokenSource cts;
bool disposed;
HttpRequestHeaders headers;
long buffer_size;
{
buffer_size = int.MaxValue;
timeout = TimeoutDefault;
+ cts = new CancellationTokenSource ();
}
public Uri BaseAddress {
public void CancelPendingRequests ()
{
- if (cancellation_token != null)
- cancellation_token.Cancel ();
-
- cancellation_token = new CancellationTokenSource ();
+ // Cancel only any already running requests not any new request after this cancellation
+ using (var c = Interlocked.Exchange (ref cts, new CancellationTokenSource ()))
+ c.Cancel ();
}
protected override void Dispose (bool disposing)
if (disposing && !disposed) {
disposed = true;
- if (cancellation_token != null)
- cancellation_token.Dispose ();
+ cts.Dispose ();
}
base.Dispose (disposing);
public Task<HttpResponseMessage> GetAsync (string requestUri, CancellationToken cancellationToken)
{
- return SendAsync (new HttpRequestMessage (HttpMethod.Get, requestUri));
+ return SendAsync (new HttpRequestMessage (HttpMethod.Get, requestUri), cancellationToken);
}
public Task<HttpResponseMessage> GetAsync (string requestUri, HttpCompletionOption completionOption)
async Task<HttpResponseMessage> SendAsyncWorker (HttpRequestMessage request, HttpCompletionOption completionOption, CancellationToken cancellationToken)
{
- try {
- if (cancellation_token == null)
- cancellation_token = new CancellationTokenSource ();
+ using (var lcts = CancellationTokenSource.CreateLinkedTokenSource (cts.Token, cancellationToken)) {
+ lcts.CancelAfter (timeout);
- using (var cts = CancellationTokenSource.CreateLinkedTokenSource (cancellation_token.Token, cancellationToken)) {
- cts.CancelAfter (timeout);
-
- var task = base.SendAsync (request, cts.Token);
- if (task == null)
- throw new InvalidOperationException ("Handler failed to return a value");
-
- var response = await task.ConfigureAwait (false);
- if (response == null)
- throw new InvalidOperationException ("Handler failed to return a response");
-
- //
- // Read the content when default HttpCompletionOption.ResponseContentRead is set
- //
- if (response.Content != null && (completionOption & HttpCompletionOption.ResponseHeadersRead) == 0) {
- await response.Content.LoadIntoBufferAsync (MaxResponseContentBufferSize).ConfigureAwait (false);
- }
+ var task = base.SendAsync (request, lcts.Token);
+ if (task == null)
+ throw new InvalidOperationException ("Handler failed to return a value");
- return response;
+ var response = await task.ConfigureAwait (false);
+ if (response == null)
+ throw new InvalidOperationException ("Handler failed to return a response");
+
+ //
+ // Read the content when default HttpCompletionOption.ResponseContentRead is set
+ //
+ if (response.Content != null && (completionOption & HttpCompletionOption.ResponseHeadersRead) == 0) {
+ await response.Content.LoadIntoBufferAsync (MaxResponseContentBufferSize).ConfigureAwait (false);
}
- } finally {
- cancellation_token.Dispose ();
- cancellation_token = null;
+
+ return response;
}
}