I'm working on a piece of software that uploads files into a given sharepoint directory from a remote location (ASP.NET MVC). I have everything working properly, uploading with CSOM when files are under 1.5MB, uploading with REST when files are larger.

This works great (based on OAuth Tokens), and I have zero issues on my local dev environment - production environment is causing issues.

Sharepoint 2013 (O365 in this case) randomly kills the file transfer connection. This happens after 3-4 transfers, sometimes it doesn't at all, sometimes it throws an exception right after the first transfer.

System.IO.IOException: {"Unable to write data to the transport connection: An existing connection was forcibly closed by the remote host."}

I dug deeper, and basically I'm getting

Socket Error 10054: Connection reset by peer.

NOTE: C# CSOM transfers work fine both locally and remote.

We are hosting this web application on Azure (WAWS).

            endpointRequest = (HttpWebRequest)WebRequest.Create(String.Format(
                "{0}_api/web/GetFolderByServerRelativeUrl('{1}')/Files/Add(url='{2}', overwrite=true)",
            Trace.TraceInformation("Uploading {0} with REST", uploadName);
            endpointRequest.Timeout = Timeout.Infinite;
            endpointRequest.Method = "POST";
            endpointRequest.Accept = "application/atom+xml";
            endpointRequest.Headers.Add("Authorization", "Bearer " + GetAccessToken());
            //endpointRequest.ContinueTimeout = 10000;
            //endpointRequest.Proxy = null;
            //endpointRequest.ReadWriteTimeout = 10000;
            endpointRequest.AllowWriteStreamBuffering = false;
            var blobStream = GetBlobStream();
            endpointRequest.ContentLength = GetBlobSize();

            using (var requestStream = endpointRequest.GetRequestStream())
                // Send the file as body request. 

            using (var responseStream = endpointRequest.GetResponse())
                using (var reader = new StreamReader(responseStream.GetResponseStream()))
                    Trace.TraceInformation("Sharepoint response: {0}", reader.ReadToEnd());
            Trace.TraceInformation("REST UPLOAD OF {0} FINISHED", FileName);

Note: The commented options for the HttpWebRequest are all things I tried, to no avail.

Does anyone have any idea what is wrong here, and what the reason might be SharePoint kills the connection?

  • What is the maximum file size you are targeting in your application? – Garima Jun 24 '14 at 4:19
  • For CSOM based calls the max is 1.5 Mb, for REST based calls this is 500 mb. – Las3r Jun 25 '14 at 8:48

I have found a solution: I think something was wrong with the environment settings on Windows Azure Web Sites. I've changed the HttpWebRequest object to an HttpClient object (which internally uses the HttpWebRequest) and my problems went away. My new code:

 using (var client = new HttpClient())
                client.DefaultRequestHeaders.Add("Accept", "application/json;odata=verbose");
                client.Timeout = Timeout.InfiniteTimeSpan;
                client.DefaultRequestHeaders.Add("Authorization", "Bearer " + GetAccessToken());
                using (var blobStream = GetBlobStream())
                    var response = await client.PostAsync(sharepointTargetUri, new StreamContent(blobStream));
                    Trace.TraceInformation(await response.Content.ReadAsStringAsync());
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.