Share via


Interceptors (WCF Data Services)

WCF Data Services enables an application to intercept request messages so that you can add custom logic to an operation. You can use this custom logic to validate data in incoming messages. You can also use it to further restrict the scope of a query request, such as to insert a custom authorization policy on a per request basis.

Interception is performed by specially attributed methods in the data service. These methods are called by WCF Data Services at the appropriate point in message processing. Interceptors are defined on a per-entity set basis, and interceptor methods cannot accept parameters from the request like service operations can. Query interceptor methods, which are called when processing an HTTP GET request, must return a lambda expression that determines whether an instance of the interceptor's entity set should be returned by the query results. This expression is used by the data service to further refine the requested operation. The following is an example definition of a query interceptor.

' Define a query interceptor for the Orders entity set.
<QueryInterceptor("Orders")> _
Public Function OnQueryOrders() As Expression(Of Func(Of Order, Boolean))
// Define a query interceptor for the Orders entity set.
[QueryInterceptor("Orders")]
public Expression<Func<Order, bool>> OnQueryOrders()

For more information, see How to: Intercept Data Service Messages (WCF Data Services).

Change interceptors, which are called when processing non-query operations, must return void (Nothing in Visual Basic). Change interceptor methods must accept the following two parameters:

  1. A parameter of a type that is compatible with the entity type of the entity set. When the data service invokes the change interceptor, the value of this parameter will reflect the entity information that is sent by the request.

  2. A parameter of type UpdateOperations. When the data service invokes the change interceptor, the value of this parameter will reflect the operation that the request is trying to perform.

The following is an example definition of a change interceptor.

' Define a change interceptor for the Products entity set.
<ChangeInterceptor("Products")> _
Public Sub OnChangeProducts(ByVal product As Product, _
                            ByVal operations As UpdateOperations)
// Define a change interceptor for the Products entity set.
[ChangeInterceptor("Products")]
public void OnChangeProducts(Product product, UpdateOperations operations)

For more information, see How to: Intercept Data Service Messages (WCF Data Services).

The following attributes are supported for interception.

  • [QueryInterceptor( EnitySetName )]
    Methods with the QueryInterceptorAttribute attribute applied are called when an HTTP GET request is received for the targeted entity set resource. These methods must always return a lambda expression in the form of Expression<Func<T,bool>>.

  • [ChangeInterceptor( EnitySetName )]
    Methods with the ChangeInterceptorAttribute attribute applied are called when an HTTP request other than HTTP GET request is received for the targeted entity set resource. These methods must always return void (Nothing in Visual Basic).

For more information, see How to: Intercept Data Service Messages (WCF Data Services).

See Also

Concepts

Service Operations (WCF Data Services)