WCF 数据服务 (OData)、SetEntitySetPageSize 和自定义操作的问题
我有一个 WCF 数据服务,其中包含名为 MostRecentFilms
的自定义操作,该操作返回源中最新的 10 部电影。每部电影都有一个 Year
属性。使用默认设置一切正常,但是当我设置实体集的页面大小 config.SetEntitySetPageSize("*", 100)
时,返回的影片的顺序不好。如果结果集少于 100 个也没关系,它们没有正确地从较新到较少排序,当不存在页面大小限制时如何返回它。
我不知道这是否是 WCF 数据服务错误,或者我是否缺少某些配置。任何澄清这一点的帮助将不胜感激。
I have a WCF Data Service with a custom operation named MostRecentFilms
that returns the 10 most recent films in the source. Each film has, among others, a Year
property. All goes fine with the default setting but when I set the page size for entity sets, config.SetEntitySetPageSize("*", 100)
, the order of returned films are not good. Doesn't matter if the result set has less than 100, they are not properly ordered from more recent to less, how it was returned when the page size restriction wasn't present.
I don't know if this is a WCF Data Service bug or if I'm missing some configuration here. Any help to clarify this will be appreciated.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这实际上是设计使然。服务器驱动的分页(SetEntitySetPageSize 支持它)的实现方式需要稳定的(且众所周知的)结果顺序。
因此,服务将根据给定实体上的所有关键属性对服务操作的结果进行排序(它也对实体集执行此操作)。
客户端可以在某种程度上影响排序 - 如果请求中有 $orderby,则生成的订单将是 $orderby 的应用程序,后跟所有关键属性。
目前,服务器上无法规定实体集或服务操作的顺序,以便服务器驱动的分页处理会像处理客户端的 $orderby 一样考虑它。
您可以让您的客户端添加正确的 $orderby ,或者如果这不可能,我能想到的唯一其他解决方法是在 URL 被 WCF 数据服务处理之前将 $orderby 注入到 URL 中(这可以通过自定义主机来完成) 、特殊标头、WCF...取决于您托管服务的具体方式等)。但这有点棘手,需要您半解析 URL 才能识别任何现有的 $orderby 。
请注意,此行为不仅适用于服务器驱动的分页,$top 和 $skip 也会对结果重新排序以保持稳定的排序。
This is actually by design. The way the server driven paging (SetEntitySetPageSize enables it) is implemented requires a stable (and well known) order of the results.
So the service will order the results of your service operation (it does that with entity sets as well) by all the key properties on the given entity.
The client can influence the ordering to some extent - if there's an $orderby on the request the resulting order will be the application of $orderby followed by all the key properties.
Currently there's no way on the server to prescribe the order for either entity set or service operation such that the processing of server driven paging would take it into considertation the same way it does with $orderby from the client.
You can either make your client add the right $orderby or if that's not possible the only other workaround I can think of is to inject the $orderby into the URL before it gets processed by WCF Data Service (this can be done through a custom host, special headers, WCF, ... depends on how exactly you host the service and such). But that's a bit hacky and requires you to semi-parse the URL to be able to recognize any existing $orderby already there.
Note that this behavior is not only for server driven paging, also $top and $skip will reorder the results to maintain a stable ordering.