Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Client bug]: Batch request retries, created with BatchRequestContentCollection.NewBatchWithFailedRequests, fail via GraphServiceClient.Batch.PostAsync due to a closed Stream #2166

Closed
Debro012 opened this issue Oct 17, 2023 · 0 comments · Fixed by microsoftgraph/msgraph-sdk-dotnet-core#776
Labels

Comments

@Debro012
Copy link

Debro012 commented Oct 17, 2023

Describe the bug
Retrying request batches lead to an exception being thrown due to the inability to deserialize content from a closed Stream

To Reproduce
Steps to reproduce the behavior:

// build the batch
var batchRequestContent = new BatchRequestContentCollection(graphClient);
var requestIds = new List<string>();

// build a request for each list of attendees and add the request to the batch
foreach (attendeeList in attendeesByRegion)
{
    var meetingStartTime = new DateTimeTimeZone
    {
        DateTime = startTime,
        TimeZone = timeZone
    };

    var meetingEndTime = new DateTimeTimeZone
    {
        DateTime = endTime,
        TimeZone = timeZone
    };


    GetSchedulePostRequestBody requestBody = new GetSchedulePostRequestBody()
    {
        AvailabilityViewInterval = timeInterval,
        EndTime = meetingEndTime,
        StartTime = meetingStartTime,
        Schedules = attendeeList,
    };

    var request = graphClient.Users[userEmailAddress]
        .Calendar
        .GetSchedule
        .ToPostRequestInformation(requestBody, requestConfig => requestConfig.Headers.Add("Prefer", $"outlook.timezone=\"{timeZone}\""));

    // add the request to the batch
    var requestId = await batchRequestContent.AddBatchRequestStepAsync(request);
    requestIds.Add(requestId);
};

var batchResponseContent = await graphClient.Batch.PostAsync(batchRequestContent);
var statusCodes = await batchResponseContent.GetResponsesStatusCodesAsync();
var failedRequests = statusCodes.Where(x => !BatchResponseContent.IsSuccessStatusCode(x.Value));
var successfulRequests = statusCodes.Where(x => BatchResponseContent.IsSuccessStatusCode(x.Value));

// retry failed requests
var failedRequestDictionary = new Dictionary<string, HttpStatusCode>();
foreach (var request in requests)
{
    failedRequestDictionary.Add(request.Key, request.Value);
}

var retryBatch = batchRequestContent.NewBatchWithFailedRequests(failedRequestDictionary);            
var batchResponseContent = await graphClient.Batch.PostAsync(retryBatch);

Expected behavior
For a BatchResponseContentCollection to be returned, containing the results of the retried requests

Screenshots

Exception thrown: 'Microsoft.Graph.ClientException' in System.Private.CoreLib.dll
Microsoft.AspNetCore.Diagnostics.DeveloperExceptionPageMiddleware: Error: An unhandled exception has occurred while executing the request.

Microsoft.Graph.ClientException: Unable to deserialize content.
 ---> System.ObjectDisposedException: Cannot access a closed Stream.
   at System.IO.MemoryStream.Read(Byte[] buffer, Int32 offset, Int32 count)
   at System.IO.MemoryStream.ReadAsync(Memory`1 buffer, CancellationToken cancellationToken)
--- End of stack trace from previous location ---
   at System.Text.Json.JsonDocument.ReadToEndAsync(Stream stream, CancellationToken cancellationToken)
   at System.Text.Json.JsonDocument.ParseAsyncCore(Stream utf8Json, JsonDocumentOptions options, CancellationToken cancellationToken)
   at Microsoft.Graph.BatchRequestContent.GetRequestContentAsync(HttpRequestMessage request)
   --- End of inner exception stack trace ---
   at Microsoft.Graph.BatchRequestContent.GetRequestContentAsync(HttpRequestMessage request)
   at Microsoft.Graph.BatchRequestContent.WriteBatchRequestStepAsync(BatchRequestStep batchRequestStep, Utf8JsonWriter writer)
   at Microsoft.Graph.BatchRequestContent.GetBatchRequestContentAsync()
   at Microsoft.Graph.Core.Requests.BatchRequestBuilder.ToPostRequestInformationAsync(BatchRequestContent batchRequestContent)
   at Microsoft.Graph.Core.Requests.BatchRequestBuilder.PostAsync(BatchRequestContent batchRequestContent, CancellationToken cancellationToken, Dictionary`2 errorMappings)
   at Microsoft.Graph.Core.Requests.BatchRequestBuilder.PostAsync(BatchRequestContentCollection batchRequestContentCollection, CancellationToken cancellationToken, Dictionary`2 errorMappings)

Client version
5.30.0

Desktop (please complete the following information):

  • OS: Windows 11
  • Browser: Chrome
  • Visual Studio Enterprise 2022 - 17.5.5

Additional context
Add any other context about the problem here.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants