1

I have a VB.NET (2008) console application which consumes a web service. All is working fine -- I've used WSDL.exe to import the web service's definition and create the appropriate classes, I can interact with those classes to fill them up with data, and the call to the web service succeeds and data is passed.

However, after the successful transfer of data, I would like to log this event to a table in my Oracle database -- including the XML that was transferred (for review at a later time, in case there are questions about the specific data which was passed, etc.). How can I get the XML that was created through the automatic serialization of these objects (by the .NET internal plumbing)?

I can serialize the objects myself and get an approximate representation of the data, for example:

Dim oStringBuilder As New StringBuilder
Dim oStringWriter As New StringWriter(oStringBuilder)
Dim oXmlTextWriter As New XmlTextWriter(oStringWriter)

'-- (oData, for the purposes of this example, is an instance of the main data class explosed in the code generated by WSDL.exe)
Dim oXmlSerializer as New System.Xml.Serialization.XmlSerializer(oData.GetType)

'-- Serialize the data to a StringBuilder
oXmlTextWriter.Formatting = Formatting.Indented
oXmlSerializer.Serialize(oXmlTextWriter, oData)

'-- Display serialized XML (in practice this would be an insert to a table, etc.)
MessageBox.Show(oStringBuilder.ToString)

... however, this really isn't the same as what actually gets passed to the web service (most notably, some of the element names are different, and it doesn't include the same header information and root elements). There are classes with Serialization.XmlTypeAttribute definitions, etc., which I assume are being used by the .NET framework to do handle the real SOAP plumbing. How can I get the actual XML that is passed to the web service?

John Saunders
  • 160,644
  • 26
  • 247
  • 397
Marcoid
  • 53
  • 1
  • 7
  • Welcome to Stack Overflow! First of all, you're using old, legacy technology that shouldn't be used for new development. WCF should be used for all new development of web service clients and servers. One hint: Microsoft has retired the [ASMX Forum](http://social.msdn.microsoft.com/Forums/en-US/asmxandxml/threads) on MSDN. – John Saunders Apr 19 '13 at 04:07
  • Second, you should not use `new XmlTextReader()` or `new XmlTextWriter()`. They have been deprecated since .NET 2.0. Use `XmlReader.Create()` or `XmlWriter.Create()` instead. – John Saunders Apr 19 '13 at 04:08
  • Finally, there is no value in placing "Microsoft .NET:" in front of your title. I have edited it. Please see, "[Should questions include “tags” in their titles?](http://meta.stackexchange.com/questions/19190/)", where the consensus is "no, they should not". – John Saunders Apr 19 '13 at 04:10
  • Thanks for the tips, John -- not only regarding the old technology vs. WCF and the XmlWriter, but also the etiquette for question titles. Regarding WCF, after a little research I was able to implement it pretty easily. I've been programming for a long time, but web services are new to me and this was all self-taught (combined with some knowledge remembered on web services back int the .NET 1.x days); so I guess I missed the subtle difference in WCF vs. the legacy technology. In any case, though, it was a learning experience. – Marcoid Apr 20 '13 at 03:28

1 Answers1

0

If you have no choice other than to use the legacy ASMX technology (WSDL.EXE, "Add Web Reference"), then you should use a SoapExtension to log your service requests and responses.

John Saunders
  • 160,644
  • 26
  • 247
  • 397
  • Thanks, John. I'll take a look at SoapExtension. I've converted my code over to WCF (and in the process, ran into a new show-stopper of an issue -- http://stackoverflow.com/questions/16116211/send-username-and-password-in-clear-text-to-web-service-over-http). However, if I am able to overcome that issue and still use WCF, is there a preferred way to get this serialized Xml with WCF? – Marcoid Apr 20 '13 at 03:31
  • WCF Message tracing will get it where you can see it, and possibly log it. You can use other techniques to extend the WCF pipeline and put the XML where you'd like it to be. – John Saunders Apr 20 '13 at 07:08
  • I think it's fair to accept this as the answer. WCF message tracing (more info here http://msdn.microsoft.com/en-us/library/ms751526(v=vs.90).aspx) essentially got me all the data I could ask for. For my immediate needs, it was a bit of overkill (lots of other tracing info beyond just the XML data which was transferred), and it logs to a file which is not exactly what I need -- but this was a helpful answer. – Marcoid Apr 23 '13 at 06:14
  • In the end, what I'm probably going to end up doing is serializing the XML myself like I described in my original question (except with John's corrections to use the newer XmlWriter.Create() method), and logging that to the database (it's simple and readable enough). But I will also turn on WCF message logging (which can be turned on and off via the config file, no coding needed) during our testing phases, and maybe early in production deployment, as an added measure of tracing & logging. – Marcoid Apr 23 '13 at 06:17
  • You can specify your own TraceListeners for WCF logging and tracing. Your listener could write to the database. – John Saunders Apr 23 '13 at 06:36
  • Thanks, John. I've been looking into that, and what I'll probably do is implement a custom listener so I have more control over the output .svclog filenames; but I'll still log to the database with the basic XML generated from my own serialization of the object (because Trace Logging stuff is still a bit of overkill, and rather verbose no matter how much I pare it down - and I may not want to keep it on all the time in production anyway). – Marcoid Apr 24 '13 at 01:35