private void HandleOperationException(InvalidOperationException e, HttpWebResponse response) { Func <Stream> getResponseStream = null; Descriptor descriptor = base.ChangedEntries[base.entryIndex]; Dictionary <string, string> headers = null; HttpStatusCode internalServerError = HttpStatusCode.InternalServerError; Version parsedResponseVersion = null; if (response != null) { headers = WebUtil.WrapResponseHeaders(response); base.HandleOperationResponseHeaders(response.StatusCode, headers); if (getResponseStream == null) { getResponseStream = () => WebUtil.GetResponseStream(response, (DataServiceContext)this.Source); } e = BaseSaveResult.HandleResponse(base.RequestInfo, response.StatusCode, response.Headers["DataServiceVersion"], getResponseStream, false, out parsedResponseVersion); internalServerError = response.StatusCode; } else { headers = new Dictionary <string, string>(StringComparer.Ordinal); headers.Add("Content-Type", "text/plain"); if (e.GetType() != typeof(DataServiceClientException)) { e = new DataServiceClientException(e.Message, e); } } this.cachedResponses.Add(new CachedResponse(descriptor, headers, internalServerError, parsedResponseVersion, null, e)); base.perRequest = null; this.CheckContinueOnError(); }
internal void BatchRequest() { ODataRequestMessageWrapper request = this.GenerateBatchRequest(); if (request != null) { request.SetContentLengthHeader(); MemoryStream cachedRequestStream = request.CachedRequestStream; DataServiceContext source = (DataServiceContext)base.Source; using (Stream stream2 = WebUtil.GetRequestStream(request, source)) { byte[] buffer = cachedRequestStream.GetBuffer(); int position = (int)cachedRequestStream.Position; int count = ((int)cachedRequestStream.Length) - position; stream2.Write(buffer, position, count); } try { base.batchResponse = WebUtil.GetResponse(request, source, false); } catch (WebException exception) { WebUtil.GetHttpWebResponse(exception, ref this.batchResponse); throw; } finally { if (base.batchResponse != null) { this.responseStream = WebUtil.GetResponseStream(base.batchResponse, source); } } } }
protected override void AsyncEndGetResponse(IAsyncResult asyncResult) { BaseAsyncResult.AsyncStateBag asyncState = asyncResult.AsyncState as BaseAsyncResult.AsyncStateBag; BaseAsyncResult.PerRequest request = (asyncState == null) ? null : asyncState.PerRequest; DataServiceContext context = (asyncState == null) ? null : asyncState.Context; try { this.CompleteCheck(request, InternalError.InvalidEndGetResponseCompleted); request.SetRequestCompletedSynchronously(asyncResult.CompletedSynchronously); BaseAsyncResult.EqualRefCheck(base.perRequest, request, InternalError.InvalidEndGetResponse); ODataRequestMessageWrapper wrapper = Util.NullCheck <ODataRequestMessageWrapper>(request.Request, InternalError.InvalidEndGetResponseRequest); HttpWebResponse response = null; response = WebUtil.EndGetResponse(wrapper, asyncResult, context); request.HttpWebResponse = Util.NullCheck <HttpWebResponse>(response, InternalError.InvalidEndGetResponseResponse); if (!this.IsBatch) { this.HandleOperationResponse(response); this.HandleOperationResponseHeaders(response.StatusCode, WebUtil.WrapResponseHeaders(response)); } Stream responseStream = WebUtil.GetResponseStream(response, context); request.ResponseStream = responseStream; if ((responseStream != null) && responseStream.CanRead) { if (this.buildBatchBuffer == null) { this.buildBatchBuffer = new byte[0x1f40]; } do { asyncResult = BaseAsyncResult.InvokeAsync(new BaseAsyncResult.AsyncAction(responseStream.BeginRead), this.buildBatchBuffer, 0, this.buildBatchBuffer.Length, new AsyncCallback(this.AsyncEndRead), new AsyncReadState(request)); request.SetRequestCompletedSynchronously(asyncResult.CompletedSynchronously); }while (((asyncResult.CompletedSynchronously && !request.RequestCompleted) && !base.IsCompletedInternally) && responseStream.CanRead); } else { request.SetComplete(); if (!base.IsCompletedInternally && !request.RequestCompletedSynchronously) { this.FinishCurrentChange(request); } } } catch (Exception exception) { if (base.HandleFailure(request, exception)) { throw; } } finally { this.HandleCompleted(request); } }
protected override void CompletedRequest() { Func <Stream> getResponseStream = null; if (this.response != null) { InvalidOperationException e = null; if (!WebUtil.SuccessStatusCode(this.response.StatusCode)) { getResponseStream = () => WebUtil.GetResponseStream(this.response, (DataServiceContext)base.Source); e = BaseSaveResult.GetResponseText(getResponseStream, this.response.StatusCode); } if (e != null) { this.response.Close(); base.HandleFailure(e); } } }
private void HandleOperationResponseData(HttpWebResponse response) { using (Stream stream = WebUtil.GetResponseStream(response, (DataServiceContext)base.Source)) { if (stream != null) { using (MemoryStream stream2 = new MemoryStream()) { if (WebUtil.CopyStream(stream, stream2, ref this.buildBatchBuffer) != 0L) { stream2.Position = 0L; this.HandleOperationResponseData(response, stream2); } else { this.HandleOperationResponseData(response, null); } } } } }
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); }
protected override void AsyncEndGetResponse(IAsyncResult asyncResult) { BaseAsyncResult.AsyncStateBag asyncState = asyncResult.AsyncState as BaseAsyncResult.AsyncStateBag; BaseAsyncResult.PerRequest request = (asyncState == null) ? null : asyncState.PerRequest; DataServiceContext context = (asyncState == null) ? null : asyncState.Context; try { if (base.IsAborted) { if (request != null) { request.SetComplete(); } base.SetCompleted(); } else { this.CompleteCheck(request, InternalError.InvalidEndGetResponseCompleted); request.SetRequestCompletedSynchronously(asyncResult.CompletedSynchronously); base.SetCompletedSynchronously(asyncResult.CompletedSynchronously); HttpWebResponse response = WebUtil.EndGetResponse(Util.NullCheck <ODataRequestMessageWrapper>(request.Request, InternalError.InvalidEndGetResponseRequest), asyncResult, context); request.HttpWebResponse = Util.NullCheck <HttpWebResponse>(response, InternalError.InvalidEndGetResponseResponse); this.SetHttpWebResponse(request.HttpWebResponse); Stream responseStream = null; if (HttpStatusCode.NoContent != response.StatusCode) { responseStream = WebUtil.GetResponseStream(response, context); request.ResponseStream = responseStream; } if ((responseStream != null) && responseStream.CanRead) { if (this.outputResponseStream == null) { this.outputResponseStream = Util.NullCheck <Stream>(this.GetAsyncResponseStreamCopy(), InternalError.InvalidAsyncResponseStreamCopy); } if (this.asyncStreamCopyBuffer == null) { this.asyncStreamCopyBuffer = Util.NullCheck <byte[]>(this.GetAsyncResponseStreamCopyBuffer(), InternalError.InvalidAsyncResponseStreamCopyBuffer); } this.ReadResponseStream(asyncState); } else { request.SetComplete(); base.SetCompleted(); } } } catch (Exception exception) { if (base.HandleFailure(exception)) { throw; } } finally { this.HandleCompleted(request); } }
internal void ExecuteQuery(DataServiceContext context) { try { if ((this.requestContentStream != null) && (this.requestContentStream.Stream != null)) { if (this.requestContentStream.IsKnownMemoryStream) { this.Request.SetContentLengthHeader(); } using (Stream stream = WebUtil.GetRequestStream(this.Request, context)) { int num; byte[] buffer = new byte[0x10000]; do { num = this.requestContentStream.Stream.Read(buffer, 0, buffer.Length); if (num > 0) { stream.Write(buffer, 0, num); } }while (num > 0); } } HttpWebResponse response = null; response = WebUtil.GetResponse(this.Request, context, true); this.SetHttpWebResponse(Util.NullCheck <HttpWebResponse>(response, InternalError.InvalidGetResponse)); if (HttpStatusCode.NoContent != this.StatusCode) { using (Stream stream2 = WebUtil.GetResponseStream(this.httpWebResponse, context)) { if (stream2 != null) { Stream asyncResponseStreamCopy = this.GetAsyncResponseStreamCopy(); this.outputResponseStream = asyncResponseStreamCopy; byte[] asyncResponseStreamCopyBuffer = this.GetAsyncResponseStreamCopyBuffer(); long num2 = WebUtil.CopyStream(stream2, asyncResponseStreamCopy, ref asyncResponseStreamCopyBuffer); if (this.responseStreamOwner) { if (0L == num2) { this.outputResponseStream = null; } else if (asyncResponseStreamCopy.Position < asyncResponseStreamCopy.Length) { ((MemoryStream)asyncResponseStreamCopy).SetLength(asyncResponseStreamCopy.Position); } } this.PutAsyncResponseStreamCopyBuffer(asyncResponseStreamCopyBuffer); } } } } catch (Exception exception) { base.HandleFailure(exception); throw; } finally { base.SetCompleted(); this.CompletedRequest(); } if (base.Failure != null) { throw base.Failure; } }