9.2. Querying WCF Data Services

WCF Data Services uses the URL to pass query parameters. For example, to retrieve the film entities, add /Films to the end of the existing URL (for example, http://localhost/Chapter9/MovieService.svc/Films).

You should then be returned a list of all the films in AtomPub format (Figure 9-8).

Figure 9.8. Returning all the films from the database

Having all the data returned at once isn't too useful, so WDS supports a number of query operators that all work with the URL.

I have listed some commonly used methods in Table 9-1, but for a full list, please refer to http://msdn.microsoft.com/en-us/library/cc907912.aspx.

Table 9.1. Common Query Operators
ActionOperator
Get film with ID 3Films(3)
Select film where FilmID is equal to 3Films?$filter=FilmID%20eq%203
Get FilmName field from first film showingsFilmShowings(1)/Film/Title
Get film showings for first filmFilms(1)/FilmShowings
Display orders by order dateOrders?$orderby=OrderDate
Order list of films in descending orderOrders?$orderby=OrderDate%20desc
Select top two ordersOrders?$top=2
Skip first orders and select next twoOrders?$skip=1&top=2

When working with WCF Data Services, you should note the following:

  • Objects are case sensitive. Film is not the same as film (grrrrr!).

  • The query needs to be URL-encoded, so spaces must be encoded as %20.

  • Note the use of the ? query string character and $ to specify options.

IS THERE A WAY TO LIMIT THE NUMBER OF RECORDS RETURNED?

Yes—when you are using WCF Data Services version 1.5. Please refer to the "WDS 1.5" section.


9.2.1. Security in WCF Data Services

WDS allows you to fine-tune access to individual entities. For example, open MovieService.cs, and find this line:

config.SetEntitySetAccessRule("*", EntitySetRights.AllRead);

This code allows read-only access to all entities (defined by the *). If you wanted to enable full access to everything (which is generally a very bad idea but useful for testing), then you can change this to the following:

config.SetEntitySetAccessRule("*", EntitySetRights.All);

If you do this, anyone who can access the data service will have full access.


You can apply permissions to individual objects by specifying them by name. The following line of code will enable full access to the Film entity:

config.SetEntitySetAccessRule("Films", EntitySetRights.All);

You will need full access to the Film entity in a few minutes, so add the previous line to MovieService.cs.

9.2.2. Query Interceptors

Sometimes you might want to intercept the user's query to apply additional logic to it (for example, to filter depending on the current user). WDS allows you to do this through query interceptors.

The following example demonstrates how to apply this technique to any films requests to allow only those where FilmID equals 1. Add the following code to MovieService.svc:

using System.Linq.Expressions;

[QueryInterceptor("Films")]
public Expression<Func<Film, bool>> FilterOnlyShowFilmID1()
{
    return f => f.FilmID==1;
}

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.137.219.117