Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add a custom HTTP header to every WCF call?

Tags:

wcf

I have a WCF service that is hosted in a Windows Service. Clients that using this service must pass an identifier every time they're calling service methods (because that identifier is important for what the called method should do). I thought it is a good idea to somehow put this identifier to the WCF header information.

If it is a good idea, how can I add the identifier automatically to the header information. In other words, whenever the user calls the WCF method, the identifier must be automatically added to the header.

UPDATE: Clients that are using the WCF service are both Windows applications and Windows Mobile application (using Compact Framework).

like image 864
mrtaikandi Avatar asked Jun 08 '09 11:06

mrtaikandi


People also ask

How can I add authorization header to the request in WCF?

MessageHeader header = MessageHeader. CreateHeader("Authorization", "", "Basic Y19udGk6Q29udGlfQjNTVA=="); request. Headers. Add(header);

How do I add a SOAP header in WCF?

To publish metadata for WCF services using custom SOAP headers, you should manually create a Web Services Description Language (WSDL) file. You can use the externalMetadataLocation attribute of the <serviceMetadata> element in the Web. config file that the wizard generates to specify the location of the WSDL file.

Can you add custom HTTP headers?

Another example of using a custom HTTP header would be to implement the X-Pull header. You can use this custom header for a variety of purposes including rate limiting bandwidth on your origin server, restricting CDN traffic, creating custom logic on your origin server, etc.

How do I add HTTP header to request?

To add custom headers to an HTTP request object, use the AddHeader() method. You can use this method multiple times to add multiple headers. For example: oRequest = RequestBuilder:Build('GET', oURI) :AddHeader('MyCustomHeaderName','MyCustomHeaderValue') :AddHeader('MySecondHeader','MySecondHeaderValue') :Request.


1 Answers

The advantage to this is that it is applied to every call.

Create a class that implements IClientMessageInspector. In the BeforeSendRequest method, add your custom header to the outgoing message. It might look something like this:

public object BeforeSendRequest(ref System.ServiceModel.Channels.Message request, System.ServiceModel.IClientChannel channel) {     HttpRequestMessageProperty httpRequestMessage;     object httpRequestMessageObject;     if (request.Properties.TryGetValue(HttpRequestMessageProperty.Name, out httpRequestMessageObject))     {         httpRequestMessage = httpRequestMessageObject as HttpRequestMessageProperty;         if (string.IsNullOrEmpty(httpRequestMessage.Headers[USER_AGENT_HTTP_HEADER]))         {             httpRequestMessage.Headers[USER_AGENT_HTTP_HEADER] = this.m_userAgent;         }     }     else     {         httpRequestMessage = new HttpRequestMessageProperty();         httpRequestMessage.Headers.Add(USER_AGENT_HTTP_HEADER, this.m_userAgent);         request.Properties.Add(HttpRequestMessageProperty.Name, httpRequestMessage);     }     return null; } 

Then create an endpoint behavior that applies the message inspector to the client runtime. You can apply the behavior via an attribute or via configuration using a behavior extension element.

Here is a great example of how to add an HTTP user-agent header to all request messages. I am using this in a few of my clients. You can also do the same on the service side by implementing the IDispatchMessageInspector.

Is this what you had in mind?

Update: I found this list of WCF features that are supported by the compact framework. I believe message inspectors classified as 'Channel Extensibility' which, according to this post, are supported by the compact framework.

like image 91
Mark Good Avatar answered Sep 19 '22 17:09

Mark Good