WCF doesnt have out of box features to send SOAP message with Attachments. The Austrian interoperablity council has provided a solution to add a custom message encoder in WCF to send SWA. However the solution can only be used to send PDF attachments with a SOAP Payload. I have added a configurable parameter to set the attachment type.

Building the Sample

You probably need VS 2010 for building this project and to test this solution, you need a service which accepts SOAP Payload with attachments.


Soap With attachments is a very old technique and is still used in lot of legacy systems. More new technologies has emerged like MTOM which is widely used within the WCF arena. That said, there is no out of the box support within WCF to send a SOAP message with attachments.

An Austrian Microsoft Interoperability Council had a challenge to consume a webservice which expects to send a SOAP message with attachments.

They have developed a custom encoder which can add the zip message as an attachment in the outgoinh SOAP envelope.

However I had a different challenge in consuming a webservice which requires me to send a soap payload which has an XML attachment. So I had to modify the custom encoder in such a way that I can attach a XML document. I had lot more challenges. The webservice which I had to consume was written in Java and the field appears as xsd:hexBinary in the WSDL file. So there is no way I can add a service reference / web reference to my project. I did tried that, but when I tried to send the message, the server was rejecting the message. The reason for this is Visual studio when generating the client code for the service, puts the type as xsd:base64binary.

So, I had to code the datacontracts, messagecontracts and servicecontracts based on the WSDL. Then I plugged in the code to use the encoder to attach the XML message. There is no direct way to do this from BizTalk. I wrote this entire logic in a WCF Service which acts as a client to the Target web service and acts as a Server for BizTalk. BizTalk application will consume this WCF service and send the SOAP message with attachment.

public SwaEncoder(MessageEncoder innerEncoder, SwaEncoderFactory factory, string attachmentContentType) 
            // Initialize general fields 
            _ContentType = "multipart/related"; 
            _MediaType = _ContentType; 
            // Create owned objects 
            _Factory = factory; 
            _InnerEncoder = innerEncoder; 
            _MimeParser = new MimeParser(); 
            // Create object for the mime content message 
            _SoapMimeContent = new MimePart() 
                ContentType = "text/xml", 
                ContentId = "<EFD659EE6BD5F31EA7BC0D59403AF049>",   // TODO: make content id dynamic or configurable? 
                CharSet = "UTF-8",                                  // TODO: make charset configurable? 
                TransferEncoding = "binary"                         // TODO: make transfer-encoding configurable? 
            _AttachmentMimeContent = new MimePart() 
                ContentType = attachmentContentType, 
                ContentId = "<UZE_26123_>",                         // TODO: AttachmentMimeContent.ContentId configurable/dynamic? 
                TransferEncoding = "binary"                         // TODO: AttachmentMimeContent.TransferEncoding dynamic/configurable? 
            _MyContent = new MimeContent() 
                Boundary = "------=_Part_0_21714745.1249640163820"  // TODO: MimeContent.Boundary configurable/dynamic? 

Source Code Files

More Information

For more information on this solution, please refer the below blog entry.