SkeetJon
SkeetJon

Reputation: 1491

How to inject MessageHeaders into WCF calls

I am trying to consume a WCF service that requires MessageHeaders to be present. I can create the Headers, and also call methods on the service

            factory = new ChannelFactory<IMyService>("*");
            proxy = factory.CreateChannel();

            var ns = "http://www.myNamespace/...";
            var header = MessageHeader.CreateHeader("Username", ns, "foo");

            // Current is null <--
            OperationContext.Current.OutgoingMessageHeaders.Add(header);


            return proxy.CallMyMethod();

The problem is that the Current in OperationContext is null, so my question is how do I inject these headers in to the message please ?

Upvotes: 0

Views: 377

Answers (1)

H. Mahida
H. Mahida

Reputation: 2366

Have you created object for OperationContext ?

try putting your Add header code in

  using (OperationContextScope scope = new OperationContextScope(((IContextChannel) proxy))
  {
          // your code to add custom header
  }

Hope it helps..!!!

Upvotes: 3

Related Questions