简体   繁体   中英

Microsoft Graph HttpProvider Error on File Upload

I have a PDF I need to upload to a SharePoint Document Library using Microsoft Graph. My code has successfully done this for months, until it stopped working April 17 with seemingly no change to our SharePoint and certainly not my code.

I have a function that performs the upload:

public static async Task<DriveItem> UploadLargeFile(string localFilePath, string spPath)
    {
        DriveItem uploadedFile = null;
        using (Stream fileStream = System.IO.File.OpenRead(localFilePath))
        {
            UploadSession uploadSession = await GraphAPIConnection.GraphClient.Sites[siteid].Drives[driveid].Root.ItemWithPath(spPath).CreateUploadSession().Request().PostAsync();
            List<Exception> exceptions = new List<Exception>();
            if (uploadSession != null)
            {
                //Chunk size must be divisible by 320KiB, our chunk size will be slightly more than 1MB 
                int maxSizeChunk = (320 * 1024) * 4;
                ChunkedUploadProvider uploadProvider = new ChunkedUploadProvider(uploadSession, GraphAPIConnection.GraphClient, fileStream, maxSizeChunk);
                IEnumerable<UploadChunkRequest> chunkRequests = uploadProvider.GetUploadChunkRequests();
                byte[] readBuffer = new byte[maxSizeChunk];

                //Using one session, upload each chunk of the file individually
                foreach (UploadChunkRequest request in chunkRequests)
                {
                    try
                    {
                        UploadChunkResult result = await uploadProvider.GetChunkRequestResponseAsync(request, readBuffer, exceptions);
                        if (result.UploadSucceeded)
                        {
                            uploadedFile = result.ItemResponse;
                        }
                    }
                    catch (Exception ex)
                    {
                        Debug.WriteLine("message: " + ex.Message + "\t\ttrace: " + ex.StackTrace);
                    }
                }
            }
        }
        return uploadedFile;
    }

My catch produces the following output:

message: Code: generalException Message: An error occurred sending the request. trace: at Microsoft.Graph.HttpProvider.d__19.MoveNext() --- End of stack trace from previous location where exception was thrown --- at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) at Microsoft.Graph.HttpProvider.d__18.MoveNext() --- End of stack trace from previous location where exception was thrown --- at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) at Microsoft.Graph.UploadChunkRequest.d__17.MoveNext() --- End of stack trace from previous location where exception was thrown --- at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) at Microsoft.Graph.UploadChunkRequest.d__16.MoveNext() --- End of stack trace from previous location where exception was thrown --- at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) at Microsoft.Graph.ChunkedUploadProvider.d__17.MoveNext() --- End of stack trace from previous location where exception was thrown --- at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) at System.Runtime.CompilerServices.TaskAwaiter`1.GetResult() at MyNameSpace.SharePointUploader.d__9.MoveNext() in MyDir\SharePointUploader.cs:line 173

As you can see,

uploadProvider.GetChunkRequestResponseAsync(request, readBuffer, exceptions)

leads to the following that produces an error:

at Microsoft.Graph.HttpProvider.d__19.MoveNext()

I've double checked that all of the inputs to the function appear valid; and again there have been no changes to the code in months. I've tried a third party ETL software and was able to upload the PDF to the folder I wanted - that software is also built using Microsoft Graph.

Here is where I am stumped. How can I further debug this to determine exactly why HttpProvider is throwing an error?

Found the fix to this. Two part answer.

How to better debug an issue like this (how I found it, in case my solution below does not help you)

  • Set a breakpoint where the error occurs.
  • Have the following settings in your Visual Studio Debug options. This will allow you to Step Into (F11) libraries like this to get a better look at what's going. 在此处输入图像描述
  • Run the code until the breakpoint line.
  • Have the Debug "Locals" window open.
  • Hit F11 to step into the line with the error.
  • Expand the nested "Inner Exception" variables to get more verbose errors.

Solving the issue

  • Using the debugging above led me to the following error " unable to read data from the transport connection: the connection was closed by remote host " (remote host being SharePoint).
  • SharePoint has been deprecating old TLS. Using older .NET Frameworks means that 1.2 may not be default in your code. Credit to: Unable to read data from the transport connection: An existing connection was forcibly closed by the remote host
  • Simply update your Project Framework to 4.7.2 or later to enforce TLS 1.2. The connection should now be accepted by the SharePoint host, and not get closed by your application.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM