ADO.NET/WCF Data Service Interceptors

There are two possible interceptions you can perform on an ADO.NET Data Service:

  • Query interception: intercept a query made for an entity in order to enforce a restriction;
  • Change interception: intercept a request for updating an entity and possibly cancel it, or change the entity.

Query interceptions are achieved by marking a method with attribute [QueryInterceptor]; the method must have the following signature:

[QueryInterceptor("Author")]
public Expression<Func<Author, Boolean>> OnFilterAuthor()
{
    return (author => author.Name == "Ricardo Peres");
}

Regardless of the query that you specify on the client proxy or the URL, this restriction is always enforced. The name and visibility are ignored.

Interception queries, on the other hand, allow you to change the entity that is being submitted for persistence, or cancel the operation totally. Decorate a method with a [ChangeInterceptor] attribute and add this arguments:

[ChangeInterceptor("Author")]
public void OnChangeAuthor(Author author, UpdateOperations operations)
{
    if (operations == UpdateOperations.Add)
    {
nbsp;       author.Name = "Ricardo Peres";
    }}

Once again, the visibility and name do not count.

If you want to have restrictions based on the caller's identity (HttpContext.Current.User), you must enable ASP.NET compatibility mode:

[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Required)]
public class MyDataContext: DataService<MyEntities>

And also enable it on the Web.config:

<system.serviceModel>
        <serviceHostingEnvironment aspNetCompatibilityEnabled="true" />
    </system.serviceModel>

Bookmark and Share

                             

No Comments

Add a Comment

As it will appear on the website

Not displayed

Your website