Search code examples
wcfrestodatawcf-data-services

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


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.


Solution

  • 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.