1

我正在使用 AfterReceiveRequest() 方法,如下所示

public object AfterReceiveRequest(ref System.ServiceModel.Channels.Message request, System.ServiceModel.IClientChannel channel, System.ServiceModel.InstanceContext instanceContext)
{

}

我得到了如下所示的整个消息头( request.ToString()):

<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/">

<s:Header>

<MyHeader xmlns:i="http://www.w3.org/2001/XMLSchema-instance">
  <AppIp xmlns="http://schemas.datacontract.org/2004/07/WcfTest">192.168.1.0</AppIp>
  <AppPwd xmlns="http://schemas.datacontract.org/2004/07/WcfTest">UITPass</AppPwd>
  <Appid xmlns="http://schemas.datacontract.org/2004/07/WcfTest">UIT</Appid>
  <ShibbolethSessionID i:nil="true" xmlns="http://schemas.datacontract.org/2004/07/WcfTest" />
  <UCLALogonID i:nil="true" xmlns="http://schemas.datacontract.org/2004/07/WcfTest" />
  <UserUID i:nil="true" xmlns="http://schemas.datacontract.org/2004/07/WcfTest" />
</MyHeader>

<To s:mustUnderstand="1" xmlns="http://schemas.microsoft.com/ws/2005/05/addressing/none">http://localhost:60729/Service/StudyListService.svc</To>
<Action s:mustUnderstand="1" xmlns="http://schemas.microsoft.com/ws/2005/05/addressing/none">http://tempuri.org/IStudyListService/GetStudyListData</Action>

</s:Header>

</s:Envelope>

我想读使用 request.Headers 的节点。我的标题

默认情况下 request.Headers 为ToAction节点提供属性,如下所示:

request.Headers.To
request.Headers.Action

类似地有什么方法可以获取request.Headers.MyHeader吗?

任何想法表示赞赏。

4

2 回答 2

1
public object AfterReceiveRequest(ref Message request, IClientChannel channel, InstanceContext instanceContext)
{
    var header = request.Headers.GetHeader<MyHeader>("MyHeader", "replace this with you custom namespace uri");

    var userId = header.UserUID;

    // ...
    // ...
    // ...

    return null; 
}
于 2013-10-31T20:16:22.867 回答
0

我通过解析整个 XML 来解决它:

var requestXML = request.ToString();
var headerData = System.Text.Encoding.UTF8.GetBytes(requestXML);
  using (MemoryStream memoryStream = new MemoryStream(headerData))
  {
         using (XmlReader xmlReader = new XmlTextReader(memoryStream))
         {
              xmlReader.MoveToContent();
              while (xmlReader.Read())
              {
                  if (xmlReader.NodeType == XmlNodeType.Element)
                  { 
                      //read whatever element is desired      
                  }
               }
          }
    }
于 2013-11-13T23:15:01.523 回答