Add header to WCF SOAP message (with Binary Encoding) RRS feed

  • Question

  • I need to send in the WCF message a security token (some string) the best way I found was adding on the message header (for SOAP)  

    I know how to do when using BasicHttpBinding, but now I want to use CustomBinding using Binary encoding and I can't figure out how to do.


    thanks for the help.

    Wednesday, October 28, 2009 6:51 AM


  • Hi,

       Based on my knowledge , using binary encoding should not affect the way you custom a header. I did a test. Here is how i do it:

      My Service:

    public class MyService
        public string DoWork()
            OperationContext.Current.OutgoingMessageHeaders.Add(new MyMessageHeader());
            return OperationContext.Current.OutgoingMessageHeaders[0].ToString();
    public class MyMessageHeader : MessageHeader
        protected override void OnWriteHeaderContents(XmlDictionaryWriter writer, MessageVersion messageVersion)
            writer.WriteAttributeString("Test", "test");
        public override string Name
           get { return " MyHeader "; }
        public override string Namespace
            get { return ""; }

        My Configuration:

        <behavior name="MyServiceBehavior">
         <serviceMetadata httpGetEnabled="true" />
         <serviceDebug includeExceptionDetailInFaults="false" />
        <binding name="customBinding0">
         <binaryMessageEncoding />
         <httpTransport />
      <serviceHostingEnvironment aspNetCompatibilityEnabled="true" />
       <service behaviorConfiguration="MyServiceBehavior" name="MyService">
        <endpoint address="" binding="customBinding" bindingConfiguration="customBinding0"
         contract="MyService" />
        <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" />

     And my silverlight application comunicates with the wcf service correctly.  Here are some links you can take as references.




     custom message header

    Best Regards.

    Tuesday, November 3, 2009 12:04 AM