SvRooij.Graph.Batching
0.2.1
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
<PackageReference Include="SvRooij.Graph.Batching" Version="0.2.1" />
paket add SvRooij.Graph.Batching --version 0.2.1
#r "nuget: SvRooij.Graph.Batching, 0.2.1"
// 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
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.
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
andv1
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
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 | Versions |
---|---|
.NET | net5.0 net5.0-windows net6.0 net6.0-android net6.0-ios net6.0-maccatalyst net6.0-macos net6.0-tvos net6.0-windows net7.0 net7.0-android net7.0-ios net7.0-maccatalyst net7.0-macos net7.0-tvos net7.0-windows |
.NET Core | netcoreapp2.0 netcoreapp2.1 netcoreapp2.2 netcoreapp3.0 netcoreapp3.1 |
.NET Standard | netstandard2.0 netstandard2.1 |
.NET Framework | net461 net462 net463 net47 net471 net472 net48 net481 |
MonoAndroid | monoandroid |
MonoMac | monomac |
MonoTouch | monotouch |
Tizen | tizen40 tizen60 |
Xamarin.iOS | xamarinios |
Xamarin.Mac | xamarinmac |
Xamarin.TVOS | xamarintvos |
Xamarin.WatchOS | xamarinwatchos |
-
.NETStandard 2.0
- Microsoft.Graph (>= 5.0.0)
-
net6.0
- Microsoft.Graph (>= 5.0.0)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.