tags:

views:

961

answers:

3

I need to retrieve binary (non-XML) documents as Messages in a custom WCF LOB Adapter (for consumption by BizTalk). The messages need to be represented as instances of System.ServiceModel.Channel.Message.

I can't find how to create an instance of this class when the content is binary (by "binary" I mean any arbitrary byte array or Stream, and not only binary representations of XML documents).

Is this possible? Or would wraping the binary contents in an XML enveloppe be the only way to do this in WCF?

+1  A: 

WCF offers some ways to send binary attachments and stream data in various ways. We have an application at work that does this, but I haven't had a chance to dig through the code, so I can't offer too much help. Here are a few links that might get you started (Nicholas Allen's Indigo Blog is a great place for WCF info):

Andy White
good links, thanks!
rally25rs
+1  A: 

Fundamentally, WCF messages are XML, since the S.S.C.Message class uses the Xml InfoSet as the base message representation.

So yes, in some way you need to "wrap" your binary content into an XML envelope (which doesn't need to be a SOAP envelope, mind you, depending on how your binding is configured).

That said, note that this doesn't preclude streaming to deal with large message payloads without buffering the entire message in memory; WCF still allows you to do this, though sometimes it's not greatly obvious how it works.

Since you're working with a custom channel, you've got one of two choices, I think:

  1. Have your own channel add the XML wrapper around your binary content before passing it upwards or
  2. Create a custom MessageEncoder that adds it in for you automatically.

In either case, if you're dealing with large messages, you'll want to make sure you use the MessageEncoder overloads that handle Streams instead of buffers, as they are the ones that give you the option of doing coordinated streaming with the service implementation.

tomasr
A: 

I have a similar question, as I have a WCF channel that communicates via files. However, it's possible that the file may be something other than a XML file containing a message...it may be a comma delimited file. The point is that all the channel should do is take the contents of the file, wrap it into a message with the content as the body, whatever it is. Can it be something as simple as calling WriteRaw or WriteBase64 on the incoming XmlDictionaryWriter?

Rich