Share via


Using Custom SOAP Headers

Here is a simple way of adding custom SOAP Headers on an outgoing request.

Write a Data class as shown below:

[DataContract(Namespace = “http://personalspace.org”, Name ="CustomHeader")]
public class CustomHeader
{
private string something;
[DataMember(Name=”CustomElement”)”]
public string someThing
{
get { return something; }
set { something= value; }
}
}

In the BeforeSendRequest() method of the class that you have implemented the IClientMessageInterceptor interface, add the code below

MessageHeader<CustomHeader> header = new MessageHeader<CustomHeader>();
MyHeaderClass customHeader= new MyHeaderClass();
customHeader.someThing = "My value";
header.Content = header;
header.Actor = “anonymoususer”;

MessageHeader unTypedHeader =
header.GetUntypedHeader("CustomHeader", “http://personalspace.org”);
request.Headers.Add(unTypedHeader);

That’s it. This will generate a header similar to the one below:

<CustomHeader:actor="Anyone" xmlns="http://personalspace.org">
<CustomElement>My Value</CustomElement>
</CustomHeader>

Enjoy !!