Passing a Binary as a parameter to a Web Method from a BizTalk 2004 Orchestration Web Port

Christof Claessens has an excellent entry on how to pass a binary object
to a Send Port, such as a file send port. The binary that is saved by the 
send port using his method will automatically be saved to its native format when written out to a file. For
example : c:\myBTS2004SendLocation\MyExcelFile.xls

But using the above will not necessarily work when calling a web method
via a BizTalk Orchestration Web Port as below:

[WebMethod]
ProcessBinary (System.XML.XMLDocument binaryDocument)
{

}

The Web Method is expecting well formed XML that is not present in the binary document.
When the Web Service is called from the orchestration, the Web Service will throw an error.

A possible solution is to convert the binary document to an array Of bytes
and then call a web method via a BizTalk Orchestration Web Port as below:

[WebMethod]
ProcessBinary (System.Byte[] binaryDocument)
{

}

But when you try to add the Web Reference to a BizTalk project, you will get an error such as:
Could not generate BizTalk files. An (WebMethodName) operation parameter
or return type is an Array. Array types are not supported.

A very simple solution is to convert the binary document to an array of bytes,
then convert the array of bytes to a string using something like the code below:

byte[] myBytes;
FileStream fs = new FileStream(@"C:\myExcelDoc.xls",FileMode.Open, FileAccess.Read);
BinaryReader r = new BinaryReader(fs);
myBytes = r.ReadBytes(Convert.ToInt32(r.BaseStream.Length));
// Now convert the array of bytes to a string.
string myBase64String;
myBase64String = Convert.ToBase64String(myArrayofBytes);

The above string can now be passed to the below web method via a
BizTalk Orchestration Web Port as below:

[WebMethod]
ProcessBinary (System.String binaryDocument)
{

 byte[] arrayOfBytes;
 arrayOfBytes = Convert.FromBase64String(binaryDocument);
 FileStream fs = new FileStream(@“c:\fileout.xls“, FileMode.CreateNew);
 BinaryWriter w = new BinaryWriter(fs);
 w.Write(arrayOfBytes);  

}


Here download the complete solution.