GraphServiceClient lifetime #1654
Unanswered
albus-sonitus
asked this question in
Q&A
Replies: 0 comments
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
-
I've been struggling a bit with whether or not I should be registering a GraphServiceClient with a singleton or scoped lifetime. I see in the AddMicrosoftGraph extension method that it's been added with a scoped lifetime. Is there a reason for this? I've seen recommendations that one should keep the client around and creating new clients all the time isn't a good idea. Originally, I was hesitant to make it a singleton due to token expirations, but after further digging, it looks like the client is fetching a new token on every request. (I could be wrong there. I've been digging through the code and not entirely confident I was looking in the right place.) Any insights? As a very basic example, I was hoping to something like..
services.AddSingleton(new GraphServiceClient(new ManagedIdentityCredential()));My worry is that the token will expire at some point during the lifetime of the application, unless it will automatically refresh? If it's fetching a new one for every request, then I guess it's a moot point. Where am I going wrong here?
Beta Was this translation helpful? Give feedback.
All reactions