Other Attachment Technologies


Although DIME and WS-Attachments provides an innovative new method for more efficiently transporting all types of file attachments with SOAP messages, there are alternate methods for accomplishing this. I will briefly describe these technologies, but I will not cover them in great depth as support for them is not implemented in WSE.

Base64 Encoding Attachments

As I already mentioned, including binary data as Base64 encoded text is a mechanism that can be used to include binary data inside a SOAP envelope. The XML facilities of the .NET Framework makes this fairly easy to accomplish using the System.Xml.XmlTextWriter class. The following example code shows how to use an XmlTextWriter object to manually construct a simple SOAP envelope containing a binary image file as a Base64 encoded attachment:

 // Set the buffer for reading input bytes 
int size = 4096;
byte[] bytes = new byte[4096];
int numBytes = 0;

// Reader for the binary file from a FilesStream
BinaryReader myReader = new BinaryReader(myInFS);

// Instantiate an XML text writer out to a FilesStream
XmlTextWriter myWriter = new XmlTextWriter(myOutFS, Encoding.UTF8);

// Create the SOAP envelope and body
myWriter.WriteStartDocument();
myWriter.WriteStartElement(
"soap","Envelope","http://schemas.xmlsoap.org/soap/envelope/");
myWriter.WriteStartElement(
"soap","Body","http://schemas.xmlsoap.org/soap/envelope/");

// Create an element to contain the encoded attachment.
myWriter.WriteStartElement("x","Base64Attachment", "http://example.com");
myWriter.WriteStartAttribute("x","mimeType","http://example.com");
myWriter.WriteString("image/jpeg");
myWriter.WriteEndAttribute();

try
{
// Write bytes from incoming stream to Base64 encoded XML
while((numBytes = myReader.Read(bytes, 0, size)) > 0)
{
myWriter.WriteBase64(bytes, 0, numBytes);
}
}
catch (Exception ex)
{
// Do error handling here
}
myWriter.WriteEndElement();
myWriter.WriteEndElement();
myWriter.WriteEndElement();
myWriter.Flush();
myWriter.Close();

This code builds the following SOAP envelope containing encoded binary data:

 <?xml version="1.0 encoding="utf-8?> 
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
<soap:Body>
<x:Base64Attachment x:mimeType="image/jpeg xmlns:x=http://example.com
>/9j/4AAQSkZJRgABAQEBL10NqCuznSc3ZH/9k

</x:Base64Attachment>
</soap:Body>
</soap:Envelope>

For readability, I truncated the five pages of Base64 encoded text generated from a 14,000-byte file. Actually, when Base64 encoded, my 14-KB binary file became approximately 19 KB of text. You can see how this transport mechanism can become resource intensive when attaching large binary files. In a similar fashion, the XmlTextReader class can be used to read the Base64 encoded data so that it can be decoded back into a binary data stream, as in the following example:

 // Set the buffer for reading input bytes 
int size = 4096;
byte[] bytes = new byte[4096];
int numBytes = 0;

// Reader for the XML FileStream
XmlTextReader myReader = new XmlTextReader(myInFS);

// Instantiate an XML text writer out to a FileStream
BinaryWriter myWriter = new BinaryWriter(myOutFS);

// Read from the SOAP envelope.
try
{
while (myReader.Read())
{
// Find the element with the attachment.
if (myReader.IsStartElement() &&
myReader.Name == x:Base64Attachment)
{
// Write Base64 encoded XML to the stream writer.
while((numBytes = myReader.ReadBase64(bytes, 0, size)) > 0)
{
myWriter.Write(bytes, 0, numBytes);
}
}
}
}
catch (Exception ex)
{
// Do error handling here
}
myWriter.Flush();
myWriter.Close();

With all of the SOAP facilities provided by the .NET Framework, you usually would not manually construct or parse a SOAP envelope in this manner; however, you can still see how easy it is to create a Web method to transport small binary files as Base64 encoded XML.

SOAP with Attachments

The idea behind SOAP Messages with Attachments, a note published by the W3C ( http://www.w3.org/TR/SOAP-attachments ), is to provide a binding between SOAP 1.1 and MIME. This enables you to use a MIME multipart encapsulation mechanism, which many developers are more familiar with, instead of DIME for encapsulating a primary SOAP message and one or more attachments. The general concept of a MIME multipart encapsulation is the same as with DIME, namely that there is a primary SOAP message which references one or more attachments that are found in other parts of the MIME encapsulation. There is also a proposed addendum to SOAP Messages with Attachments, published initially at http://www.gotdotnet.com/team/jeffsch/ paswa /paswa61.html , which brings this specification more inline with XML-based processors.

The following is what a MIME-encapsulated SOAP message with attachments might look like using the two binary image files from the previous example:

 MIME-Version: 1.0 
Content-Type: Multipart/Related; boundary=MIME_boundary; type=text/xml;
start="<http://example.com/mime/imageservice/myimages.xml>"
Content-Description: An XML document with one or more JPEG image files

--MIME_boundary
Content-Type: text/xml; charset=UTF-8
Content-Transfer-Encoding: 8bit
Content-ID: <http://example.com/mime/imageservice/myimages.xml>

<soap:Envelope xmlns:soap="http://www.w3.org/2002/12/soap-envelope"
xmlns:xbinc="http://schemas.xmlsoap.org/2003/04/xbinc"
xmlns:xmime="http://schemas.xmlsoap.org/2003/04/xmime" >
<soap:Header>
<xbinc:DoInclude
soap:role="http://www.w3.org/2002/12/soap-envelope/role/next"
soap:mustUnderstand="false"
soap:relay="true" />

</soap:Header>
<soap:Body>
<m:manifest xmlns:m="http://example.org/mime/imageservice" >
<m:item name="image1.jpg" xmime:MediaType="image/jpeg" >
<xbinc:Include href="uuid:09233523-345b-4351-b623-5dsf35sgs5d6" />
</m:item>
<m:item name="image2.jpg" xmime:MediaType="image/jpeg" >
<xbinc:Include href="uuid:592e3541-a53b-af31-b94c-f31b6392ca8e" />
</m:item>
</m:manifest>

</soap:Body>
</soap:Envelope>

--MIME_boundary
Content-Type: image/jpeg
Content-Transfer-Encoding: binary
Content-ID: <uuid:09233523-345b-4351-b623-5dsf35sgs5d6>

fd a5 8a 29 aa 46 1b 24


--MIME_boundary
Content-Type: image/jpeg
Content-Transfer-Encoding: binary
Content-ID: <uuid:592e3541-a53b-af31-b94c-f31b6392ca8e>

b1 d7 1f a3 62 53 89 71

Although this encapsulation mechanism might gain traction in the future with people who prefer MIME or who need to use SMTP for sending SOAP messages with attachments, as this book goes to press, there is no Microsoft support for these technologies and no functional examples that I have come across. That should not stop you from checking out the specifications if you are interested.




Understanding Web Services Specifications and the WSE
Understanding Web Services Specifications and the WSE (Pro Developer)
ISBN: 0735619131
EAN: 2147483647
Year: 2006
Pages: 79

Similar book on Amazon

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net