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

3

6

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

I found the following, which should work.

request.Headers.FindHeader("Auth", "xWow");
request.Headers.GetHeader<AuthHeader>(index);

If I manually find the right index and call the second line, it works as expected. However FindHeader is returning -1 as the index, even though I have confirmed in the watch window that those are the correct values for the name and namespace.

Hover answered 15/3, 2010 at 1:47 Comment(0)
H
6
request.Headers.FindHeader("Auth", "xWow");
request.Headers.GetHeader<AuthHeader>(index);
Hover answered 15/3, 2010 at 3:20 Comment(0)
M
4
HttpRequestMessageProperty requestProperty = 
    (HttpRequestMessageProperty)OperationContext.Current
        .IncomingMessageProperties[HttpRequestMessageProperty.Name];

string contextToken = requestProperty.Headers["MyCustomHeader"];
Mel answered 13/12, 2011 at 18:18 Comment(0)
B
1

I think that you need to add as third parameter the actor on the function FindHeader

Bogard answered 18/12, 2013 at 22:38 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.