C# 文件上载时出现Microsoft Graph HttpProvider错误
我需要使用Microsoft Graph将PDF上载到SharePoint文档库。我的代码已经成功地实现了这一点好几个月了,直到4月17日它停止工作,我们的SharePoint似乎没有改变,当然我的代码也没有改变 我有一个执行上传的功能:C# 文件上载时出现Microsoft Graph HttpProvider错误,c#,http,debugging,microsoft-graph-api,C#,Http,Debugging,Microsoft Graph Api,我需要使用Microsoft Graph将PDF上载到SharePoint文档库。我的代码已经成功地实现了这一点好几个月了,直到4月17日它停止工作,我们的SharePoint似乎没有改变,当然我的代码也没有改变 我有一个执行上传的功能: public static async Task<DriveItem> UploadLargeFile(string localFilePath, string spPath) { DriveItem uploadedFil
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;
}
导致产生错误的以下情况:
在Microsoft.Graph.HttpProvider.d_u19.MoveNext()上
我仔细检查了函数的所有输入是否有效;同样,代码在几个月内没有变化。我尝试过第三方ETL软件,能够将PDF上传到我想要的文件夹中——该软件也是使用Microsoft Graph构建的
这就是我被难倒的地方。如何进一步调试以确定HttpProvider抛出错误的确切原因?找到了此问题的修复方法。答案分两部分 如何更好地调试这样的问题(如果我下面的解决方案对您没有帮助,我是如何找到它的)
- 设置发生错误的断点
- 在Visual Studio调试选项中具有以下设置。这将允许您进入(F11)这样的库,以便更好地了解正在发生的事情。
- 运行代码直到到达断点行
- 打开调试“本地”窗口
- 点击F11进入错误行
- 展开嵌套的“内部异常”变量以获取更详细的错误
- 使用上述调试导致我出现以下错误“无法从传输连接读取数据:连接已被远程主机关闭”(远程主机为SharePoint)
- SharePoint一直在弃用旧的TLS。使用较旧的.NET框架意味着1.2可能不是代码中的默认版本。归功于:
- 只需将项目框架更新到4.7.2或更高版本,即可强制实施TLS1.2。连接现在应该被SharePoint主机接受,而不是被应用程序关闭
uploadProvider.GetChunkRequestResponseAsync(request, readBuffer, exceptions)