Microsoft Graph - Random error downloading files

Miguel Viña 51 Reputation points
2021-04-06T13:27:24.437+00:00

Hi,

I work for a company in which we develop a Desktop Application that uses C# Microsoft Graph Library to download files from the OneDrive Cloud.
For the last weeks a few of our costumers are randomly getting this error in the application when they try to download files through the API:

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.EndGetResponse(IAsyncResult asyncResult)
at System.Net.Http.HttpClientHandler.GetResponseCallback(IAsyncResult ar)

This error occurs with random files and it gets fixed after a while, could be two minutes or two hours. We tried to find some kind of pattern on the files but we couldn't find anything. Also, we didn't make any change in the communication with OneDrive through the API for the last months so we don't understand what's happening and why we are getting this error now.

We have been using Microsoft Graph for a while now and this is the first time we are getting this error, we are starting to think that could be some problem in OneDrive servers although that seems to be unlikely because nobody else seems to have reported any issue recently apart from us.

We've replicated the error in different computers so it doesn't seems to be a problem with a particular workstation or with the local network.

We are currently using version 1.12 of Microsoft.Graph. Any help would be much appreciated cause I'm completely stuck with this.

Microsoft Graph
Microsoft Graph
A Microsoft programmability model that exposes REST APIs and client libraries to access data on Microsoft 365 services.
13,704 questions
{count} votes

Accepted answer
  1. César 236 Reputation points
    2021-04-21T14:01:10.13+00:00

    UPDATED: This problem shows only on Framework versions lower than 4.6 and only if TLS 1.2 is not enabled. The workaround below doesn't make sense if you enable TLS 1.2 or you update your App to Framework 4.6 or higher.

    Even though we haven't found the cause for the failure, this workaround is tested and it works. It is in the comments, but I think is more useful as an aswer.

    It is based on the fact that when the API is failing, download from OneDrive still works:

    try
    {
     //download file using the API
    }
    catch (ServiceException exception)
    {
     var key = "@microsoft.graph.downloadUrl";
     if (exception.Error?.Code == "generalException"
     && driveItem.AdditionalData != null && driveItem.AdditionalData.ContainsKey(key))
     {
     var downloadUrl = "" + driveItem.AdditionalData[key];
    
     //If you are using a Framework version lower than 4.6, you have to enable TLS 1.2
     using (HttpClient httpClient = new HttpClient())
     {
     var response = await httpClient.GetAsync(downloadUrl, default(System.Threading.CancellationToken));
     return await response.Content.ReadAsByteArrayAsync();
     }
     }
     else
     throw;
    }
    
    1 person found this answer helpful.

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.