BizTalk 自定义管道组件 System.OutOfMemoryException

发布于 2025-01-05 09:10:53 字数 3030 浏览 1 评论 0原文

我目前正在开发一个 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}

I'm currently developing a BizTalk Custom Send Pipeline that accepts an xml file and converts it to Excel. Unfortunately, after deploying the pipeline, I'm receiving a System.OutOfMemoryException. I've included the code for Execute Method of IComponent interface. All suggestions are welcome.

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));
        }
    }

Here is the most recent error received:

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}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

森林很绿却致人迷途 2025-01-12 09:10:53

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

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

您遇到的另一个问题是您的缓冲区可能会溢出 - 当 MemoryStream 被写入 (ms.Write(buffer, 0, read))。

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

You could try pre-allocating the MemoryStream buffer (as suggested here). The error message states that it can't allocate enough memory for the line when performing the buffered write (ms.Write(buffer, 0, read);).

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

The other issue you have is that your buffer could overflow - producing this exact error when the MemoryStream is written to (ms.Write(buffer, 0, read)).

byte[] buffer = new byte[2 * 1024 * 1024]; // try increasing to 2MB buffer
放肆 2025-01-12 09:10:53

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

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

Even I face the same issue. I can find that the reason is the stream position does not advance and remains at 0 for the inmsg.BodyPart.Data stream, even after the below statement executes:

read = inmsg.BodyPart.Data.Read(buffer, 0, buffer.Length)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文