Why Are My Microsoft Graph Calls to SharePoint Unreliable in ASP.NET Core API?

Groenewald, Zander 0 Reputation points
2024-12-12T05:36:56.3666667+00:00

I'm currently developing an ASP.NET Core API that interacts with SharePoint via Microsoft Graph. While the setup works occasionally, I'm experiencing significant reliability issues. The API calls to Graph work about 50% of the time and fail the rest of the time without any changes to the code or environment.

Setup:

Custom Authentication Provider:

public class CustomAuthenticationProvider : IAuthenticationProvider
public CustomAuthenticationProvider(ITokenAcquisition tokenAcquisition)
{
    _tokenAcquisition = tokenAcquisition;
}

	public async Task AuthenticateRequestAsync(HttpRequestMessage request)
	{
	    string[] scopes = new[] { "https://graph.microsoft.com/.default" };
	    string accessToken = await _tokenAcquisition.GetAccessTokenForUserAsync(scopes);
	
	    request.Headers.Authorization = new AuthenticationHeaderValue("Bearer", accessToken);
	}
}

Graph Service Client Initliaziation:


#### Sample Graph Query:


```csharp
var query = $"fields/Number '{itemId}'"; 
var page = await _graphServiceClient.Sites[siteId].Lists[listId].Items
    .Request()
    .Expand("fields")
    .Filter(query)
    .GetAsync();

Problem

The API works sporadically, failing immediately with errors like:

  • "An existing connection was forcibly closed by the remote host."
  • MSAL Debug Info:
    • "False MSAL 4.61.3.0 MSAL.NetCore .NET 8.0.11 Microsoft Windows 10.0.22631 [2024-12-11 09:13:35Z] Only in-memory caching is used. The cache is not persisted and will be lost if the machine is restarted"

The issue occurs regardless of the size of the SharePoint library:

  • Libraries with 10,000–15,000 documents fail.
  • Libraries with just 2 documents also fail.

Observations

  • This does not seem like throttling, as the returned error codes don't match those documented for throttling scenarios.
  • The failures suggest an intermittent connectivity issue, almost as if there's no internet.

Environment

  • NuGet Packages:
    • Microsoft.Graph v4.54.0
  • Dotnet Version:
    • 8.0.404
  • OS:
    • Microsoft Windows 10.0.22631

What I Need Help With

  1. Diagnosing the root cause: Could this be related to network issues, MSAL caching, or something else entirely?
  2. Resolving intermittent connectivity: Any tips for improving reliability when making Graph API calls?
  3. Error interpretation: What does the error "An existing connection was forcibly closed by the remote host" suggest in this context?
ASP.NET Core
ASP.NET Core
A set of technologies in the .NET Framework for building web applications and XML web services.
4,719 questions
Microsoft Graph
Microsoft Graph
A Microsoft programmability model that exposes REST APIs and client libraries to access data on Microsoft 365 services.
12,689 questions
SharePoint
SharePoint
A group of Microsoft Products and technologies used for sharing and managing content, knowledge, and applications.
11,010 questions
ASP.NET API
ASP.NET API
ASP.NET: A set of technologies in the .NET Framework for building web applications and XML web services.API: A software intermediary that allows two applications to interact with each other.
358 questions
0 comments No comments
{count} votes

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.