WCF ResponseFormat For WebGet

Try using

BodyStyle = WebMessageBodyStyle.Bare

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:

[OperationContract()]
[WebGet(UriTemplate = "Person/{personID}/Image", BodyStyle = WebMessageBodyStyle.Bare)]
System.IO.Stream GetImage(string personID);

Implementation:

public System.IO.Stream GetImage(string personID)
{
    // parse personID, call DB

    OutgoingWebResponseContext context = WebOperationContext.Current.OutgoingResponse;

    if (image_not_found_in_DB)
    {
        context.StatusCode = System.Net.HttpStatusCode.Redirect;
        context.Headers.Add(System.Net.HttpResponseHeader.Location, url_of_a_default_image);
        return null;
    }

    // everything is OK, so send image

    context.Headers.Add(System.Net.HttpResponseHeader.CacheControl, "public");
    context.ContentType = "image/jpeg";
    context.LastModified = date_image_was_stored_in_database;
    context.StatusCode = System.Net.HttpStatusCode.OK;
    return new System.IO.MemoryStream(buffer_containing_jpeg_image_from_database);
}

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:

return new System.IO.MemoryStream(ASCIIEncoding.Default.GetBytes(string_to_send));

Leave a Comment