1

I am using AfterReceiveRequest() method as below

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

}

I am getting entire message header as shown below (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>

I want to read node using request.Headers.MyHeader

By default request.Headers is giving properties for To and Action nodes like shown below:

request.Headers.To
request.Headers.Action

Similary is there any way to get request.Headers.MyHeader ?

Any ideas is appreciated.

svick
  • 236,525
  • 50
  • 385
  • 514
Sanjay Sutar
  • 544
  • 1
  • 5
  • 18
  • Is [this](http://stackoverflow.com/questions/2444615/how-to-i-get-the-value-of-a-custom-soap-header-in-wcf?rq=1) helpfull? – rene Oct 31 '13 at 19:13

2 Answers2

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; 
}
Elie
  • 1,140
  • 2
  • 9
  • 16
0

I solved it by parsing the entire 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      
                  }
               }
          }
    }
Sanjay Sutar
  • 544
  • 1
  • 5
  • 18