如何:设置客户端请求(WCF 数据服务)中的标头
使用 WCF 数据服务 客户端库访问支持 开放式数据协议 (OData) 的数据服务时,客户端库会自动在发送给数据服务的请求消息中设置所需 HTTP 标头。但是,在某些情况下客户端库不知道要设置所需的消息标头,例如当数据服务要求基于声明的身份验证或 Cookie 时。有关更多信息,请参见 Securing WCF Data Services。这时,必须先在请求消息中手动设置消息标头,然后再发送消息。本主题中的示例揭示了如何处理 SendingRequest 事件以便在将请求消息发送至数据服务之前在其中添加新标头。
本主题中的示例使用罗斯文示例数据服务和自动生成的客户端数据服务类。此服务和这些客户端数据类是在完成 WCF 数据服务快速入门时创建的。还可以使用 OData 网站上发布的罗斯文示例数据服务;该示例数据服务是只读的,它会尝试保存更改并返回错误。OData 网站上的示例数据服务允许进行匿名身份验证。
示例
以下示例为 SendingRequest 事件注册一个处理程序,然后执行针对数据服务的查询。
注意: |
---|
如果数据服务要求为每个请求手动设置消息标头,则考虑在代表数据服务的实体容器(在本例中为 NorthwindEntities)中覆盖 OnContextCreated 分部方法,以此注册 SendingRequest 事件的处理程序。 |
' 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}\nAddress:\n{1}\n{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\"");
}