BizTalk 2010カスタムパイプラインコンポーネントはバイナリを返します
-
14-12-2019 - |
質問
私は複雑なExcelスプレッドシートをXMLに変換するカスタムパイプラインコンポーネントを作成しました。変換はうまく機能し、チェックするデータを書き出すことができます。ただし、このデータをINMSGまたは新しいメッセージのBodypart.Dataの部分に割り当てると、常にルーティングの失敗が発生します。管理コンソールのメッセージを見ると、本文にはバイナリデータが含まれている(オリジナルのExcelを想定しています)。下のスクリーンショットを参照してください。私はこれを行う多数のチュートリアルとさまざまな方法がありましたが、常に同じ結果が得られました。
現在のコードは次のとおりです。
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;
}
. 解決
これはメッセージを書き戻すサンプルです:
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;
}
.
ASCISTreamは、ストリームを読むためにこのような方法を使用しました:
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;
}
. 他のヒント
まず最初にメモリストリームロジック周辺のコンポーネントへのログ記録を追加します.XMLバージョンが正しいことを確認できるように、ファイルシステムにファイルを書き出すことができます。また、BizTalkプロセスに添付し、デバッグを簡単にするコンポーネントのコードを実行することもできます。
メモリストリームの使用をより基本的なカスタムストリームに切り替えることを試みます。PipelineコンポーネントのBizTalk SDKサンプルでは、カスタムストリームの例がいくつかあります。ストリームを書き込むだけで、ストリームサンプルをカスタマイズする必要があります。例を投稿することができます。だから、最初に上記の追加の診断を行います。
ありがとう、
所属していません StackOverflow