How to fix StackOverflowException when using the C# Graph SDK

Marc ten Cate 26 Reputation points
2023-04-12T13:06:01.21+00:00

At the moment we are struggling with the API connection used by the Microsoft Graph SDK for C#. I can't find any code which should generate a System.StackOverflowException and the calls that the SDK makes work most times and sometimes it just gives the exception. The SDK is used in a Console Application (.NET Framework 4.7.2) and uses version 4.48 at this point. Below an example of the initiation of the GraphServiveClient.

// Create builder
var confidentialClient = ConfidentialClientApplicationBuilder
     .Create(Config.GraphClientId)
     .WithAuthority($"https://login.microsoftonline.com/{Config.GraphTenantId}/v2.0")
     .WithClientSecret(Config.GraphClientSecret)
     .Build();

// Use the static GraphClientFactory to get the default pipeline
var handlers = GraphClientFactory.CreateDefaultHandlers(new DelegateAuthenticationProvider(async (requestMessage) =>
{
    // Retrieve an access token for Microsoft Graph (gets a fresh token if needed).
    var authResult = await confidentialClient
        .AcquireTokenForClient(new[] { "https://graph.microsoft.com/.default" })
        .ExecuteAsync();

    // Add the access token in the Authorization header of the API request.
    requestMessage.Headers.Add("Authorization", "Bearer " + authResult.AccessToken);
    requestMessage.Version = new Version(1, 1);
}));

// Create HTTP client
var httpClient = GraphClientFactory.Create(handlers);

// Create client
GraphClient = new GraphServiceClient(httpClient);

One of the problems that I'm facing right now is that an email won't be send. The code is as follows:

// Send mail to the administration
await GraphClient.Users["email address removed for privacy reasons"].SendMail(new GraphMessage
{
    ToRecipients = new Recipient[] { new Recipient { EmailAddress = new EmailAddress { Address = "email address removed for privacy reasons" } } },
    Subject = $"Nieuwe aanmeldingen {DateTime.Now:dd-MM-yyyy}",
    Body = new ItemBody
    {
        Content = template,
        ContentType = BodyType.Html
    }
}).Request().PostAsync();

When executing the code right after creating the GraphServiceClient it sends the mail perfectly, but when executing it further along in the code it gives an StackOverflowException.

I've tried to remove the RetryHandler which is default in the GraphServiceClient, disabling server certificate validation, different versions of the SDK. Execute it asynchronous, execute it synchronous.

All without success

Microsoft Graph
Microsoft Graph
A Microsoft programmability model that exposes REST APIs and client libraries to access data on Microsoft 365 services.
12,579 questions
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
11,128 questions
0 comments No comments
{count} vote

1 answer

Sort by: Most helpful
  1. Osjaetor 480 Reputation points
    2023-06-05T17:44:49.4333333+00:00

    Hi Marc ten Cate,

    You can configure logging using the 'GraphLogger' class to get more insights into the requests and responses exchanged with Microsoft Graph.
    
     [https://microsoftgraph.github.io/microsoft-graph-comms-samples/docs/common/Microsoft.Graph.Communications.Common.Telemetry.GraphLogger.html](https://microsoftgraph.github.io/microsoft-graph-comms-samples/docs/common/Microsoft.Graph.Communications.Common.Telemetry.GraphLogger.html)
    

    Regards,

    0 comments No comments

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.