Example #1
0
        void SendAsyncCore(HttpRequestMessage request, object userState)
        {
            if (userState != null)
            {
                CancelManager.EnableCancel(request);
                lock (pendingAsync)
                {
                    HttpStageProcessingAsyncResult pend;
                    if (pendingAsync.TryGetValue(userState, out pend))
                    {
                        if (pend == null)
                        {
                            throw new ArgumentException("userState is not unique", "userState");
                        }
                        else
                        {
                            throw new ArgumentException("userState is already being used for " + pend.HttpAsyncState.request, "userState");
                        }
                    }
                    pendingAsync.Add(userState, null);
                }
            }
            var operation = AsyncOperationManager.CreateOperation(userState);
            var state     = new SendAsyncState(this, operation);
            var result    = this.BeginSendCore(request, SendCompletedCallback, state);

            if (userState != null && !result.IsCompleted)
            {
                lock (pendingAsync)
                {
                    Debug.Assert(pendingAsync[userState] == null);
                    pendingAsync[userState] = result;
                }
            }
        }
 public static void EnableCancel(HttpRequestMessage request)
 {
     var manager = request.GetPropertyOrDefault<CancelManager>();
     if (manager == null)
     {
         manager = new CancelManager();
         request.Properties.Add(manager);
     }
 }
        public static void EnableCancel(HttpRequestMessage request)
        {
            var manager = request.GetPropertyOrDefault <CancelManager>();

            if (manager == null)
            {
                manager = new CancelManager();
                request.Properties.Add(manager);
            }
        }
Example #4
0
            public HttpTransportAsyncResult(bool preferSync, HttpRequestMessage request, HttpWebRequestTransportSettings settings, AsyncCallback callback, object state)
                : base(callback, state)
            {
                if (request.Uri == null)
                {
                    throw new ArgumentNullException("request", "request.Uri is null");
                }
                if (!request.Uri.IsAbsoluteUri)
                {
                    throw new UriFormatException("\"" + request.Uri + "\" is not an absolute URI");
                }
                this.stayedSync = true;
                this.settings   = settings;
                this.request    = request;

                CancelManager.AddIfCancelManagerPresent(this.request, this);

                CreateAndPrepareWebRequest(this);

                if (!HttpContent.IsNullOrEmpty(request.Content))
                {
                    var    writer = request.Content;
                    Stream stream;
                    if (!preferSync)
                    {
                        stayedSync = false;
                        Trace(this, "Going async");
                        this.timedOutReason = TimeoutReason.GetRequestStream;
                        var result = webRequest.BeginGetRequestStream(EndGetRequestStreamAndWriteCallback, this);
                        Trace(this, "called BeginGetRequestStream");
                        if (result.CompletedSynchronously)
                        {
                            Trace(this, "BeginGetRequestStream completed synchronously");
                            stream = webRequest.EndGetRequestStream(result);
                        }
                        else
                        {
                            Trace(this, "went async for BeginGetRequestStream");
                            RegisterAbortTimeout(result, TimeoutReason.GetRequestStream);
                            return;
                        }
                    }
                    else
                    {
                        stream = this.webRequest.GetRequestStream();
                    }
                    WriteToRequestStream(this, stream, writer);
                }

                this.timedOutReason = TimeoutReason.GetResponse;
                if (preferSync)
                {
                    PopulateWebResponse(this, null, PopulateWebResponseSyncFunc);
                }
                else
                {
                    var result = this.webRequest.BeginGetResponse(EndGetResponseCallback, this);
                    if (result.CompletedSynchronously)
                    {
                        this.stayedSync = true;
                        PopulateWebResponse(this, result, PopulateWebResponseEndSyncFunc);
                    }
                    else
                    {
                        this.stayedSync = false;
                        RegisterAbortTimeout(result, TimeoutReason.GetResponse);
                    }
                }
            }