How to: Intercept Data Service Messages (ADO.NET Data Services)

With ADO.NET Data Services, you can intercept request messages so that you can add custom logic to an operation. To intercept a message, you use specially attributed methods in the data service. For more information, see Interceptors (ADO.NET Data Services).

The example in this topic uses the Northwind sample data service. This service is created when you complete the ADO.NET Data Services quickstart.

To define a query interceptor for the Orders entity set

  1. In the Northwind data service project, open the Northwind.svc file.

  2. In the code page for the Northwind class, add the following using statement (Imports in Visual Basic).

    Imports System.Linq.Expressions
    
    using System.Linq.Expressions;
    
  3. In the Northwind class, define a service operation method named OnQueryOrders as follows:

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

To define a change interceptor for the Products entity set

  1. In the Northwind data service project, open the Northwind.svc file.

  2. In the Northwind class, define a service operation method named OnChangeProducts as follows:

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

Example

This example defines a query interceptor method for the Orders entity set that returns a lambda expression. This expression contains a delegate that filters the requested Orders based on related Customers that have a specific contact name. The name is in turn determined based on the requesting user. This example assumes that the data service is hosted within an ASP.NET Web application that uses WCF, and that authentication is enabled. The HttpContext class is used to retrieve the principle of the current request.

' Define a query interceptor for the Orders entity set.
<QueryInterceptor("Orders")> _
Public Function OnQueryOrders() As Expression(Of Func(Of Orders, Boolean))
    ' Filter the returned orders to only orders 
    ' that belong to a customer that is the current user.
    Return Function(o) o.Customers.ContactName = _
        HttpContext.Current.User.Identity.Name
End Function
// Define a query interceptor for the Orders entity set.
[QueryInterceptor("Orders")]
public Expression<Func<Orders, bool>> OnQueryOrders()
{
    // Filter the returned orders to only orders 
    // that belong to a customer that is the current user.
    return o => o.Customers.ContactName ==
        HttpContext.Current.User.Identity.Name;
}

This example defines a change interceptor method for the Products entity set. This method validates input to the service for an Add() or Change() operation and raises an exception if a change is being made to a discontinued product. It also blocks the deletion of products as an unsupported operation.

' Define a change interceptor for the Products entity set.
<ChangeInterceptor("Products")> _
Public Sub OnChangeProducts(ByVal product As Products, _
                            ByVal operations As UpdateOperations)
    If operations = UpdateOperations.Add OrElse _
           operations = UpdateOperations.Change Then
        ' Reject changes to discontinued products.
        If product.Discontinued Then
            Throw New DataServiceException(400, _
                        "A discontinued product cannot be modified")
        End If
    ElseIf operations = UpdateOperations.Delete Then
        ' Block the delete and instead set the Discontinued flag.
        Throw New DataServiceException(400, _
            "Products cannot be deleted; instead set the Discontinued flag to 'true'")
    End If
End Sub
// Define a change interceptor for the Products entity set.
[ChangeInterceptor("Products")]
public void OnChangeProducts(Products product, UpdateOperations operations)
{
    if (operations == UpdateOperations.Add ||
       operations == UpdateOperations.Change)
    {
        // Reject changes to discontinued products.
        if (product.Discontinued)
        {
            throw new DataServiceException(400,
                        "A discontinued product cannot be modified");
        }
    }
    else if (operations == UpdateOperations.Delete)
    {
        // Block the delete and instead set the Discontinued flag.
        throw new DataServiceException(400, 
            "Products cannot be deleted; instead set the Discontinued flag to 'true'"); 
    }
}

See Also

Other Resources

How to: Define a Service Operation (ADO.NET Data Services)

Defining a Data Service (ADO.NET Data Services)