This project has moved and is read-only. For the latest updates, please go here.

SOAP Headers

Server Side

Place a SoapHeaders attribute on your Service Contract (The interface with the ServiceContract attribute). This marks this service as one that uses SOAP headers

    [SoapHeaders] //This attribute signals the wsdl exporter that this contract uses SoapHeaders
    [ServiceContract(Namespace = "http://WCFExtrasPlus/Samples")]
    public interface ISoapHeadersSample

Place a SoapHeader attribute on operation contracts that use SOAP headers. The first parameter is the name of the header. The second is the type that will represent this header. This type must have a DataContract attribute on it. The third parameter specifies the direction of this header. Valid values are In, Out, InOut.

        [SoapHeader("MyHeader", typeof(Header), Direction = SoapHeaderDirection.In)]
        string In();

Example header type:

    public class Header
        public string Value { get; set; }

To access the header inside WCF calls, use the SoapHeaderHelper class:
Accessing an "Input" direction header:
Header soapHeader = SoapHeaderHelper<Header>.GetInputHeader("MyHeader"); 
string value = soapHeader.value; 

Setting an "Output" direction header:
Header header=new Header();
header.value="Some value";
SoapHeaderHelper<Header>.SetOutputHeader("MyHeader", header);

Client Side

To configure clients to use the custom importers, you should first add a reference to the WCFExtrasPlus assembly on the client and then add a section to the client app.config file.

To generate a client proxy that is "aware" of soap headers declared on the server add the following section to your app.config file:

        <extension type="WCFExtras.Soap.SoapHeaderImporter, WCFExtras" />

Extend the generated proxy by adding properties to the proxy class (Where "MyHeader" is the header name and Header is the class representing the header):

public partial class SoapHeadersSampleClient
  public Header MyHeader
      return InnerChannel.GetHeader<Header>("MyHeader");
      InnerChannel.SetHeader("MyHeader", value);

Now you can write code like the following:

  client.MyHeader = new Header() { Value = "Test Input" }; //This will be automatically sent to the server in every request
  string result = client.In(); 
  //If a method on the server changes the value of the header, you can access the new value like this
  result = client.MyHeader.Value;

Last edited Apr 17, 2011 at 7:22 AM by crjansen, version 3


TacoWannee Oct 9, 2015 at 11:15 AM 
It works fine with me as long as I only add one object to it. When adding another, I get a run time error on the client when creating instance of service:
An unhandled exception of type 'System.InvalidOperationException' occurred in System.Xml.dll
Additional information: There was an error reflecting type 'client.SoapHeader'

Shevek Jul 8, 2011 at 12:16 PM 
Should be <extension type="WCFExtrasPlus.Soap.SoapHeaderImporter, WCFExtrasPlus" /> in client side section