Export (0) Print
Expand All

DataServiceRequest<TElement> Class

Represents request objects submitted as a batch to the data service.

Namespace:  System.Data.Services.Client
Assembly:  System.Data.Services.Client (in System.Data.Services.Client.dll)

public sealed class DataServiceRequest<TElement> : DataServiceRequest

Type Parameters


In a group of queries submitted as a batch to the data service, the queries are specified as DataServiceRequest<TElement> instances. A DataServiceResponse is returned that represents the response of the batch request as a whole. Individual query responses are represented as QueryOperationResponse objects, derived from OperationResponse, that are accessible by enumerating the DataServiceResponse instance.

When ExecuteBatch returns, the entire HTTP response for the batch request has been read from the network stream, but the responses have not been processed. Identity resolution and object materialization do not occur for a specified entity in the response until it is iterated as shown in the following example.

DataServiceContext service = new DataServiceContext(new 

// Create query batches.
DataServiceRequest[] reqs = new DataServiceRequest[] { 
    new DataServiceRequest<Category>(
            new Uri("http://myserviceroot/Categories")),
            new DataServiceRequest<Customer>(
            new Uri("http://myserviceroot/Customers"))

DataServiceResponse dsr;
    // Client will not throw an exception on ExecuteBatch because the 
    // entire response has not been processed yet to know
    // whether an exception should be thrown.

    dsr = service.ExecuteBatch(reqs);   

    if (dsr.IsBatchResponse) 
        /*inspect HTTP artifacts associated with the entire batch:
                      dsr.BatchHeaders, dsr.BatchStatusCode*/ }

    foreach (QueryOperationResponse qr in dsr)
        if (IsErrorStatusCode(qr.StatusCode))   
            //q.Error.Message contains the full contents of the error.
            /* process any part of the Error Contract (<error> element)
                      sent from the service.  */
            if (qr.Query.ElementType == typeof(Customer))
                //process customers
                foreach (Customer c in qr){ /*process the customer*/ }
                // the DataServiceContext does not materialize, resolve
                // identity on Customer until it is enumerated.
            else if (qr.Query.ElementType == typeof(Category))
                // Process categories.
                foreach (Category cat in qr)
                    /*process the category*/ 
                // the DataServiceContext does not materialize or
                // resolve identity on the Category until
                // it is enumerated.
                // This means that instream errors will be thrown
                // during iteration.
catch (DataServiceRequestException e)
    // This error is thrown if the data service returns with
    // a response code < 200 or >299 or the top level element.
    // If neither of the above or true, this exception is not 
    // thrown.

    dsr = e.Response;
    if (dsr.IsBatchResponse) 
        /*inspect HTTP artifacts associated with the entire batch:
                        dsr.BatchHeaders, dsr.BatchStatusCode*/ 
    /* There will always only be one of these because if the top level
     status code was >=200 and =<299 and the first element was not an
     error, the call to start the query will not throw. */

    foreach (QueryOperationResponse qr in dsr)
        if (qr.Error != null)
            // Process error.


Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.

Windows 7, Windows Vista, Windows XP SP2, Windows Server 2008 R2, Windows Server 2008, Windows Server 2003

The .NET Framework and .NET Compact Framework do not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

.NET Framework

Supported in: 3.5 SP1

Community Additions

© 2014 Microsoft