Attaching files to WCF REST service responses

Posted by David Seiler on Stack Overflow See other posts from Stack Overflow or by David Seiler
Published on 2010-06-07T18:30:39Z Indexed on 2010/06/07 23:42 UTC
Read the original article Hit count: 229

Filed under:
|

I have a resource that looks something like this:

/users/{id}/summary?format={format}

When format is "xml" or "json" I respond with a user summary object that gets automagically encoded by WCF - fine so far. But when format equals "pdf", I want my response to consist of a trivial HTTP response body and a PDF file attachment.

How is this done? Hacking on WebOperationContext.Current.OutgoingResponse doesn't seem to work, and wouldn't be the right thing even if it did. Including the bits of the file in a CDATA section or something in the response isn't safe. Should I create a subclass of Message, then provide a custom IDispatchMessageFormatter that responds with it? I went a short distance down that path but ultimately found the documentation opaque.

What's the right thing?

© Stack Overflow or respective owner

Related posts about wcf

Related posts about rest