Skip to content

Commit 366662e

Browse files
authored
Merge pull request #1786 from svrooij/docs/batching
docs: Information on batching
2 parents 20962ff + cff4cc6 commit 366662e

1 file changed

Lines changed: 31 additions & 8 deletions

File tree

docs/upgrade-to-v5.md

Lines changed: 31 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -297,16 +297,17 @@ await graphServiceClient.Me
297297
```
298298

299299
### Batch Requests
300+
300301
Apart from passing instances of `HttpRequestMessage`, batch requests support the passing of `RequestInformation` instances as follows.
301302

302303
```cs
303304
var requestInformation = graphServiceClient
304305
.Users
305306
.ToGetRequestInformation();
306307

307-
// create the content
308+
// create the batch request
308309
var batchRequestContent = new BatchRequestContent(graphServiceClient);
309-
// add steps
310+
// add one or more steps (up to 20, or check below)
310311
var requestStepId = await batchRequestContent.AddBatchRequestStepAsync(requestInformation);
311312

312313
// send and get back response
@@ -316,15 +317,37 @@ var usersResponse = await batchResponseContent.GetResponseByIdAsync<UserCollecti
316317
List<User> userList = usersResponse.Value;
317318

318319
```
319-
Find failing responses
320+
321+
Find failing responses (and retry)
322+
320323
```cs
321-
var responses = await result.GetResponsesAsync();
322-
//all the responses are successfull?
323-
var allReponsesSuccessFull = responses.Any( response => !response.Value.IsSuccessStatusCode);
324-
//the responses which do not have a success code.
325-
var failedResponses = responses.Where(response => !response.Value.IsSuccessStatusCode);
324+
var statusCodes = await batchResponseContent.GetResponsesStatusCodesAsync();
325+
// all the responses are successfull?
326+
var allReponsesSuccessFull = statusCodes.Any( x => !BatchResponseContent.IsSuccessStatusCode(x.Value));
327+
// the responses with a
328+
var rateLimitedResponses = statusCodes.Where(x => x.Value == (HttpStatusCode)429);
329+
// maybe retry those rate limited?
330+
var retryBatch = batchRequestContent.NewBatchWithFailedRequests(rateLimitedResponses);
331+
var retryResponse = await graphServiceClient.Batch.PostAsync(retryBatch);
332+
```
333+
334+
Automatically manage batch size with `BatchRequestContentCollection`.
335+
The sample above uses the `BatchRequestContent` which has a limit of max. 20 combined requests. This means you'll need to manage the batch size yourself if you go beyond the batch limit of 20 requests.
336+
337+
```csharp
338+
// Replace BatchRequestContent with BatchRequestContentCollection and you're good to go.
339+
var batchRequestContent = new BatchRequestContentCollection(graphServiceClient);
340+
341+
// or with a set batch size
342+
// var batchRequestContent = new BatchRequestContentCollection(graphServiceClient, 4);
343+
344+
// Add "unlimited" requests, but don't use "DependsOn", you cannot be sure they will be in the same request and thus fail.
345+
var requestStepId = await batchRequestContent.AddBatchRequestStepAsync(requestInformation);
346+
347+
// Execute the request like before and use the response like before.
326348
```
327349

350+
Using batched requests can make your code a lot faster, if you need to query several endpoints at once or if you're creating/deleting a lot of items at the same time. Check out the [sample code](https://github.com/svrooij/msgraph-sdk-dotnet-batching/tree/main/sample/BatchClient) in this [repository](https://github.com/svrooij/msgraph-sdk-dotnet-batching/) for a complete sample on batching and experience yourself how much faster your application can process those requests.
328351

329352
### Support for $count in request builders
330353

0 commit comments

Comments
 (0)