You might have noticed a common theme in dealing with some of the extras that come with WSE with regard to web service communication. Many of the additional industry standards, such as WS-Attachments, WS-Security, and so on, can be accessed via methods and properties of the WSE RequestSoapContext and ResponseSoapContext properties.
The same is true of attachments. To attach a file to a message being sent to a web service (or any other WSE-based SOAP endpoint, such as the TCP samples from earlier in the chapter), all you need to do is simply add the file to the Attachments collection by creating a DimeAttachment.
Introduction to DIME
DIME (Direct Internet Message Encapsulation) is a standard for sending attachments between hosts on the Internet. Without spending too much time getting into the low-level details of DIME, there is some basic information that might prove useful: DIME breaks up a file being attached to a message into multiple records for efficient transmission over the Internet.
Transferring Files via WSE
To send a message from one WSE endpoint to another, you can create an instance of the DimeAttachment class and add it to the Attachments collection of the appropriate SoapContext class. The following few lines of code shows how easy it is to add a simple image file as an attachment by passing a stream to an image to the DimeAttachment constructor:
DimeAttachment da = new DimeAttachment( "image/gif", TypeFormatEnum.MediaType, gifStream ); da.id = "myfile.gif"; wseProxy.RequestSoapContext.Attachments.Add( da );