首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Biztalk 2010自定义管道组件返回二进制

Biztalk 2010自定义管道组件返回二进制
EN

Stack Overflow用户
提问于 2012-03-14 23:00:57
回答 2查看 3.1K关注 0票数 0

我已经创建了一个自定义管道组件,它可以将复杂的excel电子表格转换为XML。转换工作正常,我可以写出要检查的数据。但是,当我将此数据分配给inMsg的BodyPart.Data部分或新消息时,我总是收到路由失败。当我在管理控制台中查看该消息时,它显示正文包含的是二进制数据(我假设是原始的excel),而不是我分配的XML -参见下面的屏幕截图。我遵循了许多教程和许多不同的方法,但总是得到相同的结果。

我当前的代码是:

代码语言:javascript
复制
public Microsoft.BizTalk.Message.Interop.IBaseMessage Execute(Microsoft.BizTalk.Component.Interop.IPipelineContext pc, Microsoft.BizTalk.Message.Interop.IBaseMessage inmsg)
    {


        //make sure we have something
        if (inmsg == null || inmsg.BodyPart == null || inmsg.BodyPart.Data == null)
        {
            throw new ArgumentNullException("inmsg");
        }

        IBaseMessagePart bodyPart = inmsg.BodyPart;

        //create a temporary directory
        const string tempDir = @"C:\test\excel";
        if (!Directory.Exists(tempDir))
        {
            Directory.CreateDirectory(tempDir);
        }

        //get the input filename
        string inputFileName = Convert.ToString(inmsg.Context.Read("ReceivedFileName", "http://schemas.microsoft.com/BizTalk/2003/file-properties"));


        swTemp.WriteLine("inputFileName: " + inputFileName);

        //set path to write excel file
        string excelPath = tempDir + @"\" + Path.GetFileName(inputFileName);
        swTemp.WriteLine("excelPath: " + excelPath);

        //write the excel file to a temporary folder  
        bodyPart = inmsg.BodyPart;
        Stream inboundStream = bodyPart.GetOriginalDataStream();
        Stream outFile = File.Create(excelPath);
        inboundStream.CopyTo(outFile);
        outFile.Close();

        //process excel file to return XML
        var spreadsheet = new SpreadSheet();
        string strXmlOut = spreadsheet.ProcessWorkbook(excelPath);

        //now build an XML doc to hold this data
        XmlDocument xDoc = new XmlDocument();
        xDoc.LoadXml(strXmlOut);

        XmlDocument finalMsg = new XmlDocument();
        XmlElement xEle;
        xEle = finalMsg.CreateElement("ns0", "BizTalk_Test_Amey_Pipeline.textXML",
                                      "http://tempuri.org/INT018_Workbook.xsd");
        finalMsg.AppendChild(xEle);

        finalMsg.FirstChild.InnerXml = xDoc.FirstChild.InnerXml;

        //write xml to memory stream
        swTemp.WriteLine("Write xml to memory stream");
        MemoryStream streamXmlOut = new MemoryStream();
        finalMsg.Save(streamXmlOut);
        streamXmlOut.Position = 0;


        inmsg.BodyPart.Data = streamXmlOut;
        pc.ResourceTracker.AddResource(streamXmlOut);


        return inmsg;
    }
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2012-03-15 04:08:32

下面是一个回写消息的示例:

代码语言:javascript
复制
    IBaseMessage Microsoft.BizTalk.Component.Interop.IComponent.Execute(IPipelineContext pContext, IBaseMessage pInMsg)
        {
            IBaseMessagePart bodyPart = pInMsg.BodyPart;          

 if (bodyPart != null)
            {      
using (Stream originalStrm = bodyPart.GetOriginalDataStream())
                {
                    byte[] changedMessage = ConvertToBytes(ret);
                    using (Stream strm = new AsciiStream(originalStrm, changedMessage, resManager))
                    {
                        // Setup the custom stream to put it back in the message.
                        bodyPart.Data = strm;
                        pContext.ResourceTracker.AddResource(strm);
                    }
                }
            }
     return pInMsg;
}

AsciiStream使用类似这样的方法来读取流:

代码语言:javascript
复制
override public int Read(byte[] buffer, int offset, int count)
        {
            int ret = 0;
            int bytesRead = 0;

            byte[] FixedData = this.changedBytes;

            if (FixedData != null)
            {
                bytesRead = count > (FixedData.Length - overallOffset) ? FixedData.Length - overallOffset : count;
                Array.Copy(FixedData, overallOffset, buffer, offset, bytesRead);


                if (FixedData.Length == (bytesRead + overallOffset))
                    this.changedBytes = null;

                // Increment the overall offset.
                overallOffset += bytesRead;
                offset += bytesRead;
                count -= bytesRead;
                ret += bytesRead;
            }

            return ret;
        }
票数 1
EN

Stack Overflow用户

发布于 2012-03-14 23:16:53

首先,我会围绕MemoryStream逻辑向您的组件添加更多日志记录--可能会将文件写出到文件系统,这样您就可以确保Xml版本是正确的。您还可以附加到BizTalk进程并逐步执行组件的代码,这将使调试变得更加容易。

我会尝试将MemoryStream的使用切换到一个更基本的自定义流,它可以为您写入字节。在管道组件的BizTalk开发工具包示例中,有一些自定义流的示例。您必须自定义流示例,以便它只写入流。我可以写一个例子。因此,首先执行上面的附加诊断。

谢谢,

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/9704474

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档