简体   繁体   English

将流从基于WCF REST的服务返回到Silverlight应用程序

[英]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: 我创建了以下方法协定,该协定从基于WCF REST的服务返回Stream

[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 ? 答:如何使用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. 由于这听起来像是一个简单的问题,所以我怀疑我可能正在吠叫错误的树...也许返回XmlElement是更好的方法。

B. What is the recommended way of returning raw XML from a WCF REST based service? B.从基于WCF REST的服务中返回原始XML的推荐方法是什么?

I will first answer your second question 我会先回答你的第二个问题

What is the recommended way of returning raw XML from a WCF REST based service? 从基于WCF REST的服务中返回原始XML的推荐方法是什么?

Generally there is no recommended way. 通常没有推荐的方法。 RESTful API concept is abstracted from data format. RESTful API概念是从数据格式中抽象出来的。 On returning Stream from HTTP based WCF service I would quote this MSDN article 从基于HTTP的WCF服务返回Stream ,我会引用此MSDN文章

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. 因为该方法返回一个Stream,所以WCF假定该操作完全控制了从服务操作返回的字节,并且不对返回的数据应用任何格式。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM