在 Silverlight 中删除过时的 WCF 响应
在 Silverlight 中我遇到了以下问题。 如果您向 Web 服务发出多个请求,响应可能不会按顺序返回。 这意味着如果第一个请求花费的时间比以下请求的时间长,它的响应将最终返回:
1. Sending request A.. (takes longer for some reason)
2. Sending request B..
3. Sending request C..
4. ...
5. Receiving response B
6. Receiving response C
7. Receiving response A
现在在我的场景中,我只对最近发出的请求感兴趣。 因此,A 和 B 应该被丢弃,C 应该被保留为唯一接受的响应。
管理这个问题的最佳方法是什么? 到目前为止,我想出了这个解决方案:
在发送请求时将生成的 GUID 作为用户对象传递并将该值存储在某处。 由于所有响应都将包含其各自的 GUID,因此您现在可以过滤掉过时的响应。 使用请求计数器代替 GUID 也可以。
现在我想知道是否有更好的方法。 也许有任何开箱即用的功能可以使这成为可能? 欢迎任何想法..
In Silverlight I got the following problem. If you fire multiple requests to the web service, the responses might not return in an ordered sequence. Meaning if the first request takes longer than the following ones, its response will return at last:
1. Sending request A.. (takes longer for some reason)
2. Sending request B..
3. Sending request C..
4. ...
5. Receiving response B
6. Receiving response C
7. Receiving response A
Now in my scenario, I am only interested in the most recent request being made. So A and B should be discareded and C should be kept as only accepted response.
What is the best approach to manage this? I came up with this solution so far:
Pass a generated GUID as user object when sending the request and store that value somewhere. As all responses will contain their respective GUID, you can now filter out the stale responses. A request-counter instead of a GUID would work as well.
Now I wonder if there are any better approaches to this. Maybe there are any out of the box features to make this possible? Any ideas are welcome..
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我在非 WCF ASP.NET Web 服务中采用了类似的方法,但我使用请求的
DateTime
来代替,然后只存储最近请求的DateTime
。 这样我就可以直接进行小于比较,以确定返回的服务是否是最新的。我确实考虑过在进行新服务调用之前取消旧服务调用,但是 Silverlight 中没有针对 Web 服务的
CancelAsync
调用,而且我一直无法找到执行此操作的等效方法。I take a similar approach in my non-WCF ASP.NET web services, though I use the
DateTime
of the request instead and then just store theDateTime
of the most recent request. This way I can do a direct less than comparison to determine if the returning service is the most recent or not.I did look into canceling old service calls before making new ones, but there is no
CancelAsync
call for web services in Silverlight and I have been unable to find an equivalent way of doing this.这两种方法都是我在处理具有大量服务调用的实时系统时所采用的。 基本上只是有一些方法来跟踪顺序(递增变量、时间戳等),然后跟踪收到的最高响应。 如果当前响应低于最高响应,则将其丢弃。
Both of these approaches are what I took when I worked on a real time system with a lot of service calls. Basically just have some way to keep track of order ( incrementing variable, timestamp, etc. ) then keep track of highest received response. If the current response is lower than the highest, drop it.