Example #1
0
        /// <summary>
        /// common handler for EndExecuteBatch &amp; EndSaveChanges
        /// </summary>
        /// <typeparam name="T">derived type of the AsyncResult</typeparam>
        /// <param name="source">source object of async request</param>
        /// <param name="method">async method name on source object</param>
        /// <param name="asyncResult">the asyncResult being ended</param>
        /// <returns>data service response for batch</returns>
        internal static T EndExecute <T>(object source, string method, IAsyncResult asyncResult) where T : BaseAsyncResult
        {
            Util.CheckArgumentNull(asyncResult, "asyncResult");

            T result = (asyncResult as T);

            if ((null == result) || (source != result.Source) || (result.Method != method))
            {
                throw Error.Argument(Strings.Context_DidNotOriginateAsync, "asyncResult");
            }

            Debug.Assert((result.CompletedSynchronously && result.IsCompleted) || !result.CompletedSynchronously, "CompletedSynchronously && !IsCompleted");

            if (!result.IsCompleted)
            {   // if the user doesn't want to wait forever, they should explictly wait on the handle with a timeout
                result.AsyncWaitHandle.WaitOne();

                Debug.Assert(result.IsCompleted, "not completed after waiting");
            }

            // Prevent EndExecute from being called more than once.
            if (System.Threading.Interlocked.Exchange(ref result.done, 1) != 0)
            {
                throw Error.Argument(Strings.Context_AsyncAlreadyDone, "asyncResult");
            }

            // Dispose the wait handle.
            if (null != result.asyncWait)
            {
                System.Threading.Interlocked.CompareExchange(ref result.asyncWaitDisposeLock, new object(), null);
                lock (result.asyncWaitDisposeLock)
                {
                    result.asyncWaitDisposed = true;
                    Util.Dispose(result.asyncWait);
                }
            }

            if (result.IsAborted)
            {
                throw Error.InvalidOperation(Strings.Context_OperationCanceled);
            }

            if (null != result.Failure)
            {
                if (Util.IsKnownClientExcption(result.Failure))
                {
                    throw result.Failure;
                }

                throw Error.InvalidOperation(Strings.DataServiceException_GeneralError, result.Failure);
            }

            return(result);
        }
Example #2
0
        internal static T EndExecute <T>(object source, string method, IAsyncResult asyncResult) where T : BaseAsyncResult
        {
            Util.CheckArgumentNull <IAsyncResult>(asyncResult, "asyncResult");
            T local = asyncResult as T;

            if (((local == null) || (source != local.Source)) || (local.Method != method))
            {
                throw Error.Argument(Strings.Context_DidNotOriginateAsync, "asyncResult");
            }
            if (!local.IsCompleted)
            {
                local.AsyncWaitHandle.WaitOne();
            }
            if (Interlocked.Exchange(ref local.done, 1) != 0)
            {
                throw Error.Argument(Strings.Context_AsyncAlreadyDone, "asyncResult");
            }
            if (local.asyncWait != null)
            {
                Interlocked.CompareExchange(ref local.asyncWaitDisposeLock, new object(), null);
                lock (local.asyncWaitDisposeLock)
                {
                    local.asyncWaitDisposed = true;
                    Util.Dispose <ManualResetEvent>(local.asyncWait);
                }
            }
            if (local.IsAborted)
            {
                throw Error.InvalidOperation(Strings.Context_OperationCanceled);
            }
            if (local.Failure == null)
            {
                return(local);
            }
            if (Util.IsKnownClientExcption(local.Failure))
            {
                throw local.Failure;
            }
            throw Error.InvalidOperation(Strings.DataServiceException_GeneralError, local.Failure);
        }
        /// <summary>cleanup work to do once the request has completed</summary>
        protected override void CompletedRequest()
        {
            Util.Dispose(ref this.asyncResponseStream);
            Util.Dispose(ref this.requestStream);
            Util.Dispose(ref this.requestStreamContent);

            byte[] buffer = this.asyncStreamCopyBuffer;
            this.asyncStreamCopyBuffer = null;
#if StreamContainsBuffer
            if (!this.responseStreamIsCopyBuffer)
#endif
            if ((null != buffer) && !this.usingBuffer)
            {
                this.PutAsyncResponseStreamCopyBuffer(buffer);
            }

            if (this.responseStreamOwner)
            {
                if (null != this.responseStream)
                {
                    this.responseStream.Position = 0;
                }
            }

            Debug.Assert(null != this.httpWebResponse || null != this.Failure, "should have response or exception");
            if (null != this.httpWebResponse)
            {
                // we've cached off what we need, headers still accessible after close
                this.httpWebResponse.Close();

                Exception ex = DataServiceContext.HandleResponse(this.StatusCode, this.httpWebResponse.Headers[XmlConstants.HttpDataServiceVersion], this.GetResponseStream, false);
                if (null != ex)
                {
                    this.HandleFailure(ex);
                }
            }
        }
Example #4
0
 public void Dispose()
 {
     Util.Dispose(ref this.response);
 }
Example #5
0
        private DataServiceResponse HandleBatchResponse()
        {
            Func <Stream>       func2             = null;
            Func <Stream>       getResponseStream = null;
            DataServiceResponse response3;
            bool flag = true;

            try
            {
                Version          version;
                ODataBatchReader reader;
                if ((base.batchResponse == null) || (base.batchResponse.StatusCode == HttpStatusCode.NoContent))
                {
                    throw System.Data.Services.Client.Error.InvalidOperation(System.Data.Services.Client.Strings.Batch_ExpectedResponse(1));
                }
                if (func2 == null)
                {
                    func2 = () => this.ResponseStream;
                }
                Func <Stream> func = func2;
                BaseSaveResult.HandleResponse(base.RequestInfo, base.batchResponse.StatusCode, base.batchResponse.Headers["DataServiceVersion"], func, true, out version);
                if (this.ResponseStream == null)
                {
                    System.Data.Services.Client.Error.ThrowBatchExpectedResponse(InternalError.NullResponseStream);
                }
                HttpWebResponseMessage     responseMessage = new HttpWebResponseMessage(base.batchResponse, func);
                ODataMessageReaderSettings settings        = WebUtil.CreateODataMessageReaderSettings(base.RequestInfo.GetDeserializationInfo(null), null, false);
                this.batchMessageReader = new ODataMessageReader(responseMessage, settings);
                try
                {
                    reader = this.batchMessageReader.CreateODataBatchReader();
                }
                catch (Exception responseText)
                {
                    string   str;
                    Encoding encoding;
                    HttpProcessUtility.ReadContentType(base.batchResponse.ContentType, out str, out encoding);
                    if (string.Equals("text/plain", str))
                    {
                        if (getResponseStream == null)
                        {
                            getResponseStream = () => WebUtil.GetResponseStream(base.batchResponse, (DataServiceContext)base.Source);
                        }
                        responseText = BaseSaveResult.GetResponseText(getResponseStream, base.batchResponse.StatusCode);
                    }
                    throw System.Data.Services.Client.Error.InvalidOperation(System.Data.Services.Client.Strings.Batch_ExpectedContentType(base.batchResponse.ContentType), responseText);
                }
                DataServiceResponse response = this.HandleBatchResponseInternal(reader);
                flag      = false;
                response3 = response;
            }
            catch (DataServiceRequestException)
            {
                throw;
            }
            catch (InvalidOperationException exception3)
            {
                Dictionary <string, string> headers = WebUtil.WrapResponseHeaders(base.batchResponse);
                int statusCode = (base.batchResponse == null) ? 500 : ((int)base.batchResponse.StatusCode);
                DataServiceResponse response2 = new DataServiceResponse(headers, statusCode, null, this.IsBatch);
                throw new DataServiceRequestException(System.Data.Services.Client.Strings.DataServiceException_GeneralError, exception3, response2);
            }
            finally
            {
                if (flag)
                {
                    Util.Dispose <ODataMessageReader>(ref this.batchMessageReader);
                }
            }
            return(response3);
        }
 public void Dispose()
 {
     Util.Dispose <HttpWebResponse>(ref this.response);
 }