Skip to content

svrooij/msgraph-sdk-dotnet-batching

Repository files navigation

SvRooij.Graph.Batching

nuget github issues github stars Language License github sponsor

The Microsoft Graph Client has support for Batching, which is a great idea when you are doing a lot of requests to the Graph API. By batching requests you can achieve much higher throughput.

The original batch implementation in the GraphServiceClient feels incomplete, by default the GraphServiceClient let's you combine up to 20 requests before throwing an exception.

By using this extension you can combine "unlimited" requests and have this library automatically split up the requests in multiple batches. While staying very close to the original implementation.

Hackathon

This project was starting during the Hack Together: Microsoft Graph and .NET

Watch the video

Superseded by Microsoft.Graph.Core 3.0.1

After the release of this extension to the Graph SDK, it got a lot of attention, so I decided to create a PR for the SDK itself. Today, March 7th they released Microsoft.Graph.Core version 3.0.1 which included the code that was also in this extension. The extension will get one last update, to deprecate all code and refer to the official sdk. More details

Batching with Microsoft Graph

This library stays really close to the build-in batch support so go ahead and read that documentation before hand.

// Create a GraphServiceClient with your required IAuthenticationProvider
var graphClient = new GraphServiceClient(...);

// Create a BatchRequestContent (your batch request container)
var batchRequestContent = new BatchRequestContent(graphClient);

// Add two or more (but max 20) requests to it
var getRequest1 = await batchRequestContent.AddBatchRequestStepAsync(graphClient.Me.ToGetRequestInformation());
var getRequest2 = await batchRequestContent.AddBatchRequestStepAsync(graphClient.Me.ToGetRequestInformation());

// Execute the batch request
var response = await graphClient.Batch.PostAsync(batchRequestContent);

// Do something with the result
var user = await response.GetResponseByIdAsync<User>(getRequest1);
Console.WriteLine("Hi {0}", user.DisplayName);

Introducing the BatchRequestContentCollection

Instead of creating a BatchRequestContent, you now create a BatchRequestContentCollection and continue using it as before.

// Create a GraphServiceClient with your required IAuthenticationProvider
var graphClient = new GraphServiceClient(...);

// Create a BatchRequestContentCollection (your batch request container)
var batchRequestContent = new BatchRequestContentCollection(graphClient);

// Add two or more requests to it
// If you add more then 20 they will be spitted across multiple batch requests automatically.
var getRequest1 = await batchRequestContent.AddBatchRequestStepAsync(graphClient.Me.ToGetRequestInformation());
var getRequest2 = await batchRequestContent.AddBatchRequestStepAsync(graphClient.Me.ToGetRequestInformation());

// Execute all the batch requests
var response = await graphClient.Batch.PostAsync(batchRequestContent);

// Do something with the result
var user = await response.GetResponseByIdAsync<User>(getRequest1);
...

Things to keep in mind

  • You cannot combine requests to multiple tenants in a single batch.
  • You cannot combine requests to beta and v1 endpoints.
  • You should test wether or not batching results in higher speeds.

Regular batching support request dependencies, because you don't know if the requests are put in the same batch, you should be careful depending on those.

About the author

LinkedIn Profile Link Mastodon Follow on Twitter Check my blog

I like building applications and am somewhat of a Microsoft Graph API expert. I used this knowledge to build this batching helper. But I'm only human so please validate, and if you find an issue please let me know. If you like this extension give me a shout out on twitter @svrooij. You can also follow my blog if you're interested in these sort of projects