Solved

stringbuilder out of memory error

Posted on 2014-07-29
5
72 Views
Last Modified: 2016-06-21
I have some code that gets documents from a sharepoint library via a webservice.
During testing I attempted to get a 220mb document and get this error
An error has occurred: System.OutOfMemoryException: Exception of type 'System.OutOfMemoryException' was thrown. at System.Text.StringBuilder.ToString() at System.Xml.XmlTextReaderImpl.ParseText()

Is there anything I can do to get around this?
This is the code I am using.

 public void CopyFile()
        {
            string webUrl = "http://xxx";
            FileCopy.Copy copyService = new FileCopy.Copy();
            copyService.Url = webUrl +"/_vti_bin/copy.asmx";
            NetworkCredential m_credentials = new NetworkCredential("x", "x", "x");
                 copyService.Credentials = m_credentials;

            //Source and Destination Document URLs
             string sourceUrl = "http://x/Documents/1784.pdf";
             string destinationUrl = "C:\\Documents\\1784.pdf";

            //Variables for Reading metadata’s of a document
             FileCopy.FieldInformation fieldInfo = new FileCopy.FieldInformation();
             FileCopy.FieldInformation[] fieldInfoArray = { fieldInfo };
             FileCopy.CopyResult cResult1 = new FileCopy.CopyResult();
             FileCopy.CopyResult cResult2 = new FileCopy.CopyResult();
             FileCopy.CopyResult[] cResultArray = { cResult1, cResult2 };

        

            //Receive a Document Contents  into Byte array (filecontents)
             byte[] fileContent; // no need to initialize the GetItem takes care of that.
             copyService.GetItem(sourceUrl, out fieldInfoArray, out fileContent);

            //Create a new file and write contents to that document
             FileStream fStream = new FileStream(destinationUrl, FileMode.Create, FileAccess.ReadWrite);
            fStream.Write(fileContent, 0, fileContent.Length);
            fStream.Close();
        }
    }

Open in new window

0
Comment
Question by:QPR
  • 2
  • 2
5 Comments
 
LVL 40
ID: 40228342
What line triggers the error.

Since the error happens in a StringBuilder and there is none in your code, it is probably triggered inside of something you are calling.

What are that FileCopy and class that you are using? If this is one of your classes, look in there. If it is something from a third party, get in contact with them.
0
 
LVL 29

Author Comment

by:QPR
ID: 40228350
It is failing on this line
copyService.GetItem(sourceUrl, out fieldInfoArray, out fileContent);

GetItem is a sharepoint webservice.
Your post has led me in the right direction tough.
Seems there is a 50mb limt lthough this can be configured up to the 2GB limit(SQL)
http://www.codeproject.com/Articles/63861/Limitations-of-SharePoint-Web-Services

If you look at the section called "Issues with Downloading Files" you will see what I am seeing.
It talks of single buffers and alternative methods but it is getting a bit beyond my expertise.
Would this webclient class be easy enough to ut in m coe and replace te SP built in webservice?
0
 
LVL 29

Accepted Solution

by:
QPR earned 0 total points
ID: 40228353
Nevermind I simply replaced all of the above code with this
 string url = "http://xx/Documents/1784.pdf";
            string localfileName = @"C:\Documents\1784.pdf";
            // Create a new WebClient instance.
            WebClient myWC = new WebClient();
            // Set the credentials
            myWC.Credentials = new NetworkCredential("x", "x", "x");
            // Download the file and save it into the local path.
            myWC.DownloadFile(url, localfileName); 

Open in new window


and it works perfectly! The limitation was SP based.
Anything I should know about the code I am using? problems/overheads/alterations?
0
 
LVL 40
ID: 41656660
Your post has led me in the right direction tough.

Isn't this enough?
0

Featured Post

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

Problem Hi all,    While many today have fast Internet connection, there are many still who do not, or are connecting through devices with a slower connect, so light web pages and fast load times are still popular.    If your ASP.NET page …
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

743 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now