Partager via


Batching Operations (WCF Data Services)

Important

WCF Data Services has been deprecated and will no longer be available for download from the Microsoft Download Center. WCF Data Services supported earlier versions of the Microsoft OData (V1-V3) protocol only and has not been under active development. OData V1-V3 has been superseded by OData V4, which is an industry standard published by OASIS and ratified by ISO. OData V4 is supported through the OData V4 compliant core libraries available at Microsoft.OData.Core. Support documentation is available at OData.Net, and the OData V4 service libraries are available at Microsoft.AspNetCore.OData.

RESTier is the successor to WCF Data Services. RESTier helps you bootstrap a standardized, queryable, HTTP-based REST interface in minutes. Like WCF Data Services before it, Restier provides simple and straightforward ways to shape queries and intercept submissions before and after they hit the database. And like Web API + OData, you still have the flexibility to add your own custom queries and actions with techniques you're already familiar with.

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<TElement> 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. 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<T> 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<T> object for the operation that failed and the remaining operations are still executed. For more information, see How to: Execute Queries in a Batch.

Batched queries can also be executed asynchronously. For more information, see Asynchronous Operations.

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(SaveChangesOptions) 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.

See also