[英]Returning a Stream from a WCF REST based service to Silverlight application
我創建了以下方法協定,該協定從基於WCF REST的服務返回Stream
:
[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>"));
}
答:如何使用WebRequest
訪問此方法?
由於這聽起來像是一個簡單的問題,所以我懷疑我可能正在吠叫錯誤的樹...也許返回XmlElement
是更好的方法。
B.從基於WCF REST的服務中返回原始XML的推薦方法是什么?
我會先回答你的第二個問題
從基於WCF REST的服務中返回原始XML的推薦方法是什么?
通常沒有推薦的方法。 RESTful API概念是從數據格式中抽象出來的。 從基於HTTP的WCF服務返回Stream
,我會引用此MSDN文章
因為該方法返回一個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'
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.