Craig
Craig

Reputation: 7076

Returning a Stream from a WCF REST based service to Silverlight application

I've created the following method contract, which returns a Stream from a WCF REST based service:

[OperationContract, WebGet(UriTemplate = "path/{id}")]
Stream Get(string id);

Implementation:

public Stream Get(string id)
{
    WebOperationContext.Current.OutgoingResponse.ContentType = "text/xml";

    return new MemoryStream(Encoding.UTF8.GetBytes("<myXml>some data</MyXml>"));
}

A. How do I access this method using WebRequest?

Being that this sounds like such an easy question, I suspect that I may be barking up the wrong tree...maybe returning an XmlElement is a better approach.

B. What is the recommended way of returning raw XML from a WCF REST based service?

Upvotes: 1

Views: 2450

Answers (1)

laika
laika

Reputation: 1497

I will first answer your second question

What is the recommended way of returning raw XML from a WCF REST based service?

Generally there is no recommended way. RESTful API concept is abstracted from data format. On returning Stream from HTTP based WCF service I would quote this MSDN article

Because the method returns a Stream, WCF assumes that the operation has complete control over the bytes that are returned from the service operation and it applies no formatting to the data that is returned.

And to answer your first question, here is a snippet of code that can invoke your implementation

var request = (HttpWebRequest)WebRequest.Create("location-of-your-endpoint/path/1");
request.Method = "GET";

using (var webResponse = (HttpWebResponse) request.GetResponse())
{
    var responseStream = webResponse.GetResponseStream();
    var theXmlString = new StreamReader(responseStream, Encoding.UTF8).ReadToEnd();

    // now you can parse 'theXmlString'
}

Upvotes: 1

Related Questions