Пример #1
0
        /// <summary>
        /// copy the response data
        /// </summary>
        /// <param name="response">response object</param>
        private void HandleOperationResponseData(IODataResponseMessage response)
        {
            Debug.Assert(response != null, "response != null");

            using (Stream stream = response.GetStream())
            {
                if (stream != null)
                {
                    // we need to check for whether the incoming stream was data or not. Hence we need to copy it to a temporary memory stream
                    using (MemoryStream memoryStream = new MemoryStream())
                    {
                        if (WebUtil.CopyStream(stream, memoryStream, ref this.buildBatchBuffer) != 0)
                        {
                            // set the memory stream position to zero again.
                            memoryStream.Position = 0;
                            this.HandleOperationResponseData(response, memoryStream);
                        }
                        else
                        {
                            this.HandleOperationResponseData(response, null);
                        }
                    }
                }
            }
        }
Пример #2
0
 /// <summary>
 /// Write empty response and set the HttpStatusCode to 204 NoContent
 /// </summary>
 /// <param name="responseMessage"></param>
 public static void WriteEmptyResponse(IODataResponseMessage responseMessage, HttpStatusCode statusCode = HttpStatusCode.NoContent)
 {
     responseMessage.SetStatusCode(statusCode);
     responseMessage.SetHeader("OData-Version", "4.0");
     if (!(responseMessage is ODataBatchOperationResponseMessage))
     {
         using (responseMessage.GetStream())
         {
         }
     }
 }
Пример #3
0
        /// <summary>
        /// Write empty response and set the HttpStatusCode to 202 Accepted
        /// </summary>
        /// <param name="responseMessage"></param>
        /// <param name="asyncToken"></param>
        public static void WriteAsyncPendingResponse(IODataResponseMessage responseMessage, string asyncToken)
        {
            responseMessage.SetStatusCode(HttpStatusCode.Accepted);
            responseMessage.SetHeader(ServiceConstants.HttpHeaders.DataServiceVersion, "4.0");
            Uri location = new Uri(ServiceConstants.ServiceBaseUri, string.Format("{0}?{1}={2}", ServiceConstants.ServicePath_Async, ServiceConstants.QueryOption_AsyncToken, asyncToken));

            responseMessage.SetHeader(ServiceConstants.HttpHeaders.Location, location.OriginalString);

            if (!(responseMessage is ODataBatchOperationResponseMessage))
            {
                using (responseMessage.GetStream())
                {
                }
            }
        }
Пример #4
0
        private Exception ProcessCurrentOperationResponse(ODataBatchReader batchReader)
        {
            MemoryStream          stream2;
            Version               version;
            IODataResponseMessage message = batchReader.CreateOperationResponseMessage();
            Stream input = message.GetStream();

            if (input == null)
            {
                System.Data.Services.Client.Error.ThrowBatchExpectedResponse(InternalError.NullResponseStream);
            }
            try
            {
                stream2 = new MemoryStream();
                WebUtil.CopyStream(input, stream2, ref this.streamCopyBuffer);
                stream2.Position = 0L;
            }
            finally
            {
                input.Dispose();
            }
            this.currentOperationResponse = new CurrentOperationResponse((HttpStatusCode)message.StatusCode, message.Headers, stream2);
            return(BaseSaveResult.HandleResponse(base.RequestInfo, this.currentOperationResponse.StatusCode, this.currentOperationResponse.GetHeader("DataServiceVersion"), () => this.currentOperationResponse.ContentStream, false, out version));
        }
Пример #5
0
        protected override void AsyncEndGetResponse(IAsyncResult asyncResult)
        {
            Debug.Assert(asyncResult != null && asyncResult.IsCompleted, "asyncResult.IsCompleted");
            AsyncStateBag asyncStateBag = asyncResult.AsyncState as AsyncStateBag;

            PerRequest pereq = asyncStateBag == null ? null : asyncStateBag.PerRequest;

            try
            {
                if (this.IsAborted)
                {
                    if (pereq != null)
                    {
                        pereq.SetComplete();
                    }

                    this.SetCompleted();
                }
                else
                {
                    this.CompleteCheck(pereq, InternalError.InvalidEndGetResponseCompleted);
                    pereq.SetRequestCompletedSynchronously(asyncResult.CompletedSynchronously);
                    this.SetCompletedSynchronously(asyncResult.CompletedSynchronously);

                    ODataRequestMessageWrapper requestMessage = Util.NullCheck(pereq.Request, InternalError.InvalidEndGetResponseRequest);

                    // the httpWebResponse is kept for batching, discarded by non-batch
                    IODataResponseMessage response = this.RequestInfo.EndGetResponse(requestMessage, asyncResult);
                    pereq.ResponseMessage = Util.NullCheck(response, InternalError.InvalidEndGetResponseResponse);

                    this.SetHttpWebResponse(pereq.ResponseMessage);

                    Debug.Assert(null == pereq.ResponseStream, "non-null async ResponseStream");
                    Stream httpResponseStream = null;

                    if (HttpStatusCode.NoContent != (HttpStatusCode)response.StatusCode)
                    {
                        httpResponseStream   = response.GetStream();
                        pereq.ResponseStream = httpResponseStream;
                    }

                    if ((null != httpResponseStream) && httpResponseStream.CanRead)
                    {
                        if (null == this.outputResponseStream)
                        {
                            // this is the stream we copy the reponse to
                            this.outputResponseStream = Util.NullCheck(this.GetAsyncResponseStreamCopy(), InternalError.InvalidAsyncResponseStreamCopy);
                        }

                        if (null == this.asyncStreamCopyBuffer)
                        {
                            // this is the buffer we read into and copy out of
                            this.asyncStreamCopyBuffer = Util.NullCheck(this.GetAsyncResponseStreamCopyBuffer(), InternalError.InvalidAsyncResponseStreamCopyBuffer);
                        }

                        // Make async calls to read the response stream
                        this.ReadResponseStream(asyncStateBag);
                    }
                    else
                    {
                        pereq.SetComplete();
                        this.SetCompleted();
                    }
                }
            }
            catch (Exception e)
            {
                if (this.HandleFailure(e))
                {
                    throw;
                }
            }
            finally
            {
                this.HandleCompleted(pereq);
            }
        }
Пример #6
0
        /// <summary>
        /// Loads the metadata and converts it into an EdmModel that is then used by a dataservice context
        /// This allows the user to use the DataServiceContext directly without having to manually pass an IEdmModel in the Format
        /// </summary>
        /// <returns>A service model to be used in format tracking</returns>
        internal IEdmModel LoadServiceModelFromNetwork()
        {
            HttpWebRequestMessage    httpRequest;
            BuildingRequestEventArgs requestEventArgs = null;

            // test hook for injecting a network request to use instead of the default
            if (InjectMetadataHttpNetworkRequest != null)
            {
                httpRequest = InjectMetadataHttpNetworkRequest();
            }
            else
            {
                requestEventArgs = new BuildingRequestEventArgs(
                    "GET",
                    context.GetMetadataUri(),
                    null,
                    null,
                    context.HttpStack);

                // fire the right events if they exist to allow user to modify the request
                if (context.HasBuildingRequestEventHandlers)
                {
                    requestEventArgs = context.CreateRequestArgsAndFireBuildingRequest(
                        requestEventArgs.Method,
                        requestEventArgs.RequestUri,
                        requestEventArgs.HeaderCollection,
                        requestEventArgs.ClientHttpStack,
                        requestEventArgs.Descriptor);
                }

                DataServiceClientRequestMessageArgs args = new DataServiceClientRequestMessageArgs(
                    requestEventArgs.Method,
                    requestEventArgs.RequestUri,
                    context.UseDefaultCredentials,
                    context.UsePostTunneling,
                    requestEventArgs.Headers);

                httpRequest = new HttpWebRequestMessage(args);
            }

            Descriptor descriptor = requestEventArgs != null ? requestEventArgs.Descriptor : null;

            // fire the right events if they exist
            if (context.HasSendingRequest2EventHandlers)
            {
                SendingRequest2EventArgs eventArgs = new SendingRequest2EventArgs(
                    httpRequest,
                    descriptor,
                    false);

                context.FireSendingRequest2(eventArgs);
            }

            Task <IODataResponseMessage> asyncResponse =
                Task <IODataResponseMessage> .Factory.FromAsync(httpRequest.BeginGetResponse, httpRequest.EndGetResponse,
                                                                httpRequest);

            IODataResponseMessage response = asyncResponse.GetAwaiter().GetResult();

            ReceivingResponseEventArgs responseEvent = new ReceivingResponseEventArgs(response, descriptor);

            context.FireReceivingResponseEvent(responseEvent);

            using (StreamReader streamReader = new StreamReader(response.GetStream()))
                using (XmlReader xmlReader = XmlReader.Create(streamReader))
                {
                    return(CsdlReader.Parse(xmlReader));
                }
        }
Пример #7
0
        /// <summary>
        /// Write empty response and set the HttpStatusCode to 202 Accepted
        /// </summary>
        /// <param name="responseMessage"></param>
        /// <param name="asyncToken"></param>
        public static void WriteAsyncPendingResponse(IODataResponseMessage responseMessage, string asyncToken)
        {
            responseMessage.SetStatusCode(HttpStatusCode.Accepted);
            responseMessage.SetHeader(ServiceConstants.HttpHeaders.DataServiceVersion, "4.0");
            Uri location = new Uri(ServiceConstants.ServiceBaseUri, string.Format("{0}?{1}={2}", ServiceConstants.ServicePath_Async, ServiceConstants.QueryOption_AsyncToken, asyncToken));
            responseMessage.SetHeader(ServiceConstants.HttpHeaders.Location, location.OriginalString);

            if (!(responseMessage is ODataBatchOperationResponseMessage))
            {
                using (responseMessage.GetStream())
                {
                }
            }
        }
Пример #8
0
 /// <summary>
 /// Write empty response and set the HttpStatusCode to 204 NoContent
 /// </summary>
 /// <param name="responseMessage"></param>
 public static void WriteEmptyResponse(IODataResponseMessage responseMessage, HttpStatusCode statusCode = HttpStatusCode.NoContent)
 {
     responseMessage.SetStatusCode(statusCode);
     responseMessage.SetHeader("OData-Version", "4.0");
     if (!(responseMessage is ODataBatchOperationResponseMessage))
     {
         using (responseMessage.GetStream())
         {
         }
     }
 }
Пример #9
0
        /// <summary>
        /// copy the response data
        /// </summary>
        /// <param name="response">response object</param>
        private void HandleOperationResponseData(IODataResponseMessage response)
        {
            Debug.Assert(response != null, "response != null");

            using (Stream stream = response.GetStream())
            {
                if (null != stream)
                {
                    // we need to check for whether the incoming stream was data or not. Hence we need to copy it to a temporary memory stream
                    using (MemoryStream memoryStream = new MemoryStream())
                    {
                        if (WebUtil.CopyStream(stream, memoryStream, ref this.buildBatchBuffer) != 0)
                        {
                            // set the memory stream position to zero again.
                            memoryStream.Position = 0;
                            this.HandleOperationResponseData(response, memoryStream);
                        }
                        else
                        {
                            this.HandleOperationResponseData(response, null);
                        }
                    }
                }
            }
        }