I’m using Microsoft Graph APIs to access a SharePoint site and everything is working great up to (and including) the NuGet package version 4.18. Any higher version of this package, causes the API calls to hang and then eventually time out with the usual error “A task was canceled”. I checked any potential deadlocks, but I can’t find anything being wrong with my code. In the example below, the program hangs when trying to retrieve the Site Id. Here is the code:
internal async Task Initialize() { await GetSpAccessTokenAsync(); await GetSiteIdAsync(); } private async Task GetSpAccessTokenAsync() { try { Console.WriteLine("Get certificate."); X509Certificate2 certificate = GetCertificate(cfg.GraphCertificate, cfg.GraphPrivateKey, cfg.GraphPassphrase); if (certificate != null) { Console.WriteLine("Certificate obtained. Get access token."); AuthenticationResult authResult = await GetAccessTokenAsync(certificate); accessTokenExpiryDate = TimeZoneInfo.ConvertTimeFromUtc(authResult.ExpiresOn.UtcDateTime, TimeZoneInfo.FindSystemTimeZoneById("Central Standard Time")); Console.WriteLine($"Token expiry date: {accessTokenExpiryDate}"); client = new GraphServiceClient( new DelegateAuthenticationProvider( (reqMsg) => { // Append the access token to the request. reqMsg.Headers.Authorization = new AuthenticationHeaderValue("bearer", authResult.AccessToken); return Task.FromResult(0); })); } } catch (Exception e) { Console.WriteLine($"Error in GetSpAccessTokenAsync(). {e.Message}"); throw; } } private async Task<AuthenticationResult> GetAccessTokenAsync(X509Certificate2 certificate) { string[] scopes = new string[] { "https://graph.microsoft.com/.default" }; IConfidentialClientApplication app; app = ConfidentialClientApplicationBuilder.Create(cfg.GraphSpApplicationId) .WithAuthority(new Uri(cfg.CertificateAuthority)) .WithCertificate(certificate) .Build(); AuthenticationResult authenticationResult = await app.AcquireTokenForClient(scopes).ExecuteAsync(); return authenticationResult; } private async Task GetSiteIdAsync() { try { Console.WriteLine("Get site id."); Site site = await client.Sites .GetByPath(cfg.SharePointSiteUrl.AbsolutePath, cfg.SharePointSiteUrl.DnsSafeHost) .Request() .GetAsync(); Console.WriteLine("Site API invoked."); siteId = site.Id; Console.WriteLine($"Site id = {siteId}"); } catch (Exception e) { Console.WriteLine($"Exception in GetSiteIdAsync(). {e.Message}"); } }