Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Check out the <a href="http://wcfextras.codeplex.com/" rel="noreferrer">WCF Extras</a> on Codeplex - it's an easy extension library for WCF which offers - among other things - custom SOAP headers.</p> <p>Another option is to use <a href="http://msdn.microsoft.com/en-us/library/ms730255.aspx" rel="noreferrer">WCF message contracts</a> in your WCF service - this also easily allows you to define and set WCF SOAP headers.</p> <pre><code>[MessageContract] public class BankingTransaction { [MessageHeader] public Operation operation; [MessageHeader] public DateTime transactionDate; [MessageBodyMember] private Account sourceAccount; [MessageBodyMember] private Account targetAccount; [MessageBodyMember] public int amount; } </code></pre> <p>Here, the "operation" and the "transactionDate" are defined as SOAP headers. </p> <p>If none of those methods help, then you should check out the concept of WCF Message Inspectors which you can write as extensions. They allow you to e.g. inject certain headers into the message on every outgoing call on the client, and retrieving those from the message on the server for your use.</p> <p>See this blog post <a href="http://weblogs.asp.net/paolopia/archive/2008/02/25/handling-custom-soap-headers-via-wcf-behaviors.aspx" rel="noreferrer">Handling custom SOAP headers via WCF Behaviors</a> for a starting point on how to write a message inspector, and how to include it in your project setup.</p> <p>The client side <code>IClientMessageInspector</code> defines two methods <code>BeforeSendRequest</code> and <code>AfterReceiveReply</code> while the server side <code>IDispatchMessageInspector</code> has the opposite methods, i.e. <code>AfterReceiveRequest</code> and <code>BeforeSendReply</code>.</p> <p>With this, you could add headers to every message going across the wire (or selectively only to a few).</p> <p>Here's a snippet from a <code>IClientMessageInspector</code> implementor we use to automagically transmit the locale information (language and culture info) across from the clients to the server - should give you an idea how to get started:</p> <pre><code>public object BeforeSendRequest(ref Message request, IClientChannel channel) { International intlHeader = new International(); intlHeader.Locale = CultureInfo.CurrentUICulture.TwoLetterISOLanguageName; MessageHeader header = MessageHeader.CreateHeader(WSI18N.ElementNames.International, WSI18N.NamespaceURI, intlHeader); request.Headers.Add(header); return null; } </code></pre> <p>On the server side, you'd then check for the presence of those headers, and if present, extract them from the SOAP envelope and use them.</p> <p><strong>UPDATE:</strong> okay, you're clients are on .NET 2.0 and <strong>not</strong> using WCF - good news is, this should still work just fine - see this blog post <a href="http://blogs.msdn.com/nathana/archive/2007/05/29/custom-soap-headers-wcf-and-asmx.aspx" rel="noreferrer">Custom SOAP Headers: WCF and ASMX</a> for details. You could still use the message inspector on the server side to sniff and extract the custom headers being sent by your .NET 2.0 clients.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload