Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

File.WriteAllBytes causes error "Insufficient system resources exist to complete the requested service"

I have a standard SOAP webservice with a WebMethod which accepts a byte array and then performs a

[WebMethod(true)]
WriteFile(byte[] Data, string FilePath)
{

    File.WriteAllBytes(FilePath, Data);
}

If this process is passed a large file, e.g. 2 meg it is bombing out with the following error message:

Insufficient system resources exist to complete the requested service

Looking at the stack trace i'm getting:

  • System.IO.File.WriteAllBytes
  • System.IO.FileStream.Write
  • System.IO.FileStream.WriteCore
  • System.IO.__Error.WinIOError
  • System.IO.IOException: Insufficient system resources exist to complete therequested service

I've tried all the obvious things such as setting the maxrequestlength and executing timeout to more realistic settings:

<httpRuntime maxRequestLength="409600" executionTimeout="900"/>

It still seems to fail over with the above. If you send a smaller file it saves to disk fine.. So it's either file size or time that's the issue.

Does anyone know of anything else i can do to sort this out?

Thanks

Dave

like image 687
CraftyFella Avatar asked Jun 15 '09 10:06

CraftyFella


2 Answers

I know you are not reaching this size of file but also be aware that File.WriteAllBytes has a limit of 64mb when writing to network paths - see this connect issue

like image 135
Richard Avatar answered Sep 29 '22 13:09

Richard


I was receiving a similar error message when using File.WriteAllBytes and changed my code to use a FileStream as in the example below. Based on the comments of others, my guess is that FileStream has a smaller memory impact.

        using (FileStream stream = new FileStream(FilePath, FileMode.Create, FileAccess.ReadWrite))
        {
            stream.Write(Data, 0, Data.Length);
            stream.Close();
        }
like image 39
Scott Avatar answered Sep 29 '22 15:09

Scott