|
Home > Archive > BizTalk Server General > August 2005 > Custom Pipeline component
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
| Author |
Custom Pipeline component
|
|
|
| Following is my code to encrypt a message with GnuPG. Everything runs fine.
I see input message and encrypted message in event viewer(Check code below.
I log those into event viewer). But the output from send pipeline is empty
string. Zero bytes. What am I doing wrong?
Note:
1. This is not production code.
2. If there are any variables missing thats because I might have not
included them in the following code.
public Execute(IPipelineContext pContext, IBaseMessage pInMsg)
{
IBaseMessagePart bodyPart = pInMsg.BodyPart;
bodyPart.Data.Position = 0;
StreamReader srDataIn = new StreamReader(bodyPart.Data);
string dataIn = srDataIn.ReadToEnd();
srDataIn.Close();
//---> Logged input message into event Log
EventLog.WriteEntry("Custom Encrypt Component", dataIn);
p = new Process();
StreamWriter sw;
StreamReader sr;
StreamReader err;
ProcessStartInfo psI = new ProcessStartInfo(@"D:\GnuPG\gpg.exe", @" -e -r
xxxxxx");
psI.UseShellExecute = false;
psI.RedirectStandardInput = true;
psI.RedirectStandardOutput = true;
psI.RedirectStandardError = true;
psI.CreateNoWindow = true;
p = Process.Start(psI);
sw = p.StandardInput;
sr = p.StandardOutput;
err = p.StandardError;
sw.AutoFlush = true;
sw.Write(dataIn);
sw.Close();
// Convert the string returned from the standardOutput back into the
pipeline stream
Stream strm = new MemoryStream();
System.Text.Encoding aCode = System.Text.Encoding.UTF8;
byte[] acodeBytes;
if (p.WaitForExit(20000))
{
//---> I see this message in Log
EventLog.WriteEntry("Custom Encrypt Component", "Success in 20 Seconds");
}
else
{
EventLog.WriteEntry("Custom Encrypt Component", "Failed after 20
seconds");
p.Kill();
}
int exitCode = p.ExitCode;
if (exitCode == 0)
{
acodeBytes = aCode.GetBytes(sr.ReadToEnd());
for (int i=0;i<acodeBytes.Length-1;i++)
{
strm.WriteByte(acodeBytes[i]);
}
sr.Close();
p.Close();
}
strm.Position = 0;
bodyPart.Data = strm;
pContext.ResourceTracker.AddResource(strm);
// Used for Debug Reasons to allow reading of the results
StreamReader srDataOut = new StreamReader(strm);
string dataOut = srDataOut.ReadToEnd();
//--> I see this printing encrypted message in event viewer.
EventLog.WriteEntry("Custom Encrypt Component", dataOut);
return pInMsg;
}
| |
| Marvin Smit 2005-08-30, 8:02 am |
| Hi,
ehh, not 100% sure but,
> // Used for Debug Reasons to allow reading of the results
> StreamReader srDataOut = new StreamReader(strm);
> string dataOut = srDataOut.ReadToEnd();
isn't this going to move the pointer to the end of the underlying
stream aswell?
Hope this helps,
Marvin Smit.
On Mon, 29 Aug 2005 23:31:21 -0400, "EAI" <EAI@EAI.com> wrote:
>Following is my code to encrypt a message with GnuPG. Everything runs fine.
>I see input message and encrypted message in event viewer(Check code below.
>I log those into event viewer). But the output from send pipeline is empty
>string. Zero bytes. What am I doing wrong?
>
>
>Note:
>1. This is not production code.
>2. If there are any variables missing thats because I might have not
>included them in the following code.
>
>
>public Execute(IPipelineContext pContext, IBaseMessage pInMsg)
>{
> IBaseMessagePart bodyPart = pInMsg.BodyPart;
> bodyPart.Data.Position = 0;
> StreamReader srDataIn = new StreamReader(bodyPart.Data);
> string dataIn = srDataIn.ReadToEnd();
> srDataIn.Close();
>
> //---> Logged input message into event Log
> EventLog.WriteEntry("Custom Encrypt Component", dataIn);
>
> p = new Process();
> StreamWriter sw;
> StreamReader sr;
> StreamReader err;
>
> ProcessStartInfo psI = new ProcessStartInfo(@"D:\GnuPG\gpg.exe", @" -e -r
>xxxxxx");
>
> psI.UseShellExecute = false;
> psI.RedirectStandardInput = true;
> psI.RedirectStandardOutput = true;
> psI.RedirectStandardError = true;
> psI.CreateNoWindow = true;
>
> p = Process.Start(psI);
>
> sw = p.StandardInput;
> sr = p.StandardOutput;
> err = p.StandardError;
>
> sw.AutoFlush = true;
> sw.Write(dataIn);
> sw.Close();
>
> // Convert the string returned from the standardOutput back into the
>pipeline stream
> Stream strm = new MemoryStream();
> System.Text.Encoding aCode = System.Text.Encoding.UTF8;
> byte[] acodeBytes;
>
> if (p.WaitForExit(20000))
> {
> //---> I see this message in Log
> EventLog.WriteEntry("Custom Encrypt Component", "Success in 20 Seconds");
> }
> else
> {
> EventLog.WriteEntry("Custom Encrypt Component", "Failed after 20
>seconds");
> p.Kill();
> }
>
> int exitCode = p.ExitCode;
> if (exitCode == 0)
> {
> acodeBytes = aCode.GetBytes(sr.ReadToEnd());
> for (int i=0;i<acodeBytes.Length-1;i++)
> {
> strm.WriteByte(acodeBytes[i]);
> }
> sr.Close();
> p.Close();
> }
>
> strm.Position = 0;
> bodyPart.Data = strm;
> pContext.ResourceTracker.AddResource(strm);
>
> // Used for Debug Reasons to allow reading of the results
> StreamReader srDataOut = new StreamReader(strm);
> string dataOut = srDataOut.ReadToEnd();
>
> //--> I see this printing encrypted message in event viewer.
> EventLog.WriteEntry("Custom Encrypt Component", dataOut);
>
> return pInMsg;
>}
>
>
| |
| Jeff Lynch 2005-08-30, 8:02 am |
| I had a similar problem and needed to rewind the stream. As you can see from
my code comments I don't understand why since the bodyPart.Data "should"
return a copy of the original stream.
// Get a *copy* of the original input stream
originalStrm = bodyPart.Data;
// Rewind the stream - why? It doesn't return the stream without this, but
why?
originalStrm.Seek(0, SeekOrigin.Begin);
--
Jeff Lynch
"A BizTalk Enthusiast"
http://codebetter.com/blogs/jeff.lynch
"EAI" <EAI@EAI.com> wrote in message
news:%23G$tLNRrFHA.1204@TK2MSFTNGP15.phx.gbl...
> Following is my code to encrypt a message with GnuPG. Everything runs
> fine. I see input message and encrypted message in event viewer(Check code
> below. I log those into event viewer). But the output from send pipeline
> is empty string. Zero bytes. What am I doing wrong?
>
>
> Note:
> 1. This is not production code.
> 2. If there are any variables missing thats because I might have not
> included them in the following code.
>
>
> public Execute(IPipelineContext pContext, IBaseMessage pInMsg)
> {
> IBaseMessagePart bodyPart = pInMsg.BodyPart;
> bodyPart.Data.Position = 0;
> StreamReader srDataIn = new StreamReader(bodyPart.Data);
> string dataIn = srDataIn.ReadToEnd();
> srDataIn.Close();
>
> //---> Logged input message into event Log
> EventLog.WriteEntry("Custom Encrypt Component", dataIn);
>
> p = new Process();
> StreamWriter sw;
> StreamReader sr;
> StreamReader err;
>
> ProcessStartInfo psI = new ProcessStartInfo(@"D:\GnuPG\gpg.exe", @" -e -r
> xxxxxx");
>
> psI.UseShellExecute = false;
> psI.RedirectStandardInput = true;
> psI.RedirectStandardOutput = true;
> psI.RedirectStandardError = true;
> psI.CreateNoWindow = true;
>
> p = Process.Start(psI);
>
> sw = p.StandardInput;
> sr = p.StandardOutput;
> err = p.StandardError;
>
> sw.AutoFlush = true;
> sw.Write(dataIn);
> sw.Close();
>
> // Convert the string returned from the standardOutput back into the
> pipeline stream
> Stream strm = new MemoryStream();
> System.Text.Encoding aCode = System.Text.Encoding.UTF8;
> byte[] acodeBytes;
>
> if (p.WaitForExit(20000))
> {
> //---> I see this message in Log
> EventLog.WriteEntry("Custom Encrypt Component", "Success in 20 Seconds");
> }
> else
> {
> EventLog.WriteEntry("Custom Encrypt Component", "Failed after 20
> seconds");
> p.Kill();
> }
>
> int exitCode = p.ExitCode;
> if (exitCode == 0)
> {
> acodeBytes = aCode.GetBytes(sr.ReadToEnd());
> for (int i=0;i<acodeBytes.Length-1;i++)
> {
> strm.WriteByte(acodeBytes[i]);
> }
> sr.Close();
> p.Close();
> }
>
> strm.Position = 0;
> bodyPart.Data = strm;
> pContext.ResourceTracker.AddResource(strm);
>
> // Used for Debug Reasons to allow reading of the results
> StreamReader srDataOut = new StreamReader(strm);
> string dataOut = srDataOut.ReadToEnd();
>
> //--> I see this printing encrypted message in event viewer.
> EventLog.WriteEntry("Custom Encrypt Component", dataOut);
>
> return pInMsg;
> }
>
>
>
| |
|
| Marvin & Jeff,
Thank you for your comments. It solved my problem. I forgot to reset the
stream.
Thanks!
"Jeff Lynch" <jeff.lynch@newsgroup.nospam> wrote in message
news:O8amUKWrFHA.3452@TK2MSFTNGP14.phx.gbl...
>I had a similar problem and needed to rewind the stream. As you can see
>from my code comments I don't understand why since the bodyPart.Data
>"should" return a copy of the original stream.
>
> // Get a *copy* of the original input stream
> originalStrm = bodyPart.Data;
>
> // Rewind the stream - why? It doesn't return the stream without this, but
> why?
> originalStrm.Seek(0, SeekOrigin.Begin);
>
> --
> Jeff Lynch
> "A BizTalk Enthusiast"
> http://codebetter.com/blogs/jeff.lynch
>
>
> "EAI" <EAI@EAI.com> wrote in message
> news:%23G$tLNRrFHA.1204@TK2MSFTNGP15.phx.gbl...
>
>
|
|
|
|
|