Adding a dynamic SoapHeader into a message
When designing a WCF ServiceContract
, we can add custom SoapHeaders into the WCF service operation through the ServiceContract
member statically. In addition to this, we can also programmatically create a custom SoapHeader and add it into a WCF request dynamically.
How to do it...
Create a custom SoapHeader instance.
The first thing to do here is to generate an instance of the SoapHeader we want to add into the request message. WCF provides a built-in generic type called
MessageHeader<T>
for creating custom SoapHeaders containing different kinds of inner data properties. The following code snippet shows how we can generate a custom SoapHeader that contains some string data through theMessageHeader<T>
type:MessageHeader<string> user = new MessageHeader<string>(Environment.UserName); MessageHeader userheader = user.GetUntypedHeader("ClientUser", "urn:test:calcservice");
Inject a custom SoapHeader object through
OperationContext...