How to read, alter, and update back the item values inside <soapenv: Body> tag in AfterReceiveRequest() method

288 Views Asked by At

I am now implementing AfterReceiveRequest(ref System.ServiceModel.Channels.Message request, System.ServiceModel.IClientChannel channel, System.ServiceModel.InstanceContext instanceContext) method of IDispatchMessageInspector interface to intercet the WCF SOAP message.

System.ServiceModel.Channels.Message request as below

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:tem="http://tempuri.org/">
  <soapenv:Header>
    <To soapenv:mustUnderstand="1" xmlns="http://schemas.microsoft.com/ws/2005/05/addressing/none">http://localhost:8993/TLS.svc/soap</To>
    <Action soapenv:mustUnderstand="1" xmlns="http://schemas.microsoft.com/ws/2005/05/addressing/none">http://tempuri.org/TLS/GetMerchant</Action>
  </soapenv:Header>
  <soapenv:Body>
      <tem:GetMerchant>
         <tem:item1>value1</tem:item1>
         <tem:item2>value2</tem:item2>
      </tem:GetMerchant>
   </soapenv:Body>
</soapenv:Envelope>

I would like to read and alter the value of <tem:item1></tem:item1> <tem:item2></tem:item2> and update back to System.ServiceModel.Channels.Message request without damaging other message original properties

<soapenv:Envelope ... >
    <soapenv:Header>
        <To soapenv:mustUnderstand="1" ... </To>
        <Action soapenv:mustUnderstand="1" ... </Action>
    </soapenv:Header>
    <soapenv:Body>
        <tem:GetMerchant>
            <tem:item1>Updatedvalue1</tem:item1>
            <tem:item2>Updatedvalue2</tem:item2>
        </tem:GetMerchant>
    </soapenv:Body>
</soapenv:Envelope>
1

There are 1 best solutions below

0
YankTHEcode On

This post describes the way you can handle it at Dispatcher level, which will allow you to intercept all incoming messages. You just need to understand and read it carefully. https://weblogs.asp.net/paolopia/writing-a-wcf-message-inspector