简体   繁体   English

文件上传时 Microsoft Graph HttpProvider 错误

[英]Microsoft Graph HttpProvider Error on File Upload

I have a PDF I need to upload to a SharePoint Document Library using Microsoft Graph.我有一个 PDF 我需要使用 Microsoft Graph 上传到 SharePoint 文档库。 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.我的代码已经成功完成了几个月,直到它在 4 月 17 日停止工作,我们的 SharePoint 似乎没有任何变化,当然也不是我的代码。

I have a function that performs the upload:我有一个执行上传的 function:

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:我的捕获产生以下 output:

message: Code: generalException Message: An error occurred sending the request.消息:代码:generalException 消息:发送请求时发生错误。 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 p跟踪:在 Microsoft.Graph.HttpProvider.d__19.MoveNext() --- 从先前引发异常的位置结束堆栈跟踪 --- 在 System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) 在 System.Runtime。 CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) at Microsoft.Graph.HttpProvider.d__18.MoveNext() --- 从先前抛出异常的位置结束堆栈跟踪---在 System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task任务)在 System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(任务任务)在 Microsoft.Graph.UploadChunkRequest.d__17.MoveNext() --- 在 System.Runtime 中从先前引发异常的位置结束堆栈跟踪。 CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) at Microsoft.Graph.UploadChunkRequest.d__16.MoveNext() --- 从 p 结束堆栈跟踪revious 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抛出异常的过往位置 --- 在 System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) 在 System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) 在 Microsoft.Graph.ChunkedUploadProvider.d__17.MoveNext() - -- 从先前引发异常的位置结束堆栈跟踪 --- 在 System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) 在 System.Runtime.CompilerServices 在 System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) .TaskAwaiter`1.GetResult() 在 MyNameSpace.SharePointUploader.d__9.MoveNext() 在 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()在 Microsoft.Graph.HttpProvider.d__19.MoveNext()

I've double checked that all of the inputs to the function appear valid;我仔细检查了 function 的所有输入是否有效; 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.我尝试了第三方 ETL 软件,并且能够将 PDF 上传到我想要的文件夹 - 该软件也是使用 Microsoft Graph 构建的。

Here is where I am stumped.这就是我难过的地方。 How can I further debug this to determine exactly why HttpProvider is throwing an error?我如何进一步调试它以确定 HttpProvider 抛出错误的确切原因?

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.在 Visual Studio 调试选项中进行以下设置。 This will allow you to Step Into (F11) libraries like this to get a better look at what's going.这将允许您像这样进入 (F11)库,以便更好地了解正在发生的事情。 在此处输入图像描述
  • Run the code until the breakpoint line.运行代码直到断点行。
  • Have the Debug "Locals" window open.打开调试“Locals”window。
  • Hit F11 to step into the line with the error.按 F11 进入出现错误的行。
  • 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)。
  • SharePoint has been deprecating old TLS. SharePoint 已弃用旧 TLS。 Using older .NET Frameworks means that 1.2 may not be default in your code.使用较旧的 .NET 框架意味着 1.2 在您的代码中可能不是默认的。 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.只需将您的项目框架更新到 4.7.2 或更高版本即可强制执行 TLS 1.2。 The connection should now be accepted by the SharePoint host, and not get closed by your application. SharePoint 主机现在应该接受连接,并且不会被您的应用程序关闭。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 无法使用 Microsoft Graph SDK 上传文件 - Unable to upload file using Microsoft Graph SDK Microsoft Graph Api 在文件夹中上传大文件 - Microsoft Graph Api Upload Large File in a folder 使用 Microsoft Graph API 将文件上传到 MVC 应用程序中的 onedrive,权限错误 - Using Microsoft Graph API to upload file to onedrive in MVC Application, Error with permissions Microsoft Graph .NET 客户端库:将文件上传到文档中心 - Microsoft Graph .NET Client Library: Upload file to document center Microsoft图形的InvalidAuthenticationToken错误 - InvalidAuthenticationToken error with microsoft graph Microsoft Graph - GraphServiceClient 错误 - Microsoft Graph - GraphServiceClient error 使用 Microsoft Graph 将 C# Asp.Net Core 中的文件上传到 Sharepoint/OneDrive,无需用户交互 - Upload a file in C# Asp.Net Core to Sharepoint/OneDrive using Microsoft Graph without user interaction Microsoft Graph - .NET SDK - OneDrive 大文件上传(&gt; 4MB 大小) - Microsoft Graph - .NET SDK - OneDrive Large File Upload (> 4MB in size) 使用 Microsoft Graph 将新文件上传到 onedrive c# asp.net - Upload new file to onedrive using microsoft graph c# asp.net Microsoft Graph,上传到共享点驱动器:调用方未通过身份验证 - Microsoft Graph, upload to sharepoint drive: The caller is not authenticated
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM