internal long GetQuerySetCount(DataServiceContext context) { long num2; Version requestVersion = this.QueryComponents(context.MaxProtocolVersion).Version; if ((requestVersion == null) || (requestVersion.Major < 2)) { requestVersion = Util.DataServiceVersion2; } QueryResult result = null; DataServiceRequest <long> serviceRequest = new DataServiceRequest <long>(this.QueryComponents(context.MaxProtocolVersion), null); KeyValuePair <string, string>[] pairArray = new KeyValuePair <string, string>[] { new KeyValuePair <string, string>("Accept", "text/plain") }; IEnumerable <KeyValuePair <string, string> > headers = pairArray; ODataRequestMessageWrapper request = context.CreateODataRequestMessage("GET", this.QueryComponents(context.MaxProtocolVersion).Uri, false, requestVersion, headers, new string[] { "Accept" }); result = new QueryResult(this, "Execute", serviceRequest, request, new RequestInfo(context), null, null); try { result.ExecuteQuery(context); if (HttpStatusCode.NoContent == result.StatusCode) { throw new DataServiceQueryException(System.Data.Services.Client.Strings.DataServiceRequest_FailGetCount, result.Failure); } StreamReader reader = new StreamReader(result.GetResponseStream()); long num = -1L; try { num = XmlConvert.ToInt64(reader.ReadToEnd()); } finally { reader.Close(); } num2 = num; } catch (InvalidOperationException exception) { QueryOperationResponse response = null; response = result.GetResponse <long>(MaterializeAtom.EmptyResults); if (response != null) { response.Error = exception; throw new DataServiceQueryException(System.Data.Services.Client.Strings.DataServiceException_GeneralError, exception, response); } throw; } return(num2); }
/// <summary> /// execute uri and materialize result /// </summary> /// <typeparam name="TElement">element type</typeparam> /// <param name="context">context</param> /// <param name="queryComponents">query components for request to execute</param> /// <returns>enumerable of results</returns> internal QueryOperationResponse <TElement> Execute <TElement>(DataServiceContext context, QueryComponents queryComponents) { QueryResult result = null; try { Uri requestUri = queryComponents.Uri; DataServiceRequest <TElement> serviceRequest = new DataServiceRequest <TElement>(requestUri, queryComponents, this.Plan); result = serviceRequest.CreateExecuteResult(this, context, null, null, Util.ExecuteMethodName); result.AllowDirectNetworkStreamReading = context.AllowDirectNetworkStreamReading; result.ExecuteQuery(); return(result.ProcessResult <TElement>(this.Plan)); } catch (InvalidOperationException ex) { if (result != null) { QueryOperationResponse operationResponse = result.GetResponse <TElement>(MaterializeAtom.EmptyResults); if (null != operationResponse) { if (context.IgnoreResourceNotFoundException) { DataServiceClientException cex = ex as DataServiceClientException; if (cex != null && cex.StatusCode == (int)HttpStatusCode.NotFound) { // don't throw return((QueryOperationResponse <TElement>)operationResponse); } } operationResponse.Error = ex; throw new DataServiceQueryException(Strings.DataServiceException_GeneralError, ex, operationResponse); } } throw; } }
/// <summary> /// Synchronizely get the query set count from the server by executing the $count=value query /// </summary> /// <param name="context">The context</param> /// <returns>The server side count of the query set</returns> internal long GetQuerySetCount(DataServiceContext context) { Debug.Assert(null != context, "context is null"); Version requestVersion = this.QueryComponents(context.Model).Version; if (requestVersion == null || requestVersion.Major < 2) { // minimum DSV for $count is V2. requestVersion = Util.DataServiceVersion2; } QueryResult response = null; QueryComponents qc = this.QueryComponents(context.Model); Uri requestUri = qc.Uri; DataServiceRequest <long> serviceRequest = new DataServiceRequest <long>(requestUri, qc, null); HeaderCollection headers = new HeaderCollection(); // Validate and set the request DSV header headers.SetRequestVersion(requestVersion, context.MaxProtocolVersionAsVersion); context.Format.SetRequestAcceptHeaderForCount(headers); string httpMethod = XmlConstants.HttpMethodGet; ODataRequestMessageWrapper request = context.CreateODataRequestMessage( context.CreateRequestArgsAndFireBuildingRequest(httpMethod, requestUri, headers, context.HttpStack, null /*descriptor*/), new string[] { XmlConstants.HttpRequestAccept } /*headersToReset*/, null /*descriptor*/); response = new QueryResult(this, Util.ExecuteMethodName, serviceRequest, request, new RequestInfo(context), null, null); response.AllowDirectNetworkStreamReading = context.AllowDirectNetworkStreamReading; IODataResponseMessage responseMessage = null; try { responseMessage = response.ExecuteQuery(); if (HttpStatusCode.NoContent != response.StatusCode) { StreamReader sr = new StreamReader(response.GetResponseStream()); long r = -1; try { r = XmlConvert.ToInt64(sr.ReadToEnd()); } finally { sr.Close(); } return(r); } else { throw new DataServiceQueryException(Strings.DataServiceRequest_FailGetCount, response.Failure); } } catch (InvalidOperationException ex) { QueryOperationResponse operationResponse = null; operationResponse = response.GetResponse <long>(MaterializeAtom.EmptyResults); if (null != operationResponse) { operationResponse.Error = ex; throw new DataServiceQueryException(Strings.DataServiceException_GeneralError, ex, operationResponse); } throw; } finally { WebUtil.DisposeMessage(responseMessage); } }