【问题标题】:How to get custom SOAP header from WCF service response in Silverlight?如何从 Silverlight 中的 WCF 服务响应获取自定义 SOAP 标头?
【发布时间】:2011-03-23 13:33:09
【问题描述】:

我正在尝试在 Silverlight 应用程序中获取自定义响应消息头。

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

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

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

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

s:标题

headerName [ xmlns=headerNS ] 标头值

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

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

            }

有人遇到过类似的问题吗?

【问题讨论】:

    标签: silverlight wcf header


    【解决方案1】:

    在 Silverlight 上的响应中获取 SOAP 标头并不像应有的那么容易。如果您使用基于事件的回调,那么您就不走运了——它就是行不通。您需要使用 Begin/End 样式的操作调用,如下例所示。

    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.
          }
       });
    }
    

    请注意,您不能直接使用生成的客户端(来自 slsvcutil / 添加服务引用),您需要将其强制转换为接口,因为 Begin/End 方法未在客户端类上公开(显式实现)。

    【讨论】:

      【解决方案2】:

      要从 http 请求中获取标头,请尝试使用 Client HTTP stack

      最简单的方法是注册前缀,例如:

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

      【讨论】:

      • 感谢您的回答,但问题是关于 SOAP 消息头
      • 是的,我明白,但我感觉这仍然与 SL 中的 Http 客户端堆栈有关。我会尝试对此进行调查 - 也许我会找到帮助您的方法。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-01-04
      • 1970-01-01
      相关资源
      最近更新 更多