BizTalk自定义管道组件System.OutOfMemoryException

时间:2012-02-13 22:59:31

标签: biztalk custom-component biztalk-2010

我目前正在开发一个BizTalk自定义发送管道,它接受一个xml文件并将其转换为Excel。不幸的是,在部署管道之后,我收到了System.OutOfMemoryException。我已经包含了IComponent接口的执行方法的代码。欢迎提出所有建议。

public Microsoft.BizTalk.Message.Interop.IBaseMessage Execute(IPipelineContext pContext, Microsoft.BizTalk.Message.Interop.IBaseMessage inmsg)
    {

        MemoryStream outMemStream = new MemoryStream();
        try
        {

            if (inmsg.BodyPart.Data != null)
            {
                // Read the source message coming from the messaging engine and convert it to memory stream 
                byte[] buffer = new byte[16 * 1024];
                using (MemoryStream ms = new MemoryStream())
                {
                    int read;
                    while ((read = inmsg.BodyPart.Data.Read(buffer, 0, buffer.Length)) > 0)
                    {
                        ms.Write(buffer, 0, read);
                    }
                    buffer = ms.ToArray();
                }

                if (buffer != null)
                {
                    var binaryWriter = new BinaryWriter(outMemStream);
                    binaryWriter.Write(buffer);
                }

                OpenXMLOffice oOffice = new OpenXMLOffice();
                outMemStream.Position = 0;
                oOffice.XMLToExcel(outMemStream, TemporaryFileLocation);
                inmsg.BodyPart.Data.Position = 0;
                inmsg.BodyPart.Data = outMemStream;
                pContext.ResourceTracker.AddResource(outMemStream);
            }

            return inmsg;
        }
        catch (Exception ex)
        {
            throw new ApplicationException(String.Format("Error converting XML to Excel:{0} - Stack Trace: {1}", ex.Message, ex.StackTrace));
        }
    }

以下是收到的最新错误:

Log Name:      Application
Source:        BizTalk Server
Date:          2/14/2012 9:29:00 AM
Event ID:      5754
Task Category: BizTalk Server
Level:         Error
Keywords:      Classic
User:          N/A
Computer:      IASDev-PC
Description:
A message sent to adapter "FILE" on send port "ExcelSendPort" with URI "C:\SeleneFTPFile\Excel\%MessageID%.xml" is suspended. 
 Error details: There was a failure executing the send pipeline: "IAS.SeleneFTPFile.ExcelEncodePipeline, IAS.SeleneFTPFile, Version=1.0.0.0, Culture=neutral, PublicKeyToken=2add433e7764165f" Source: "Excel File Encoder" Send Port: "ExcelSendPort" URI: "C:\SeleneFTPFile\Excel\%MessageID%.xml" Reason: Error converting XML to Excel:Exception of type 'System.OutOfMemoryException' was thrown. - Stack Trace:    at System.IO.MemoryStream.set_Capacity(Int32 value)
   at System.IO.MemoryStream.EnsureCapacity(Int32 value)
   at System.IO.MemoryStream.Write(Byte[] buffer, Int32 offset, Int32 count)
   at IAS.SeleneFTPFile.Components.ExcelPipeline.EncodeExcel.Execute(IPipelineContext pContext, IBaseMessage inmsg)  
 MessageId:  {ED37CDD1-EF0C-46E7-9519-061AF3D4F8A4}
 InstanceID: {B0E448B3-3DAD-4E52-8F87-07C5D5AA5224}

3 个答案:

答案 0 :(得分:1)

您可以尝试预先分配MemoryStream缓冲区( as suggested here )。错误消息指出在执行缓冲写入(ms.Write(buffer, 0, read);)时无法为该行分配足够的内存。

using (MemoryStream ms = new MemoryStream(buffer.Length))

您遇到的另一个问题是,buffer可能会溢出 - 在MemoryStream写入( ms.Write(buffer, 0, read) )时会产生此确切错误。

byte[] buffer = new byte[2 * 1024 * 1024]; // try increasing to 2MB buffer

答案 1 :(得分:1)

参考此链接......这可能对您有帮助....

http://blog.pearltechnology.com/biztalk-pipeline-out-of-memory/

答案 2 :(得分:1)

即使我面临同样的问题。我可以发现原因是流位置没有提前并且inmsg.BodyPart.Data流保持为0,即使在执行以下语句之后:

read = inmsg.BodyPart.Data.Read(buffer, 0, buffer.Length)