Technical · Web Services

Adding Headers to SOAP Messages

December 5, 2010

Web Services

Adding Headers to SOAP Messages

For different effects (routing for example) it is necessary to add to the SOAP messages managed by WCF services a customized header (header) with information, in this way information can be sent between the clients and the services without it being necessary to modify the content of the service (data). A quick reminder of how to do it using code in WCF 3.5.

ServicioFuncionario.ServicioFuncionarioClient sv = new ServicioFuncionario.ServicioFuncionarioClient();
using (new OperationContextScope(sv.InnerChannel))
{
     MessageHeader headerLocal = MessageHeader.CreateHeader("Key", "http://serviciosproduccionCP", "HostCP");
     OperationContext.Current.OutgoingMessageHeaders.Add(headerLocal);

     MessageHeader headerBogota = MessageHeader.CreateHeader("Key", "http://serviciosproduccionBogota", "HostBogota");
     OperationContext.Current.OutgoingMessageHeaders.Add(headerBogota);

     resultado = sv.FuncionarioLogin(login, passWord);
}

sv.Close();

This code adds two headers to the funcionarioLogin request, the value HostCP and HostBogota is the one we are interested in adding to the messages, with that information the service could carry out different processes like routing, logging, error handling, backup among others.

Note1: in WCF 4.0 you can use the header tag in the service configuration file.

Note2: The protocol used in the communication must have support for header management in the SOAP message.

Juan Peláez

3Metas Corp

Need Expert Help?

Let's discuss how I can help with your technical challenges

Schedule a Consultation

Get the next one in your inbox

More like this, once a week. The Digest covers AI systems and agents in one weekly edition.

Confirm by email to complete your subscription. Unsubscribe anytime.

Related reading

More in the Digest — a weekly roundup of AI and agents news — or start with AI Strategy. Want help with this? Tell me about your needs →