private Task <HttpResponseMessage> SendAsync(IMockedRequest handler, HttpRequestMessage request, CancellationToken cancellationToken)
        {
            Interlocked.Increment(ref outstandingRequests);

            IncrementMatchCount(handler);

            if (!AutoFlush)
            {
                flusher = new TaskCompletionSource <object>();
                pendingFlushers.Enqueue(flusher);
            }

            return(flusher.Task.ContinueWith(_ =>
            {
                Interlocked.Decrement(ref outstandingRequests);

                cancellationToken.ThrowIfCancellationRequested();

                var completionSource = new TaskCompletionSource <HttpResponseMessage>();

                cancellationToken.Register(() => completionSource.TrySetCanceled());

                handler.SendAsync(request, cancellationToken)
                .ContinueWith(resp =>
                {
                    resp.Result.RequestMessage = request;

                    if (resp.IsFaulted)
                    {
                        completionSource.TrySetException(resp.Exception);
                    }
                    else if (resp.IsCanceled)
                    {
                        completionSource.TrySetCanceled();
                    }
                    else
                    {
                        completionSource.TrySetResult(resp.Result);
                    }
                });

                return completionSource.Task;
            }).Unwrap());
        }
Beispiel #2
0
        private Task <HttpResponseMessage> SendAsync(IMockedRequest handler, HttpRequestMessage request, CancellationToken cancellationToken)
        {
            Interlocked.Increment(ref outstandingRequests);

            if (!AutoFlush)
            {
                flusher = new TaskCompletionSource <object>();
                pendingFlushers.Enqueue(flusher);
            }

            return(flusher.Task.ContinueWith(_ =>
            {
                Interlocked.Decrement(ref outstandingRequests);

                return handler.SendAsync(request, cancellationToken)
                .ContinueWith(resp =>
                {
                    resp.Result.RequestMessage = request;
                    return resp.Result;
                });
            }).Unwrap());
        }
        private Task<HttpResponseMessage> SendAsync(IMockedRequest handler, HttpRequestMessage request, CancellationToken cancellationToken)
        {
            Interlocked.Increment(ref outstandingRequests);

            if (!AutoFlush)
            {
                flusher = new TaskCompletionSource<object>();
                pendingFlushers.Enqueue(flusher);
            }

            return flusher.Task.ContinueWith(_ =>
                {
                    Interlocked.Decrement(ref outstandingRequests);

                    cancellationToken.ThrowIfCancellationRequested();

                    var completionSource = new TaskCompletionSource<HttpResponseMessage>();

                    cancellationToken.Register(() => completionSource.TrySetCanceled());

                    handler.SendAsync(request, cancellationToken)
                        .ContinueWith(resp =>
                        {
                            resp.Result.RequestMessage = request;

                            if (resp.IsFaulted)
                            {
                                completionSource.TrySetException(resp.Exception);
                            }
                            else if (resp.IsCanceled)
                            {
                                completionSource.TrySetCanceled();
                            }
                            else
                            {
                                completionSource.TrySetResult(resp.Result);
                            }
                        });

                    return completionSource.Task;
                }).Unwrap();
        }
        private Task<HttpResponseMessage> SendAsync(IMockedRequest handler, HttpRequestMessage request, CancellationToken cancellationToken)
        {
            Interlocked.Increment(ref outstandingRequests);

            if (!AutoFlush)
            {
                flusher = new TaskCompletionSource<object>();
                pendingFlushers.Enqueue(flusher);
            }

            return flusher.Task.ContinueWith(_ =>
                {
                    Interlocked.Decrement(ref outstandingRequests);

                    return handler.SendAsync(request, cancellationToken)
                        .ContinueWith(resp =>
                        {
                            resp.Result.RequestMessage = request;
                            return resp.Result;
                        });
                }).Unwrap();
        }