共用方式為


HOW TO:攔截資料服務訊息 (WCF Data Services)

使用 WCF Data Services ,您可以攔截要求訊息,因此您可以將自訂邏輯加入至作業。 若要攔截訊息,您可以特別使用資料服務中的屬性化方法。 如需詳細資訊,請參閱攔截器 (WCF Data Services)

本主題中的範例使用 Northwind 範例資料服務。 此服務會在您完成 WCF Data Services 快速入門時建立。

定義 Orders 實體集的查詢攔截器

  1. 在 Northwind 資料服務專案中,開啟 Northwind.svc 檔案。

  2. Northwind 類別的字碼頁中,加入下列 using 陳述式 (在 Visual Basic 中是 Imports)。

    Imports System.Linq.Expressions
    
    using System.Linq.Expressions;
    
  3. Northwind 類別中,定義名為 OnQueryOrders 的服務作業方法如下:

    ' 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()
    

定義 Products 實體集的變更攔截器

  1. 在 Northwind 資料服務專案中,開啟 Northwind.svc 檔案。

  2. Northwind 類別中,定義名為 OnChangeProducts 的服務作業方法如下:

    ' 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)
    

範例

這個範例定義 Orders 實體集的查詢攔截器方法,傳回 Lambda 運算式。 這個運算式包含的委派,可根據具有特定連絡人名稱的相關 Customers,篩選要求的 Orders。 名稱會根據要求的使用者依序判斷。 這個範例假設資料服務裝載於使用 WCF,並已啟用驗證的 ASP.NET Web 應用程式內。 HttpContext 類別會用來擷取目前要求的原則。

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

這個範例定義 Products 實體集的變更攔截器方法。 這個方法會驗證 AddChange 作業服務的輸入,如果停售的產品進行變更,將會引發例外狀況。 同時產品的刪除動作也會遭到封鎖而成為不支援的作業。

' Define a change interceptor for the Products entity set.
<ChangeInterceptor("Products")> _
Public Sub OnChangeProducts(ByVal product As Product, _
                            ByVal operations As UpdateOperations)
    If operations = UpdateOperations.Change Then
        Dim entry As System.Data.Objects.ObjectStateEntry

        If Me.CurrentDataSource.ObjectStateManager _
            .TryGetObjectStateEntry(product, entry) Then

            ' Reject changes to a discontinued Product.
            ' Because the update is already made to the entity by the time the 
            ' change interceptor in invoked, check the original value of the Discontinued
            ' property in the state entry and reject the change if 'true'.
            If CType(entry.OriginalValues("Discontinued"), Boolean) Then
                Throw New DataServiceException(400, String.Format(
                            "A discontinued {0} cannot be modified.", product.ToString()))
            Else
                Throw New DataServiceException(String.Format( _
                    "The requested {0} could not be found in the data source.", product.ToString()))
            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 If
End Sub
// Define a change interceptor for the Products entity set.
[ChangeInterceptor("Products")]
public void OnChangeProducts(Product product, UpdateOperations operations)
{
    if (operations == UpdateOperations.Change)
    {
        System.Data.Objects.ObjectStateEntry entry;

        if (this.CurrentDataSource.ObjectStateManager
            .TryGetObjectStateEntry(product, out entry))
        {
            // Reject changes to a discontinued Product.
            // Because the update is already made to the entity by the time the 
            // change interceptor in invoked, check the original value of the Discontinued
            // property in the state entry and reject the change if 'true'.
            if ((bool)entry.OriginalValues["Discontinued"])
            {
                throw new DataServiceException(400, string.Format(
                            "A discontinued {0} cannot be modified.", product.ToString()));
            }
        }
        else
        {
            throw new DataServiceException(string.Format(
                "The requested {0} could not be found in the data source.", product.ToString()));
        }
    }
    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'"); 
    }
}

另請參閱

工作

HOW TO:定義服務作業 (WCF Data Services)

其他資源

定義 WCF Data Services