This documentation is archived and is not being maintained.

Batching Operations (WCF Data Services)

The Open Data Protocol (OData) supports batch processing of requests to an OData-based service. For more information, see OData: Batch Processing. In WCF Data Services, each operation that uses the DataServiceContext, such as executing a query or saving changes, results in a separate request being sent to the data service. In order to maintain a logical scope for sets of operations, you can explicitly define operational batches. This ensures that all operations in the batch are sent to the data service in a single HTTP request, enables the server to process the operations atomically, and reduces the number of round trips to the data service.

Batching Query Operations

To execute multiple queries in a single batch, you must create each query in the batch as a separate instance of the DataServiceRequest class. When you create a query request in this manner, the query itself is defined as a URI, and it follows the rules for addressing resources. For more information, see Accessing Data Service Resources (WCF Data Services). The batched query requests are sent to the data service when the ExecuteBatch method is called that contains the query request objects. This method returns a DataServiceResponse object, which is a collection of QueryOperationResponse objects that represent responses to individual queries in the batch, each of which contains either a collection of objects returned by the query or error information. When any single query operation in the batch fails, error information is returned in the QueryOperationResponse object for the operation that failed and the remaining operations are still executed. For more information, see How to: Execute Queries in a Batch (WCF Data Services).

Batched queries can also be executed asynchronously. For more information, see Asynchronous Operations (WCF Data Services).

Batching the SaveChanges Operation

When you call the SaveChanges method, all changes that the context tracks are translated into REST-based operations that are sent as requests to the OData service. By default, these changes are not sent in a single request message. To require that all changes be sent in a single request, you must call the SaveChanges method and include a value of Batch in the SaveChangesOptions enumeration that you supply to the method.

You can also asynchronously save batched changes. For more information, see Asynchronous Operations (WCF Data Services).

See Also