Click here to Skip to main content
15,885,546 members
Articles / Programming Languages / C#

Add custom headers to a WCF channel

Rate me:
Please Sign up or sign in to vote.
2.38/5 (11 votes)
3 Jun 2009CPOL1 min read 84.5K   12   13
This piece describes how to add custom headers to a WCF channel and how to use them.

Introduction

Very often, when designing WCF based SOA services, we run into the need of passing some information repeatedly from the client to the service.

One example would be a user identity information like user ID or the client application identifier. The WCF service needs the user ID information for logging purposes. So for each method call, you have to pass that information.

One way would be to pass it as an additional request parameter. But, each and every method call needs to have this parameter(s) repeatedly. Not a very clean solution. Also, if the data type of this parameter changes, all the method signatures and their calls need to be changed.

A better way to handle this requirement is to add this information to the WCF channel header.

Let's code

First of all, we will add the custom header in the proxy constructor. (Hope you are not using Visual Studio to generate the proxies, or your code would be lost the next time you generate it. Proxy generation using Visual Studio is OK initially when you create the service. You do not have to keep on doing it for each change to the service methods. It is very simple to go to the proxy class and do it manually.)

C#
using System.ServiceModel;
using System.ServiceModel.Channels;

public partial class ZipServiceClient : 
       System.ServiceModel.ClientBase<IZipService>, IZipService
{
    //Proxy constructor
    public ZipServiceClient()
    {
        //Add basic information to the custom header
        AddCustomHeaderUserInformation(
           new OperationContextScope(base.InnerChannel));
    }
    
    // Scope is being passed. Note - If you have multiple service, you can move
    // this method to a common static class.
    // That way you would not have to same code all the proxy files.
    private static void AddCustomHeaderUserInformation(
                                 OperationContextScope scope)
    {
        //Add the basic userId
        MessageHeader<int> customHeaderUserID = 
          new MessageHeader<int>(<State.UserIDToken>);
        MessageHeader untypedHeaderUserID = 
          customHeaderUserID.GetUntypedHeader("UserID", 
          "CustomHeader");
}

The user ID here is being read from State(session). You would need to replace it to whatever storage location your information is in. Next, we will read the headers in our service.

C#
using System.Runtime.Serialization;
using System.ServiceModel; 

 // Read the headers 

int userID = 
  OperationContext.Current.IncomingMessageHeaders.GetHeader<int>(
  "UserID", "CustomHeader");

Note: These headers are available anywhere behind the WCF service. You can access them on the Service implementation code and from the layer behind the service.

That's it. It is quite straightforward. I hope I didn't miss any detail. Email me if you have any questions.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer (Senior)
United States United States
I have been designing and developing solutions on Microsoft platform for over 12 years in various roles and capacities.

Comments and Discussions

 
GeneralMy vote of 1 Pin
Daniel Murari Boatto17-Sep-14 4:05
Daniel Murari Boatto17-Sep-14 4:05 
GeneralMy vote of 3 Pin
Christian Amado30-Jul-12 4:48
professionalChristian Amado30-Jul-12 4:48 
QuestionYou are missing a detail Pin
jbrentonprivate19-Jul-12 10:27
jbrentonprivate19-Jul-12 10:27 
GeneralMy vote of 2 Pin
Marcelo Lujan [El Bebe.Net ]14-Feb-12 5:31
Marcelo Lujan [El Bebe.Net ]14-Feb-12 5:31 
GeneralMy vote of 5 Pin
Sandeepkumar Ramani15-Sep-11 3:00
Sandeepkumar Ramani15-Sep-11 3:00 
GeneralIssue adding the custom http headers in WCF routing service Pin
Vivekanand H19-Apr-11 4:36
Vivekanand H19-Apr-11 4:36 
GeneralMy vote of 1 Pin
steliodibello25-Aug-10 21:56
steliodibello25-Aug-10 21:56 
QuestionHow to add the header information Pin
Member 45016504-Mar-10 21:16
Member 45016504-Mar-10 21:16 
AnswerRe: How to add the header information Pin
Vivekanand H15-Apr-11 0:11
Vivekanand H15-Apr-11 0:11 
AnswerRe: How to add the header information Pin
Marcelo Lujan [El Bebe.Net ]14-Feb-12 7:56
Marcelo Lujan [El Bebe.Net ]14-Feb-12 7:56 
GeneralMy vote of 2 Pin
ramuknavap27-Oct-09 13:15
ramuknavap27-Oct-09 13:15 
QuestionHow? Pin
eidylon28-Sep-09 9:48
eidylon28-Sep-09 9:48 
I only give it three, for while it is a GREAT idea, your code does not show HOW to add the custom header to the OperationContext for each request. It created the custom header, but then does nothing with it.
GeneralMy vote of 2 Pin
Bad code hunter4-Jun-09 1:02
Bad code hunter4-Jun-09 1:02 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.