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).
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.
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. |
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);
|
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.
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; }
3.137.219.117