microsoftgraph / microsoftgraph/msgraph-sdk-dotnet
[Client bug]: Helper class `AsyncMonitor` doesn't work in conjunction with v5 SDK
Nobody has claimed this yet.
- Dominant language
- C#
- Stars
- 789
- Forks
- 264
- Avg merge
- 15h 17m
- Merged PRs (30d)
- 3
Description
Describe the bug
Unfortunately there is no official code example or documentation available on how to use the SDK to copy a DriveItem and check the current process of copying. The latest given example here look okay, but doesn't work and lacks some problems.
Within the class AsyncMonitor the given client is used to send the request. This will lead to the fact, that the request will use the provided authentication provider to attach an authentication token to this request. But the Url of the copy operation is anonymous and returns a 401 status if a auth header is added. The response won't be checked for a failing request and waits endless, cause the response body contains an error message, that prevents reaching the throw statement.
If a service client without authentication is used like this:
var unauthorizedClient = new GraphServiceClient(_httpClientFactory.CreateClient(string.Empty));
var asyncMonitor = new AsyncMonitor<???>(unauthorizedClient, progressUrl);
the response gets a 200 status and contains something like this:
{
"@odata.context": "https://myDomain.sharepoint.com/sites/myGroup/_api/v2.0/$metadata#oneDrive.asynchronousOperationStatus",
"percentageComplete": 100,
"resourceId": "01AIKISTH5XQ6VIEFNDNG34GGK3SAXOJBY",
"status": "completed"
}
In that case it will be deserialized using the JsonConverter and the given generic type of AsyncMonitor<T>. Unfortunately this type (would be in my case guessed as being DriveItem) doesn't match the given response json of being an oneDrive.asynchronousOperationStatus. But even if we would use an AsyncMonitor<AsynchronousOperationStatus> it wouldn't work, cause the class is using the Kiota mechanism und can't be used in conjunction with JsonSerializer. So the deserialized object has everything set to null and doesn't contain any values.
Expected behavior
Try to copy a file using the current Graph SDK and try to watch the copy progress by something like this:
var nativeResponseHandler = new NativeResponseHandler();
await graphServiceClient.Drives["driveId"].Items["sourceItemId"].Copy.PostAsync(new CopyPostRequestBody
{
Name = sourceItem.Name,
ParentReference = reference,
},
requestConfiguration => requestConfiguration.Options.Add(new ResponseHandlerOption(){
ResponseHandler = nativeResponseHandler }));
var responseMessage = nativeResponseHandler.Value as HttpResponseMessage;
var locationHeader = responseMessage.Headers.Location;
var asyncMonitor = new AsyncMonitor<DriveItem>(graphServiceClient, locationHeader.OriginalString);
var progress = new Progress<AsyncOperationStatus>(status =>
{
Console.WriteLine($"Status: {status.Status}");
Console.WriteLine($"Percentage complete: {status.PercentageComplete}");
Console.WriteLine($"Operation: {status.Operation}");
});
var result = await asyncMonitor.PollForOperationCompletionAsync(progress, CancellationToken.None);
Client version
v5.3.0
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start with src/Microsoft.Graph.Core/Requests/AsyncMonitor.cs, especially the request and response handling linked in the report, then inspect src/Microsoft.Graph.Core/Models/AsyncOperationStatus.cs and the v5 copy example. Reproduce the flow with the DriveItem copy request and its Location header. Done means authenticated monitoring no longer hangs on the anonymous status URL and the returned operation status contains its JSON values.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- csharp
- Domain
- api, backend
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 45/100