WCF OutgoingMessageHeaders does not save values

I'm trying to write a MessageHeader for OutgoingMessageHeaders but the value doesn't stick.

BasicHttpBinding basicHttpBinding = new BasicHttpBinding();
EndpointAddress endpointAddress = new EndpointAddress("http://localhost:1003/Client.svc");

IClientService serviceClient = new ChannelFactory<IClientService>(basicHttpBinding, endpointAddress).CreateChannel();

// attempt 1
using (OperationContextScope scope = new OperationContextScope(serviceClient as IContextChannel)) 
{
    OperationContext.Current.OutgoingMessageHeaders.Add(MessageHeader.CreateHeader("SessionId","","ABC"));
}

// attempt 2
using (OperationContextScope scope = new OperationContextScope(serviceClient as IContextChannel)) 
{
    OperationContext.Current.OutgoingMessageHeaders.Add(MessageHeader.CreateHeader("SessionId","","ABC"));
}

      

OK, you can see that I am setting OutgoingMessageHeaders twice, but that is just to prove the point. On my second try, before I make the actual addition, I check the OperationContext.Current.OutgoingMessageHeaders. I would have expected it to have one entry. But it is zero. As soon as it goes out of scope, the value is lost.

When this spills over to the server, it says it can't find the message header, indicating that as far as it goes, it hasn't been saved either.

Why isn't my MessageHeader sticking?

Jeff

+2


source to share


1 answer


The end of the block in use causes the previous OperationContext to be deleted and reset.

So you want something like this with a service call inside an OperationContextScope.



        using (OperationContextScope scope = new OperationContextScope(serviceClient as IContextChannel))
        {
            OperationContext.Current.OutgoingMessageHeaders.Add(MessageHeader.CreateHeader("SessionId", "", "ABC"));
            serviceClient.CallOperation();
        }

      

+8


source







All Articles