SvRooij.Graph.Batching 0.2.1

Suggested Alternatives

Microsoft.Graph.Core 3.0.1

Additional Details

Package superseded by pull request to Graph SDK https://github.com/microsoftgraph/msgraph-sdk-dotnet-core/pull/613

dotnet add package SvRooij.Graph.Batching --version 0.2.1
NuGet\Install-Package SvRooij.Graph.Batching -Version 0.2.1
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="SvRooij.Graph.Batching" Version="0.2.1" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add SvRooij.Graph.Batching --version 0.2.1
#r "nuget: SvRooij.Graph.Batching, 0.2.1"
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// Install SvRooij.Graph.Batching as a Cake Addin
#addin nuget:?package=SvRooij.Graph.Batching&version=0.2.1

// Install SvRooij.Graph.Batching as a Cake Tool
#tool nuget:?package=SvRooij.Graph.Batching&version=0.2.1

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

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

Product Compatible and additional computed target framework versions.
.NET net5.0 was computed.  net5.0-windows was computed.  net6.0 is compatible.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 was computed.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 was computed.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed. 
.NET Core netcoreapp2.0 was computed.  netcoreapp2.1 was computed.  netcoreapp2.2 was computed.  netcoreapp3.0 was computed.  netcoreapp3.1 was computed. 
.NET Standard netstandard2.0 is compatible.  netstandard2.1 was computed. 
.NET Framework net461 was computed.  net462 was computed.  net463 was computed.  net47 was computed.  net471 was computed.  net472 was computed.  net48 was computed.  net481 was computed. 
MonoAndroid monoandroid was computed. 
MonoMac monomac was computed. 
MonoTouch monotouch was computed. 
Tizen tizen40 was computed.  tizen60 was computed. 
Xamarin.iOS xamarinios was computed. 
Xamarin.Mac xamarinmac was computed. 
Xamarin.TVOS xamarintvos was computed. 
Xamarin.WatchOS xamarinwatchos was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
0.2.1 256 3/7/2023
0.1.2 244 3/2/2023