locked
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

Answers

  • 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
    {
        [OperationContract]
        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");
            writer.WriteStartElement("thing");
            writer.WriteString("value");
            writer.WriteEndElement();
        }
        public override string Name
        {
           get { return " MyHeader "; }
        }
        public override string Namespace
        {
            get { return ""; }
        }
    }

        My Configuration:

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

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

    http://blogs.microsoft.co.il/blogs/bursteg/archive/2006/04/23/141.aspx

    http://developers.de/blogs/rolf_nebhuth/archive/2006/07/27/864.aspx

    http://msdn.microsoft.com/en-us/library/ms734675.aspx 

     custom message header

    Best Regards.

    Tuesday, November 3, 2009 12:04 AM