流式传输WCF大量对象

时间:2012-11-21 13:53:47

标签: c# wcf streaming deferred-execution large-data

我面临编写在IIS上运行的WCF服务器以供不同计算机上的其他平台使用。我需要将大量对象(序列化)返回给客户端。我需要它进行流式处理,因为从我的数据库中获取所有数据并根据cilent的要求进行排列是一个非常漫长的过程,因此我需要客户端能够在流出时对结果进行处理。

在对主题进行一些阅读之后,似乎如果我使用带有transferMode StreamedResponse的WCF,我可以返回具有延迟执行的对象,这可能会满足我的需求。

我阅读了以下文章: http://weblogs.asp.net/cibrax/archive/2008/06/10/streaming-large-content-with-wcf-and-deferred-execution.aspx

并实现了与他们描述的类似的WCF。但是我改变了他们的代码:

for(long i = 0; i < 1000; i++) //All the customers should be read from the database
{
   yield return new Customer { FirstName = "Foo", LastName = "Bar", Address = "FooBar 123" };
}

为:

for(long i = 0; i < 1000000; i++) //All the customers should be read from the database
{
   yield return new Customer { FirstName = "Foo", LastName = "Bar", Address = "FooBar 123" };
}

因此它会模拟大量数据。 我注意到,当我这样做时,客户端的功能在返回对象之前会停顿几秒钟 为什么会这样? 数据不应该立即开始在流上流动吗? 这不是具有延迟执行的流式WCF的重点吗? 你对我的问题有更好的建议吗?

1 个答案:

答案 0 :(得分:1)

我看了一下链接中的代码。

看起来CreateMessage在服务器上运行,在服务器开始返回任何数据之前将整个结果放在变量消息中。

   Message message = Message.CreateMessage(MessageVersion.Soap11, "GetAllCustomers", new CustomBodyWriter(GetAllCustomersImpl()));
   return message;
相关问题