如何从Silverlight中的WCF服务响应中获取自定义SOAP头? [英] How to get custom SOAP header from WCF service response in Silverlight?

查看:125
本文介绍了如何从Silverlight中的WCF服务响应中获取自定义SOAP头?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



在服务器端新的MessageHeader添加到响应标头中:

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

我可以在Fiddler中看到这个标题:


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



Header

headerName [xmlns = headerNS]
标题值


但是,我找不到在Silverlight应用程序服务回调中读取标头值的方法:

  using(new OperationContextScope(proxy.InnerChannel))
{
var headers = OperationContext.Current.IncomingMessageHeaders;
//标题为空:(


任何人遇到过类似的问题?

解决方案

在Silverlight的响应中获取SOAP标题并不像应该那样容易。使用基于事件的回调,你运气不好 - 它只是不起作用,你需要使用Begin / End-style操作调用,如下例所示。

  void Button_Click(...)
{
MyClient client = new MyClient();
IClient proxy =(IClient)client; //需要转换到[ServiceContract]接口
proxy.BeginOperation(hello,delegate(IAsyncResult asyncResult)
{
using(new OperationContextScope(client.InnerChannel))
{
proxy.EndOperation(asyncResult);
var headers = OperationContext.Current.IncomingMessageHeaders;
//现在你可以访问它了
}
});
}

请注意,您无法直接使用生成的客户端(来自slsvcutil / add服务引用),您需要将其转换为接口,因为Begin / End方法没有在客户端类中暴露(明确实现)。


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?

解决方案

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.

这篇关于如何从Silverlight中的WCF服务响应中获取自定义SOAP头?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆