Brick
Brick

Reputation: 13

How to get custom SOAP header from WCF service response in Silverlight?

I'm trying to get custom response message header in Silverlight application.

on server-side new MessageHeader added to response headers:

OperationContext.Current.OutgoingMessageHeaders.Add(MessageHeader.CreateHeader("headerName", "headerNS", "The header value"));

and I can see this header in Fiddler:

s:Envelope [ xmlns:s=http://schemas.xmlsoap.org/soap/envelope/ ]

s:Header

headerName [ xmlns=headerNS ] The header value

But, I can't find a way to read header value in Silverlight application service callback:

            using (new OperationContextScope(proxy.InnerChannel))
            {
                var headers = OperationContext.Current.IncomingMessageHeaders;
                // headers is null :(

            }

Does anyone encountered with similar issue?

Upvotes: 2

Views: 3635

Answers (2)

carlosfigueira
carlosfigueira

Reputation: 87228

Getting SOAP headers in responses on Silverlight isn't as easy as it should be. If you use the event-based callbacks, you're out of luck - it just doesn't work. You need to use the Begin/End-style operation call, like in the example below.

void Button_Click(...)
{
   MyClient client = new MyClient();
   IClient proxy = (IClient)client; // need to cast to the [ServiceContract] interface
   proxy.BeginOperation("hello", delegate(IAsyncResult asyncResult)
   {
      using (new OperationContextScope(client.InnerChannel))
      {
         proxy.EndOperation(asyncResult);
         var headers = OperationContext.Current.IncomingMessageHeaders;
         // now you can access it.
      }
   });
}

Notice that you cannot use the generated client (from slsvcutil / add service reference) directly, you need to cast it to the interface, since the Begin/End methods are not exposed (explicitly implemented) on the client class.

Upvotes: 1

Rafal Spacjer
Rafal Spacjer

Reputation: 4918

To get headers from http request try to use Client HTTP stack.

The easies way to do it is to register the prefix, for example:

WebRequest.RegisterPrefix("http://", WebRequestCreator.ClientHttp);

Upvotes: 0

Related Questions