Problem with WCF Data Service (OData), SetEntitySetPageSize and custom operations
Asked Answered
P

1

0

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.

Postboy answered 18/4, 2011 at 21:1 Comment(1)
Is the MostRecentFilms call explicity setting the order for the results?Ingrained
P
2

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.

Peltate answered 19/4, 2011 at 10:16 Comment(3)
Thanks Vitek. The problem here is that I'm already defining an order, based on the Film's Year, that is overridden by the server, as you said. Could be possible manage this behavior from a QueryInterceptor, to bring back again the order based on the Year? Is there a way to handle that inside the Operation itself, without requiring the $orderby parameter in the QueryString?Postboy
QueryInterceptor won't help you unfortunately, that only allows filtering of the results, nothing else. I really can't think of anything other than 1) modify the URL before it gets to the service, 2) Implement your own custom paging, which is pretty hard.Peltate
@VitekKarasMSFT, would you like to talk more about stable pading in OData? I posted a question here: #27686387Cutback

© 2022 - 2024 — McMap. All rights reserved.