BizTalk Server General - receive binary file

This is Interesting: Free IT Magazines  
Home > Archive > BizTalk Server General > June 2006 > receive binary file





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 receive binary file
Jeremy Chapman

2006-06-12, 7:17 pm

I've got a file receive and send port, and am able to pass a binary file
from one port to the other in an orchestration using a message type of
xmldocument. Now I want to do something with the binary data but because
the message type is xmldocument if I try to access the message biztalk tries
to load the document and barfs because it's expecting valid xml when the
data is actually binary. How can I access the data?


BizTalkVirtuoso

2006-06-12, 7:17 pm

Jeremy,
Most likely you will need an external assembly to access the data. You
will need to instantiate a class and call a method from the
orchestration passing the message as a parameter. You should have
access to the data from the stream object. Add a few try catch
statements to be sure. Use a tool like DebugViewNT to monitor the
results.

Ben McFarlin
Microsoft BizTalk Server Consultant
www.biztalkvirtuoso.com

Matt Meleski

2006-06-12, 7:17 pm

From you orchestration you can call a .Net componet something similar to the
below:

public string GetRawMessage
(Microsoft.XLANGs.BaseTypes.XLANGMessage rawMessage)

{
System.IO.StreamReader reader = new
System.IO.StreamReader((System.IO.Stream)rawMessage[0].RetrieveAs(typeof(System.IO.Stream)));
return reader.ReadToEnd();

}


"Jeremy Chapman" wrote:

> I've got a file receive and send port, and am able to pass a binary file
> from one port to the other in an orchestration using a message type of
> xmldocument. Now I want to do something with the binary data but because
> the message type is xmldocument if I try to access the message biztalk tries
> to load the document and barfs because it's expecting valid xml when the
> data is actually binary. How can I access the data?
>
>
>

BizTalkVirtuoso

2006-06-13, 1:19 pm

Exactly. Also use the BinaryReader object to get the byte array from
the Stream.

Ben McFarlin
Microsoft BizTalk Server Consultant
www.biztalkvirtuoso.com

Jeremy Chapman

2006-06-13, 7:16 pm

OK, thanks. Now I'm going to be passing this data to a web service, which
means I need to base64 encode the data, send it to the webservice and inside
the webservice I need to unencode it to use it.

What is the recommended way to do this?

Some issues I see:
BinaryReader has no size property, so how best to get the entire byte array
to base 64 encode it?
How do I know what encoding mechanism to use when converting to byte[]?
UTf-8, ascii, not sure.


"BizTalkVirtuoso" <ben.mcfarlin@biztalkvirtuoso.com> wrote in message
news:1150222074.760326.187530@g10g2000cwb.googlegroups.com...
> Exactly. Also use the BinaryReader object to get the byte array from
> the Stream.
>
> Ben McFarlin
> Microsoft BizTalk Server Consultant
> www.biztalkvirtuoso.com
>



BizTalkVirtuoso

2006-06-13, 7:16 pm

// Stream to Byte Array
BinaryReader reader = new BinaryReader(strm);
byte[] bytes = new byte[strm.Length];
reader.Read(bytes, 0, bytes.Length);

// Base64Encode
string s = Convert.ToBase64String(bytes);

// Base64Decode
byte[] bytes_out = Convert.FromBase64String(s);

Jeremy Chapman

2006-06-13, 7:16 pm

BizTalk picks up a file from a directory, sends it contents to a web
service, and the web service is supposed to write the contents to a file.

I'm doing something wrong because the output file written by the webservice
isn't the same as the input file. I've created a method which gets the
binary data from an incomming message of type XmlDocument and Base64 encodes
it. The webservice decodes the base64 encode string and saves the file. I'm
not sure what is going wrong. when I debut it. I know that the byte array
is the same length before encoding as after encoding, so characters must be
getting stripped out. Any ideas?

public static string Encode64(Microsoft.XLANGs.BaseTypes.XLANGMessage
rawMessage)
{
System.IO.StreamReader pReader = new
System.IO.StreamReader((System.IO.Stream)rawMessage[0].RetrieveAs(typeof(System.IO.Stream)));

return
System.Convert.ToBase64String(pReader.CurrentEncoding.GetBytes(pReader.ReadToEnd()));
}

[WebMethod]
public string SendBinary(string strFileName, string strData)
{
byte[] binData = System.Convert.FromBase64String(strData);

string strFileWrite =
System.IO.Path.Combine(this.Context.Request.PhysicalApplicationPath,
System.IO.Path.GetFileName(strFileName));

System.IO.FileStream pWrite = System.IO.File.OpenWrite(strFileWrite);
try
{
pWrite.Write(binData,0,binData.Length);
}
finally
{
pWrite.Flush();
pWrite.Close();
}
}

"BizTalkVirtuoso" <ben.mcfarlin@biztalkvirtuoso.com> wrote in message
news:1150222074.760326.187530@g10g2000cwb.googlegroups.com...
> Exactly. Also use the BinaryReader object to get the byte array from
> the Stream.
>
> Ben McFarlin
> Microsoft BizTalk Server Consultant
> www.biztalkvirtuoso.com
>



BizTalkVirtuoso

2006-06-13, 7:17 pm

Wait a minute. If you are trying to post the data to a web service then
you don't need an orchestration or an external assembly. Just create
an HTTP Send Port that subscribes to the FILE Receive Port and route
the data as a passthru document. Then create the web service method
that will capture the post data.

[WebMethod]
public string MyMethod()
{
Stream strm = Context.Request.InputStream;

try
{
BinaryReader reader = new BinaryReader(strm);
byte[] bytes = new byte[strm.Length];
reader.Read(bytes, 0, bytes.Length);

// do your work here

}
catch(Exception exc)
{
System.Diagnostics.EventLog.WriteEntry("BizTalk Server 2004",
exc.ToString(), System.Diagnostics.EventLogEntryType.Error);
throw exc;
}
}
return "For BizTalk Server 2004 Consumption Only";
}

Jeremy Chapman

2006-06-13, 7:17 pm

Wish I could do it without the orchestration. I need to parse the file
name, and pass it to the web service.

"BizTalkVirtuoso" <ben.mcfarlin@biztalkvirtuoso.com> wrote in message
news:1150226979.807235.291900@f6g2000cwb.googlegroups.com...
> Wait a minute. If you are trying to post the data to a web service then
> you don't need an orchestration or an external assembly. Just create
> an HTTP Send Port that subscribes to the FILE Receive Port and route
> the data as a passthru document. Then create the web service method
> that will capture the post data.
>
> [WebMethod]
> public string MyMethod()
> {
> Stream strm = Context.Request.InputStream;
>
> try
> {
> BinaryReader reader = new BinaryReader(strm);
> byte[] bytes = new byte[strm.Length];
> reader.Read(bytes, 0, bytes.Length);
>
> // do your work here
>
> }
> catch(Exception exc)
> {
> System.Diagnostics.EventLog.WriteEntry("BizTalk Server 2004",
> exc.ToString(), System.Diagnostics.EventLogEntryType.Error);
> throw exc;
> }
> }
> return "For BizTalk Server 2004 Consumption Only";
> }
>



BizTalkVirtuoso

2006-06-13, 7:17 pm

FTP Adapter?

Sponsored Links






Free braindumps | Software forum | Database administration forum

Copyright 2003 - 2008 webservertalk.com