Jul 05 2011

Altering ALL Soap headers in WSE2

How to modify SOAP headers in Web Service Enhancements 2.0 (WSE2)

I faced with a big problem when I tried to remove wsu:TimeStamp from the header of a Soap envelop created by Web Service Enhancements 2.0 (WSE2).

After a lot of trials and a deep insight into the code I realized that the Pipeline object filters the envelope in inverse order, so the custom filters added to the pipeline are executed before the security tokes are added to the header.

In order to filter the complete header is important that the filter acts as the last filter in the pipeline. To do this the custom filter can be added to the pipeline with an Pipeline.Filter.Insert(0,CustomFilter) instead of  Pipeline.Filter.Add(CustomFilter).

The complete code can be something like this:


public class YourCustomFilter:SoapOutputFilter

{

public YourCustomFilter()
 {

 }
 public override void  ProcessMessage(SoapEnvelope envelope)
 {
 RemoveTag(envelope.DocumentElement, "wsu:Timestamp");

 }
 private void RemoveTag(System.Xml.XmlNode XE, string TagName )
 {
 foreach(XmlNode N in XE)
 {
 if (N.Name==TagName) XE.RemoveChild(N);
 else if (N.ChildNodes.Count>0) RemoveTag(N,TagName);

 }
 }

//Apply filter as last filter in the queue

YourSoapClient ServiceRequest=new YourSoupClient();
 YourCustomFilter Filter=new YourCustomFilter();
 ServiceRequest.Pipeline.OutputFilters.Insert(0,Filter);

Comments Off

Comments are closed at this time.