'OneDrive API CreateUploadSession Error Status Code 0 sometimes only
This is a new method I'm working on. It's been working on my local but now that it's released to our Dev and Staging environments it's failing randomly (can't find a clear reason why, sometimes it works on dev, then I go to staging and it doesn't work, or I try Staging first and it's working, then dev isn't). I'm trying to upload a file to OneDrive and then create a share link. The method that is failing is the one to upload the file. I'm copy and pasting my method below, I have a super over-the-top Exception handling because I'm trying to find anything useful in it. The error seems very generic and I am not getting the response headers I was expecting to see. Any ideas? I might have to go with a different technology if I can't get this figured out.
public async Task<string> UploadFile(GraphServiceClient graphServiceClient, string filePath, string fileName)
{
Guid requestId = Guid.NewGuid();
try
{
// Based on https://docs.microsoft.com/en-us/graph/sdks/large-file-upload
using (var fileStream = System.IO.File.OpenRead(filePath))
{
// Use properties to specify the conflict behavior
// in this case, replace
var uploadProps = new DriveItemUploadableProperties
{
ODataType = null,
AdditionalData = new Dictionary<string, object>
{
{ "@microsoft.graph.conflictBehavior", "replace" }
}
};
// Create the upload session
// itemPath does not need to be a path to an existing item
var uploadSession = await graphServiceClient.Sites[ConfigurationManager.AppSettings.Get("OneDriveSiteId")].Drive.Root
.ItemWithPath($"/{fileName}")
.CreateUploadSession(uploadProps)
.Request()
.Header("client-request-id", requestId.ToString())
.PostAsync();
// Max slice size must be a multiple of 320 KiB
int maxSliceSize = 320 * 1024;
var fileUploadTask = new LargeFileUploadTask<DriveItem>(uploadSession, fileStream, maxSliceSize);
// Upload the file
var uploadResult = await fileUploadTask.UploadAsync();
// If Successful, return the file Id
if (uploadResult.UploadSucceeded)
{
return uploadResult.ItemResponse.Id.ToString();
}
else
{
throw new Exception("Failed to upload file");
}
}
}
catch (ServiceException ex)
{
string body = "";
try
{
body = $"ServiceException <br />";
body += $"requestId: {requestId} <br />";
body += $"filePath: {filePath} <br />";
body += $"fileName: {fileName} <br />";
if (ex != null)
{
if (!String.IsNullOrWhiteSpace(ex.Message))
{
body += $"ex.Message: {ex.Message}<br />";
}
body += $"ex.StatusCode: {ex.StatusCode}<br />";
if (!String.IsNullOrWhiteSpace(ex.RawResponseBody))
{
body += $"ex.RawResponseBody: {ex.RawResponseBody}<br />";
}
body += $"ex.HResult: {ex.HResult}<br />";
if (ex.ResponseHeaders != null && ex.ResponseHeaders.Any())
{
int i = 1;
foreach (var header in ex.ResponseHeaders)
{
body += $"ex.ResponseHeaders[{i}]: {header.Key}<br />";
i++;
}
}
if (ex.Error != null)
{
if (!String.IsNullOrWhiteSpace(ex.Error.Code))
{
body += $"ex.Error.Code: {ex.Error.Code}<br />";
}
if (!String.IsNullOrWhiteSpace(ex.Error.Message))
{
body += $"ex.Error.Message: {ex.Error.Message}<br />";
}
if (!String.IsNullOrWhiteSpace(ex.Error.Target))
{
body += $"ex.Error.Target: {ex.Error.Target}<br />";
}
if (!String.IsNullOrWhiteSpace(ex.Error.ClientRequestId))
{
body += $"ex.Error.ClientRequestId: {ex.Error.ClientRequestId}<br />";
}
if (ex.Error.AdditionalData != null && ex.Error.AdditionalData.Any(a => a.Value != null))
{
int i = 1;
foreach (var erAd in ex.Error.AdditionalData.Where(a => a.Value != null))
{
body += $"ex.Error.AdditionalData[{i}]: {erAd.Key} {erAd.Value}<br />";
i++;
}
}
if (ex.Error.Details != null && ex.Error.Details.Any())
{
int i = 1;
foreach (var erD in ex.Error.Details.Where(a => a.Code != null && a.Message != null))
{
body += $"ex.Error.Details[{i}]: {erD.Code} {erD.Message}<br />";
i++;
}
}
}
}
}
catch (Exception)
{
body = "Failed to build error body";
}
ErrorHandler.SendErrorMessage(typeof(FundingSourcePackageLinkUtility).Name, "UploadFile", body, ex);
throw;
}
catch (Exception ex)
{
ErrorHandler.SendErrorMessage(typeof(FundingSourcePackageLinkUtility).Name, "UploadFile",
$"requestId: {requestId} <br /> filePath: {filePath} <br /> fileName: {fileName} <br />", ex);
throw;
}
}
Here's the Error Log:
Class Name: FundingSourcePackageLinkUtility
Method Name: UploadFile
Description: ServiceException
requestId: 0c646c04-5399-4b18-9bdd-feb75db27569
filePath: C:\file.pdf
fileName: file.pdf
ex.Message: Code: generalException Message: An error occurred sending the request.
ex.StatusCode: 0
ex.HResult: -2146233088
ex.Error.Code: generalException
ex.Error.Message: An error occurred sending the request.
--- Exception ---
Message: Status Code: 0 Microsoft.Graph.ServiceException: Code: generalException Message: An error occurred sending the request. ---> System.Net.Http.HttpRequestException: An error occurred while sending the request. ---> System.Net.WebException: The underlying connection was closed: An unexpected error occurred on a send. ---> System.IO.IOException: Unable to read data from the transport connection: An existing connection was forcibly closed by the remote host. ---> System.Net.Sockets.SocketException: An existing connection was forcibly closed by the remote host at System.Net.Sockets.Socket.EndReceive(IAsyncResult asyncResult) at System.Net.Sockets.NetworkStream.EndRead(IAsyncResult asyncResult) --- End of inner exception stack trace --- at System.Net.TlsStream.EndWrite(IAsyncResult asyncResult) at System.Net.PooledStream.EndWrite(IAsyncResult asyncResult) at System.Net.ConnectStream.WriteHeadersCallback(IAsyncResult ar) --- End of inner exception stack trace --- at System.Net.HttpWebRequest.EndGetRequestStream(IAsyncResult asyncResult, TransportContext& context) at System.Net.Http.HttpClientHandler.GetRequestStreamCallback(IAsyncResult ar) --- End of inner exception stack trace --- at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) at Microsoft.Graph.RedirectHandler.d__6.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.RetryHandler.d__9.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.CompressionHandler.d__2.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.AuthenticationHandler.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.SimpleHttpProvider.d__13.MoveNext() --- End of inner exception stack trace --- at Microsoft.Graph.SimpleHttpProvider.d__13.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.SimpleHttpProvider.d__10.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.UploadSliceRequest1.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.UploadSliceRequest1.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.LargeFileUploadTask1.d__14.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.LargeFileUploadTask1.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 System.Runtime.CompilerServices.TaskAwaiter`1.GetResult() at CreditBuilderApi.Application.FundingSourcePackageLinkUtility.d__5.MoveNext() in C:\Data\Git\WebApps\CRMCreditBuilder\CreditBuilderApi.Application\FundingSourcePackageLinkUtility.cs:line 310
Source: Microsoft.Graph.Core
StackTrace: at Microsoft.Graph.SimpleHttpProvider.d__13.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.SimpleHttpProvider.d__10.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.UploadSliceRequest1.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.UploadSliceRequest1.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.LargeFileUploadTask1.d__14.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.LargeFileUploadTask1.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 System.Runtime.CompilerServices.TaskAwaiter`1.GetResult() at CreditBuilderApi.Application.FundingSourcePackageLinkUtility.d__5.MoveNext() in C:\Data\Git\WebApps\CRMCreditBuilder\CreditBuilderApi.Application\FundingSourcePackageLinkUtility.cs:line 310
An error occurred while sending the request.
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) at Microsoft.Graph.RedirectHandler.d__6.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.RetryHandler.d__9.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.CompressionHandler.d__2.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.AuthenticationHandler.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.SimpleHttpProvider.d__13.MoveNext()
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
