Restsharp: Out of Memory issue when uploading large files

Created on 13 Oct 2016  ·  7Comments  ·  Source: restsharp/RestSharp

When we are uploading the large files with RestSharp, we randomly get some OutOfMemory issues. This happens because RestSharp is using HttpWebRequest behind the scene, and the default value for AllowWriteStreamBuffering is true. This mean that the whole file are loaded in memory when doing the query, in case there are some redirections to be able to replay it. But If we are uploading a 500 mb file, its an issue.

What we would need is to be able to set the AllowWriteStreamBuffering to false, but there is no way right now to alter the AllowWriteStreamBuffering in the ConfigureWebRequest.

Something like this would be sufficient for our need:

var request = new RestRequest(Method.POST); request.AllowWriteStreamBuffering = false; request.Files.Add(new FileParameter // ...

help wanted good first issue

Most helpful comment

I encounter same issue and don't how to deal with

All 7 comments

Note : To be able to test the memory pressure of .Net vs RestSharp 105.2.3, here is how we tested it as there is an unreleased bug fix when using the .AddFile:

// In the current implementation of RestSharp, there is an issue when using AddFile with a writer.
// It doesn't set the ContentLength, which break the queries with System.Net.ProtocolViolationException as we are not uploading 
// the expected amount of bytes. It's fixed in an yet unreleased version, so we are using the Files.Add manually for now.
var request = new RestRequest(Method.POST);
request.Files.Add(new FileParameter
{
                Name = "file",
                Writer = outStream => inStream.CopyTo(outStream),
                FileName = fileName,
                ContentLength = inStream.Length,
});

Any update on this? I'm having exactly the same issue and it seems to me reasonable to expose AllowWriteStreamBuffering either in RestClient or RestRequest. As far as I know there is no workaround. My use case it to send in parallel 100MB files and if AllowWriteStreamBuffering is true it will throw out of memory exception very often.

I encounter same issue and don't how to deal with

is it solved yet ????

@AgentCoolDevil you want to help? Do a pull request.

Is anyone still getting this error on the latest version?

As @yvesmh rightfully mentioned in #1213, this issue can be solved by disabling the WebRequest write stream buffering. You can use the web request configuration on IRestClient to handle such cases.

client.ConfigureWebRequest(x => x.AllowWriteStreamBuffering = false);

Was this page helpful?
0 / 5 - 0 ratings