none
Exceptions while reading some tables RRS feed

  • Question

  • I'm reading table BSAD using the following code it has 33,000 rows and 173 columns, I'm getting the following exception

     

    Exception:

    Exception of type 'System.OutOfMemoryException' was thrown.

     

     

    Stack Trace:

    at System.String.GetStringForStringBuilder(String value, Int32 startIndex, Int32 length, Int32 capacity)

    at System.Text.StringBuilder.GetNewString(String currentString, Int32 requiredLength)

    at System.Text.StringBuilder.Append(String value)

    at System.IO.StringWriter.Write(String value)

    at System.Xml.XmlTextEncoder.Write(String text)

    at System.Xml.XmlTextWriter.WriteString(String text)

    at System.Xml.XmlText.WriteTo(XmlWriter w)

    at System.Xml.XmlElement.WriteContentTo(XmlWriter w)

    at System.Xml.XmlElement.WriteTo(XmlWriter w)

    at System.Xml.XmlElement.WriteContentTo(XmlWriter w)

    at System.Xml.XmlElement.WriteTo(XmlWriter w)

    at System.Xml.XmlElement.WriteContentTo(XmlWriter w)

    at System.Xml.XmlElement.WriteTo(XmlWriter w)

    at System.Xml.XmlElement.WriteContentTo(XmlWriter w)

    at System.Xml.XmlElement.WriteTo(XmlWriter w)

    at System.Xml.XmlDocument.WriteContentTo(XmlWriter xw)

    at System.Xml.XmlDocument.WriteTo(XmlWriter w)

    at System.Xml.XmlNode.get_OuterXml()

     

    Using the following code to read the table

    string inputXml = "<RFC_READ_TABLE xmlns=\"http://Microsoft.LobServices.Sap/2007/03/Rfc/\">" +

    "<DELIMITER>;</DELIMITER> <NO_DATA></NO_DATA> <QUERY_TABLE>BSAD</QUERY_TABLE> <ROWCOUNT>0</ROWCOUNT><ROWSKIPS>0</ROWSKIPS></RFC_READ_TABLE>";

    //create an XML reader from the input XML

    XmlReader reader = XmlReader.Create(new MemoryStream(Encoding.Default.GetBytes(inputXml)));

    //create a WCF message from our XML reader

    System.ServiceModel.Channels.Message inputMessge = System.ServiceModel.Channels.Message.CreateMessage(System.ServiceModel.Channels.MessageVersion.Soap11, "http://Microsoft.LobServices.Sap/2007/03/Rfc/RFC_READ_TABLE", reader);

    //send the message to SAP and obtain a reply

    System.ServiceModel.Channels.Message replyMessage = SAPConnector.irc.Request(inputMessge);

    //create a new XML document

    XmlDocument doc = new XmlDocument();

    //load the XML document with the XML reader from the output message received from SAP

    doc.Load(replyMessage.GetReaderAtBodyContents());

    string strReply = replyMessage.GetReaderAtBodyContents().ReadString();

    //write the output XML to a file C:\\out.xml

    string ResponseXml = doc.OuterXml;

    Sunday, June 29, 2008 2:05 PM

Answers

  • Firstly, you're trying to load a LOT of data into memory via the call to the RFC.

    Secondly, you're loading it into a XmlDocument.

     

    You can try doing this - immediately after obtaining replyMesssage (after the Request call), write all the data out to file.

     

    using (XmlWriter w = XmlWriter.Create("C:\\out.xml"))

    {

      using (XmlDictionaryWriter dw = XmlDictionaryWriter.CreateDictionaryWriter(w))

      {

        replyMessage.WriteBodyContents(dw);

        replyMessage.Close();

      }

    }

     

    This might not necessarily make your OOM exception go away, but it should reduce the memory consumed by your application. Once the data is written into a file and the replyMessage closed, you can then read it back and work with that data.

     

    Monday, June 30, 2008 8:22 AM