简体   繁体   English

如何从WCF REST服务流式传输字符串

[英]How to stream a string from a WCF REST service

I wrote a very basic WCF REST Service in C#, that has the following in the service interface: 我用C#编写了一个非常基本的WCF REST服务,该服务界面中包含以下内容:

[OperationContract]
[WebGet(UriTemplate = "/stream", ResponseFormat = WebMessageFormat.Xml)]
Stream streamTest();

In the Service.svc.cs file it has this function (obviously StreamUtility is included as a reference and in the usings): 在Service.svc.cs文件中,它具有此功能(显然,StreamUtility包含在参考中和使用中):

public Stream streamTest()
    {
        return StreamUtility.streamTest();
    }

And finally the streaming function itself looks like this: (It returns a large arbitrary string as a memory stream based on source code I found here: http://www.codeproject.com/Articles/35982/REST-WCF-and-Streams-Getting-Rid-of-those-Names-Sp ) 最后,流功能本身看起来像这样:(它根据我在这里找到的源代码返回一个大的任意字符串作为内存流: http : //www.codeproject.com/Articles/35982/REST-WCF-and-Streams摆脱这些名称Sp

public static Stream streamTest()
    {
        string result = "skdjdfnlsakdjfnasdlkjfnasdkjkfnasldkjfnasdlkjfnasdlkfjnasdldkfjnassdlfkjnadsdlfkjasndflkjsanflaskjdfnalskdkjnfalskfjnslidifubsaoicubuiewnfpoiowneflskjdfansdufawneifuenfgiwe";
        for (int i = 0; i < 5; i++)
        {
            result += result;
        }
        Encoding encoding = Encoding.GetEncoding("ISO-8859-1");
        byte[] returnBytes = encoding.GetBytes(result);
        return new MemoryStream(returnBytes);
    }

Unfortunately this function does not seem to work properly for no reason that i could discern. 不幸的是,出于我无法辨别的原因,该功能似乎无法正常工作。 When i call the function via my browser by going to localhost:XXXXX/Service.svc/stream it shows a loading icon and then simply freezes. 当我通过浏览器访问localhost:XXXXX / Service.svc / stream调用此函数时,它显示一个加载图标,然后冻结。

I've been able to find some websites that try to answer this question, but none of their code actually works. 我已经找到了一些尝试回答此问题的网站,但它们的代码均无法正常工作。 The place where I found the above code has a line that doesn't work: 在上面找到上述代码的地方,有一行无效:

WebOperationContext.Current.OutgoingResponse.ContentType = "text/plain";

This line gets underlined because the program cannot find the WebOperationContext reference and I don't know where it would be located, despite having downloaded the source code from the site which only includes two C# files and is not made to actually run. 该行带有下划线,因为该程序无法找到WebOperationContext引用,并且尽管从该站点下载了仅包含两个C#文件且无法实际运行的源代码,但我不知道它将位于何处。

If anyone could tell me exactly how to stream data via REST service in WCF (even if its not by the above method) that would be greatly appreciated. 如果有人能确切告诉我如何通过WCF中的REST服务流数据(即使不是通过上述方法),也将不胜感激。 Thank you. 谢谢。

If you want to use only GET and POST, the following article may be helpful: 如果只想使用GET和POST,以下文章可能会有所帮助:

CodeProject on developing RESTful WCF services (this is the way that I personally took) 开发RESTful WCF服务的CodeProject (这是我个人采取的方式)

You could possibly use the WebHttpBinding as described here, providing PUT and DELETE also: 您可以按照此处所述使用WebHttpBinding,同时提供PUT和DELETE:

WCF REST Service with JSON 带有JSON的WCF REST服务

An even simpler example is here (also using the WebHttpBinding) 这里有一个更简单的示例(也使用WebHttpBinding)

MS Social on streaming WCF REST 流式WCF REST的MS Social

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

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