2012-01-02 70 views
1

我创建了下面的方法合同,这从一个基于WCF REST服务返回Stream Silverlight应用程序:返回从WCF基于REST的服务的流

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

实现:

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

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

A.如何使用WebRequest访问此方法?因为这听起来像是一个简单的问题,我怀疑我可能会吼出错误的树......也许返回XmlElement是一个更好的方法。

B.从基于WCF REST的服务返回原始XML的建议方式是什么?

+1

我想你要找的字[POX(HTTP:// msdn.microsoft.com/en-us/library/aa738456.aspx) – 2012-01-02 14:20:25

回答

1

我先回答你的第二个问题

什么是从WCF基于REST的服务返回原始XML的推荐的方法?

通常没有推荐的方法。 RESTful API概念是从数据格式中抽象出来的。从基于HTTP的WCF服务返回Stream我想引用this MSDN article

因为该方法返回一个Stream,WCF假定操作有超过那些从服务操作返回的字节完全控制,并没有格式适用于返回的数据。

并回答您的第一个问题,这里的代码片段,可以调用您的实现

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' 
}