2012-03-14 77 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

相關問題