How to get custom SOAP header from WCF service res

2019-07-12 14:39发布

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?

2条回答
姐就是有狂的资本
2楼-- · 2019-07-12 15:19

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.

查看更多
该账号已被封号
3楼-- · 2019-07-12 15:32

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);
查看更多
登录 后发表回答