This project has moved. 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)]
        [OperationContract]
        string In();
}


Example header type:

{
    [DataContract]
    public class Header
    {
        [DataMember]
        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:

<system.serviceModel>
  <client>
    <metadata>
      <wsdlImporters>
        <extension type="WCFExtras.Soap.SoapHeaderImporter, WCFExtras" />
        </wsdlImporters>
      </metadata>
    </client>
  </system.serviceModel>


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
  {
    get
    {
      return InnerChannel.GetHeader<Header>("MyHeader");
    }
    set
    {
      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 6:22 AM by crjansen, version 3

Comments

TacoWannee Oct 9, 2015 at 10: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 11:16 AM 
Should be <extension type="WCFExtrasPlus.Soap.SoapHeaderImporter, WCFExtrasPlus" /> in client side section