Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the recommendation to reuse GraphServiceClient in Microsoft Graph .Net SDK?

I realized Microsoft Graph .Net SDK is using HttpClient class.

https://github.com/microsoftgraph/msgraph-sdk-dotnet/blob/dev/src/Microsoft.Graph.Core/Requests/HttpProvider.cs

Microsoft's own documentation recommends reuse of HttpClient instances as much as possible instead of spinning up a new instance per request which may lead to exhausting the connection pool and SocketExceptions eventually.

Is there a similar recommendation, to reuse GraphServiceClient as much as possible? Is there any particular concern with instantiating a new GraphServiceClient per request?

like image 987
Dogu Arslan Avatar asked Sep 10 '18 09:09

Dogu Arslan


People also ask

What is Microsoft Graph SDK?

The Microsoft Graph SDKs are designed to simplify building high-quality, efficient, and resilient applications that access Microsoft Graph. The SDKs include two components: a service library and a core library.

How do I find the Client ID for Microsoft Graph?

For a service that will call Microsoft Graph under its own identity, you need to register your app for the Web platform and copy the following values: The application ID assigned by the Azure app registration portal. A client (application) secret, either a password or a public/private key pair (certificate).


Video Answer


1 Answers

I am not aware of any recommendation, but if you look at the code from both the GraphServiceClient as the underlying BaseClient, there is not state kept. Only the incoming or defaulted HttpProvider, and there is the problem. If you rely on the GraphServiceClient generating a new HttpProvider (and thus a new HttpClient) each and every time, you have the same problem as with creating multiple HttpClient instances.

So if you are recreating clients, you should at least provide it with a cached HttpProvider. And then, it doesn't hurt much to keep the entire client in cache.

like image 177
Patrick Hofman Avatar answered Sep 20 '22 23:09

Patrick Hofman