共用方式為


HOW TO:設定用戶端要求中的標頭 (WCF Data Services)

當您使用 WCF Data Services 用戶端程式庫存取支援 Open Data Protocol (OData) 的資料服務時,用戶端程式庫會在傳送至資料服務的要求訊息中自動設定必要的 HTTP 標頭。 不過,在某些情況下,用戶端程式庫不知道要設定所需的訊息標頭,例如,當資料服務需要宣告架構的驗證或 Cookie 時。 如需詳細資訊,請參閱Securing WCF Data Services。 在這些情況下,您必須在以要求訊息中手動設定訊息標頭,然後再傳送出去。 本主題中的範例會示範如何處理 SendingRequest 事件,以便將新的標頭加入至要求訊息中,然後再傳送至資料服務。

本主題中的範例使用 Northwind 範例資料服務和自動產生的用戶端資料服務類別。 此服務和用戶端資料類別會在您完成 WCF Data Services 快速入門時建立。 您也可以使用在 OData 網站上發佈的 Northwind 範例資料服務;此範例資料服務是唯讀的,因此嘗試儲存變更時,會傳回錯誤。 OData 網站上的範例資料服務允許匿名驗證。

範例

下列範例會註冊 SendingRequest 事件的處理常式,然後根據資料服務執行查詢。

注意

當資料服務要求您手動設定每個要求的訊息標頭時,請考慮註冊 SendingRequest 事件的處理常式,方法是,覆寫表示資料服務之實體容器中的 OnContextCreated 部分方法,在此情況下為 NorthwindEntities。

' Create the DataServiceContext using the service URI.
Dim context As NorthwindEntities = New NorthwindEntities(svcUri)

' Register to handle the SendingRequest event.
' Note: If this must be done for every request to the data service, consider
' registering for this event by overriding the OnContextCreated partial method in 
' the entity container, in this case NorthwindEntities. 
AddHandler context.SendingRequest, AddressOf OnSendingRequest

' Define a query for orders with a Freight value greater than 30.
Dim query = From cust In context.Customers _
    Where cust.Country = "Germany" _
    Select cust

Try
    ' Enumerate to execute the query.
    For Each cust As Customer In query
        Console.WriteLine("Name: {0}" & vbNewLine & "Address:" & vbNewLine & "{1}" _
                          & vbNewLine & "{2}, {3}", _
            cust.CompanyName, cust.Address, cust.City, cust.Region)
    Next
Catch ex As DataServiceQueryException
    Throw New ApplicationException( _
        "An error occurred during query execution.", ex)
End Try
// Create the DataServiceContext using the service URI.
NorthwindEntities context = new NorthwindEntities(svcUri);

// Register to handle the SendingRequest event.
// Note: If this must be done for every request to the data service, consider
// registering for this event by overriding the OnContextCreated partial method in 
// the entity container, in this case NorthwindEntities. 
context.SendingRequest += new EventHandler<SendingRequestEventArgs>(OnSendingRequest);

// Define a query for orders with a Freight value greater than 30.
var query = from cust in context.Customers
    where cust.Country == "Germany"
    select cust;

try
{
    // Enumerate to execute the query.
    foreach (Customer cust in query)
    {
        Console.WriteLine("Name: {0}\nAddress:\n{1}\n{2}, {3}",
            cust.CompanyName, cust.Address, cust.City, cust.Region);
    }
}
catch (DataServiceQueryException ex)
{
    throw new ApplicationException(
        "An error occurred during query execution.", ex);
}

下列方法會處理 SendingRequest 事件,並將驗證標頭加入至要求中。

Private Shared Sub OnSendingRequest(ByVal sender As Object, ByVal e As SendingRequestEventArgs)
    ' Add an Authorization header that contains an OAuth WRAP access token to the request.
    e.RequestHeaders.Add("Authorization", "WRAP access_token=""123456789""")
End Sub
private static void OnSendingRequest(object sender, SendingRequestEventArgs e)
{
    // Add an Authorization header that contains an OAuth WRAP access token to the request.
    e.RequestHeaders.Add("Authorization", "WRAP access_token=\"123456789\"");
}

請參閱

概念

保護 WCF Data Services 的安全

其他資源

資料用戶端 (WCF Data Services)