Click here to Skip to main content
12,405,498 members (54,002 online)
Click here to Skip to main content
Add your own
alternative version

Stats

80.6K views
29 bookmarked
Posted

Extract raw SOAP Message returned from Foreign Web Service after Deserialization Failure in a Web Service

, 11 Aug 2005
Rate this:
Please Sign up or sign in to vote.
Local web service calls third party web service which returns XML data and serialization fails, the article shows how to obtain the actual SOAP message.

Capturing SOAP messages that serialization cannot handle

In my job I have to write the code which consumes third party web services. In the course of working with differing companies' web services I discovered that some of those would return invalid XML data. That data was invalid due to either from an outdated schema or had some subtle problem that would cause Microsoft's deserializer to throw an exception. Since I needed to see the data sent to me, I had to work around the de-serialization operation and access the raw SOAP message which Microsoft's code refuse to divulge to my web service. This article explains I was able to end-around Microsoft by creating a specific attribute tied to a method call that allows me to see the SOAP message before serialization and will log the message for access when needed.

Notes

  • These steps are not how I coded it, but how you should proceed with the implementation.
  • This article does not cover the whys or hows of C# web services. It only details the acquisition of SOAP data in exceptional circumstances.
  • This problem occurred in a C# web service accessing a third party web service, hence the design taken below.

Step 1: Inserting a custom attribute into the proxy code

Since we are consuming a foreign web we either have to deal with Microsoft's generated proxy code or we have to create it by hand; regardless we need to find where the invoked call returns data. That is the location where we will place our specialized attribute.

If it is a generated proxy then you will need to look in the hidden file Reference.cs which resides in the web services folder. Once found, locate the primary method that returns data. That is the location were you will need to add the attribute that will handle the leg work of getting around the de-serialization situation.

// Inside proxy class before data extraction call:
// Here is our attribute to capture 
// soap and log it for us.
[ClientSoapLogger()] 
public zzzResponse ProcessData(...
{
  object[] results = this.Invoke(...
  return((zzzResponse)(results[0]));
}

Notes

  • If this is the generated proxy code, you will need to add the attribute every time the code is re-generated.
  • You will need to add the reference to the namespace that the attribute code resides in as shown in future steps. The build process will remind you if you forget.

Step 2: Defining custom attribute SoapExtensionAttribute

The attribute is the hook for our data logging process. Here is the class which is the attribute with the appropriate overrides.

/// <span class="code-SummaryComment"><summary>The attribute [ClientSoapLogger()] 
</span>

Step 3: Creating the SoapExtension

The SOAP extension will work with the flow of the handling of SOAP message in an autonomous fashion. Our goal is not to damage the data stream, only to look into it for data extraction.

/// <span class="code-SummaryComment"><summary>This class handles the data 
</span>

Step 4: Logging the data

This is the step that really got to me. If your code is in a web service (mine was a web service calling a web service) how does one pass data between the SOAP process and the code accessing the proxy? In some situations such as ASP.NET one can put the message on the HttpContext, but that wasn't an option for my code in a web service. The below code handles the situation of a null HttpContext by placing the data in the Remoting call context.

/// <span class="code-SummaryComment"><summary>Publish out the message received 
</span>

Step 5: Consuming the logged data

For my web service if the incoming data was invalid, my indication was either an exception thrown by the de-serializer or simply a null return value by the data access method. At that point my code tries to do a post extract of the SOAP raw data and handles the message.

[Editor Note: Line breaks used to avoid scrolling.]

// New class outside ClientSoapLogger...

/// <span class="code-SummaryComment"><summary>Examine the soap return for possible missing 
</span>

Notes

  • Let me know if there is possibly a better way or things could be improved.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

Share

About the Author

OmegaMan
Web Developer
United States United States
After finding myself with lots of time after a cataclysmic event I have turned to doing programming to help fill the hours.

You may also be interested in...

Comments and Discussions

 
GeneralStream was not readable Pin
GhanashyamL15-Nov-07 23:02
memberGhanashyamL15-Nov-07 23:02 
GeneralRe: Stream was not readable Pin
GhanashyamL15-Nov-07 23:38
memberGhanashyamL15-Nov-07 23:38 
GeneralRe: Stream was not readable Pin
Hardy27-Feb-08 14:18
memberHardy27-Feb-08 14:18 
Question.NET SOAP Question Pin
dramsey1128-Aug-07 4:32
memberdramsey1128-Aug-07 4:32 
GeneralBrilliant! Pin
mythilee kulkarni21-Feb-07 6:14
membermythilee kulkarni21-Feb-07 6:14 
Questionmissing method? Pin
biconix24-Aug-05 11:00
memberbiconix24-Aug-05 11:00 
AnswerRe: missing method? Pin
OmegaMan25-Aug-05 6:47
memberOmegaMan25-Aug-05 6:47 
QuestionAlternate way? Pin
bigals11-Aug-05 12:22
memberbigals11-Aug-05 12:22 
AnswerRe: Alternate way? Pin
OmegaMan25-Aug-05 6:48
memberOmegaMan25-Aug-05 6:48 
AnswerRe: Alternate way? Pin
Nermin Dibek23-Nov-05 4:42
memberNermin Dibek23-Nov-05 4:42 
GeneralRe: Alternate way? Pin
Derwisch16-Dec-05 2:38
memberDerwisch16-Dec-05 2:38 
GeneralRe: Alternate way? Pin
Nermin Dibek16-Dec-05 3:48
memberNermin Dibek16-Dec-05 3:48 
GeneralRe: Alternate way? Pin
Derwisch19-Dec-05 4:17
memberDerwisch19-Dec-05 4:17 
AnswerRe: Alternate way? Pin
Hardy27-Feb-08 13:44
memberHardy27-Feb-08 13:44 
GeneralRe: Alternate way? Pin
Jagadeesh Babu Molabanti7-Mar-09 2:16
memberJagadeesh Babu Molabanti7-Mar-09 2:16 
AnswerRe: Alternate way? Pin
KbCb22-Sep-10 7:05
memberKbCb22-Sep-10 7:05 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.160721.1 | Last Updated 11 Aug 2005
Article Copyright 2005 by OmegaMan
Everything else Copyright © CodeProject, 1999-2016
Layout: fixed | fluid