WCF offers two options for ResponseFormat attribute in WebGet annotation in ServiceContract.
[ServiceContract]
public interface IService1
{
[OperationContract]
[WebGet(UriTemplate = "greet/{value}", BodyStyle = WebMessageBodyStyle.Bare)]
string GetData(string value);
[OperationContract]
[WebGet(UriTemplate = "foo", BodyStyle = WebMessageBodyStyle.Bare, ResponseFormat = WebMessageFormat.Json)]
string Foo();
The options for ResponseFormat are WebMessageFormat.Json and WebMessageFormat.Xml. Is it possible to write my own web message format? I would like that when client calls foo() method he gets raw string - without json or xml wrappers.
I implemented this attribute like this, maybe it will help someone in the future:
WebGetAttribute
is shipped by Microsoft, and I don't think you can extendWebMessageFormat
. However you could probably extend theWebHttpBinding
that usesWebGetAttribute
. You could add your own attribute likeIn general, customizing the message layout in WCF is called custom message encoder/encoding. Microsoft provides an example: Custom Message Encoder: Compression Encoder. Also another common extension people do is to extend behavior to add custom error handling, so you could look for some example in that direction.
Try using
Then return a System.IO.Stream from your function.
Here's some code I use to return an image out of a database, but accessible via a URL:
Implementation:
In your case, to return a raw string, set the ContentType to something like "text/plain" and return your data as a stream. At a guess, something like this:
There is one way how to achieve this if you're dealing with HTTP, it's not exactly nice, but I thought I could mention it.
You can set the return type of your method to void and just output your raw string directly into the response.