WCF webservice流式响应的最佳实践 [英] Best practices for streaming response of WCF webservice
问题描述
我正在尝试从 WCF Web 服务中提取大量数据.请求相当小,响应消息将非常大.当前,由于 IIS6 对其可以分配的内存(~1.4GB)的限制,Web 服务正在抛出 SystemOutOfMemory 异常.
I'm trying to pull a large amount of data out of a WCF web service. The request is fairly small and the response message will be very big. Currently the web service is throwing SystemOutOfMemory exceptions due a limitation on IIS6 for the memory it can allocated (~1.4GB).
我在一些博客中读到实施流媒体将解决我的问题.
I have read in some blogs that implementing streaming will solve my problem.
有人可以分享他们在这个话题上的经验吗?我对任何示例客户端最感兴趣 &可以共享的服务端代码或任何建议/最佳实践.内存流与文件流?返回类型应该是 Stream、Message、Byte[]?
Can anybody share their experiences with this topic? I'm most interested in any sample client-side & service-side code that can be shared or any recommendations/best practices. MemoryStream vs FileStream? Return type should be Stream, Message, Byte[]?
我的操作如下所示:(通常它会在响应数组中返回大量元素,约 200K 个元素)
My operation looks like the following: (typically it will return a big number of elements in the response array, ~200K elements)
MediumSizeResponseClass[] GetData(SmallSizeRequestClass request)
推荐答案
如果您只想回传响应,请在绑定配置中使用 transferMode=streamedResponse
.这确保只有返回的响应才会被流式传输.
If you want to stream back only the response, then use transferMode=streamedResponse
in your binding configuration. This makes sure only the returned response will be streamed.
流函数的返回值必须是Stream
.然后,您可以从该流中读取数据,并对其进行任何您需要做的事情(存储它、分析它等等).
The return value of a streaming function must be a Stream
. You can then read from that stream and do whatever you need to do with it (store it, analyse it, whatever).
所以基本上你会有一个这样的服务合同:
So basically you'd have a service contract something like this:
[ServiceContract]
interface IYourService
{
[OperationContract]
Stream GetData(SmallSizeRequestClass request);
}
在服务器上,您基本上只是写入流,而在客户端上,您从流中读取.
On the server, you basically just write to a stream, while on the client, you read from the stream.
您是否查阅过有关 WCF 流 的 MSDN 文档?
Have you consulted the MSDN docs on WCF Streaming?
这篇关于WCF webservice流式响应的最佳实践的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!