流式传输 WCF 数据服务响应

发布于 2024-12-01 09:12:37 字数 998 浏览 0 评论 0原文

我正在尝试传输 wcf 数据服务的响应,以使等待时间更加用户友好。响应采用 XML 格式(我使用实体框架 4.1) 我有这些预定义的事件

service.SendingRequest += service_SendingRequest;
service.ReadingEntity += service_ReadingEntity;
service.WritingEntity += service_WritingEntity;

在我调用 DataServiceQuery Execute 方法之后,

var items = myItems.Query.Execute();

这是 SendingRequest 事件的正文

        var response = (HttpWebResponse)e.Request.GetResponse();
        var resStream = response.GetResponseStream();

        var sb = new StringBuilder();
        var buf = new byte[1024];

        string tempString;
        int count;

        do
        {
            count = resStream.Read(buf, 0, buf.Length);

            if (count != 0)
            {
                tempString = Encoding.ASCII.GetString(buf, 0, count);
                sb.Append(tempString);
            }
        }
        while (count > 0);

所以问题是,之后什么也没有发生。下一个事件 ReadingEntity 不会触发。 我该如何解决这个问题?

I'm trying to stream the response of my wcf dataservice to make waiting time more user friendly. The response is in XML format (I use entity framework 4.1)
I have these predefined events

service.SendingRequest += service_SendingRequest;
service.ReadingEntity += service_ReadingEntity;
service.WritingEntity += service_WritingEntity;

after that I call DataServiceQuery Execute method

var items = myItems.Query.Execute();

Here is the body of SendingRequest event

        var response = (HttpWebResponse)e.Request.GetResponse();
        var resStream = response.GetResponseStream();

        var sb = new StringBuilder();
        var buf = new byte[1024];

        string tempString;
        int count;

        do
        {
            count = resStream.Read(buf, 0, buf.Length);

            if (count != 0)
            {
                tempString = Encoding.ASCII.GetString(buf, 0, count);
                sb.Append(tempString);
            }
        }
        while (count > 0);

So the problem is that after that nothing is happening. The next event ReadingEntity is not firing.
How can I solve this issue?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

软糯酥胸 2024-12-08 09:12:37

您无法更改服务发送请求的方式。该事件允许您修改请求标头,但服务必须自行调用它。您的代码很可能破坏了服务的功能。我也不认为你想做的事情是可能的。 WCF 数据服务仍然在内部使用 WCF,除非它使用流式传输,否则它将始终等待整个消息,然后再将其传递到上层(上下文)。仅当实现 流媒体提供程序,它主要用于下载二进制数据,而不是用于下载块中的公共数据。

您尝试执行的操作需要分块响应(在 WCF 流中使用)。使用默认的 WCF 功能集,处理分块响应是不受您控制的。

You cannot change the way how service sends the request. The event is there to allow you modify you request headers but the service must call it itself. Your code has most probably broke the service's functionality. Also I don't think that what you are trying to do is possible. WCF Data Service still uses WCF internally and unless it is using streaming it will always wait for the whole message before it passes it to your upper layer (context). Streaming in WCF data services is possible only when implementing streaming provider and it is mostly for downloading binary data not for downloading common data in chunks.

What you are trying to do would require chunked response (used in WCF streaming). With default WCF feature set working with chunked response is out of your control.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文