2012-03-14 68 views
0

我已经创建了一个自定义管道组件,它将复杂的Excel电子表格转换为XML。转换工作正常,我可以写出要检查的数据。但是,当我将这些数据分配给inMsg的BodyPart.Data部分或新消息时,我总是遇到路由失败。当我查看管理控制台中的消息时,看起来主体包含二进制数据(我假定最初的excel)而不是我已经分配的XML - 请参见下面的屏幕截图。我遵循了许多教程和许多不同的方式来做到这一点,但总是得到相同的结果。Biztalk 2010自定义管道组件返回二进制

Binary Returned

我当前的代码是:

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

回答

1

这里是写消息返回的样品:

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使用这样的方法来读取流:

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; 
     } 
+0

不错,谢谢你的帮助:) – RedEyedMonster 2012-03-14 20:41:34

1

我首先到您的组件添加更多的日志记录周围的MemoryStream的逻辑 - 也许写文件输出到文件系统,以便可以使确定Xml版本是正确的。您还可以附加到BizTalk进程,并逐步完成组件的代码,从而使调试变得更容易。

我会尝试将MemoryStream的使用切换到为您写入字节的更基本的自定义流。在管道组件的BizTalk SDK示例中,有一些自定义流的示例。您将不得不自定义流样本,以便只写流。我可以发布一个例子。所以先做一下上面的附加诊断。

谢谢,

+0

谢谢,我已经完成了诊断,只是删除了他们的发布 - 我可以确认我写入数据的XML格式正确。 – RedEyedMonster 2012-03-14 15:20:19