How to I get the value of a custom soap header in WCF

Posted by Jason Coyne on Stack Overflow See other posts from Stack Overflow or by Jason Coyne
Published on 2010-03-15T01:47:40Z Indexed on 2010/03/15 1:49 UTC
Read the original article Hit count: 534

I have created a custom soap header, and added it into my message via IClientMessageInspector

public object BeforeSendRequest(ref System.ServiceModel.Channels.Message request, System.ServiceModel.IClientChannel channel)
    {
        var header = new MessageHeader<AuthHeader>();
        header.Content = new AuthHeader(Key);
        header.Actor = "Anyone";
        var header2 = header.GetUntypedHeader("Auth", "xWow");
        request.Headers.Add(header2);
        return null;
    }

   [DataContract(Name="Auth")]
    public class AuthHeader
    {
        public AuthHeader(string key)
        {
            this.Key = key;
        }

        [DataMember]
        public string Key { get; set; }
    }

I also have an IDispatchMessageInspector, and I can find the correct header in the list. However, there is no value. I know that the value went across the wire correctly, because the message string is correct

<s:Envelope xmlns:s=\"http://schemas.xmlsoap.org/soap/envelope/\">\r\n  <s:Header>\r\n    <Auth s:actor=\"Anyone\" xmlns=\"xWow\" xmlns:i=\"http://www.w3.org/2001/XMLSchema-instance\">\r\n      <Key xmlns=\"http://schemas.datacontract.org/2004/07/xWow.Lib\">HERE IS MY KEY VALUE!!!!</Key>\r\n    </Auth>\r\n    <To s:mustUnderstand=\"1\" xmlns=\"http://schemas.microsoft.com/ws/2005/05/addressing/none\">http://localhost:26443/AuthService.svc</To>\r\n    <Action s:mustUnderstand=\"1\" xmlns=\"http://schemas.microsoft.com/ws/2005/05/addressing/none\">http://tempuri.org/IAuthService/GetPayload</Action>\r\n  </s:Header>\r\n  <s:Body>\r\n    <GetPayload xmlns=\"http://tempuri.org/\" />\r\n  </s:Body>\r\n</s:Envelope>"

But there does not seem to be any property to retrieve this value. The MessageHeaderInfo class has Actor, etc, but nothing else useful I can find.

On the client side I had to convert between Header and Untyped header, is there an equivalent operation on the server?

© Stack Overflow or respective owner

Related posts about wcf

Related posts about soap