How to: Set Headers in the Client Request (WCF Data Services)

 

When you use the WCF Data Services client library to access a data service that supports the Open Data Protocol (OData), the client library automatically sets the required HTTP headers in request messages sent to the data service. However, the client library does not know to set message headers that are required in certain cases, such as when the data service requires claims-based authentication or cookies. For more information, see Authentication in the Client Library. In these cases, you must manually set message headers in the request message before it is sent. The example in this topic shows how to handle the SendingRequest event to add a new header to the request message before it is sent to the data service.

The example in this topic uses the Northwind sample data service and autogenerated client data service classes. This service and the client data classes are created when you complete the WCF Data Services quickstart. You can also use the Northwind sample data service that is published on the OData Web site; this sample data service is read-only and attempting to save changes returns an error. The sample data services on the OData Web site allow anonymous authentication.

Example

The following example registers a handler for the SendingRequest event and then executes a query against the data service.

System_CAPS_noteNote

When a data service requires you to manually set the message header for every request, consider registering the handler for the SendingRequest event by overriding the OnContextCreated partial method in the entity container that represents the data service, which in this case is NorthwindEntities.

Example

The following method handles the SendingRequest event and adds an Authentication header to the request.

Show: