2012-01-02 42 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' 
}