Capture XML Response

T

Tom Bean

I am writing a C# Windows service that calls methods on a remote web service
and would like to capture the XML returned by the web service. The reason I
need the raw XML is for back up to the sub-set of data I am saving to a
database.

All the posts I have found seem to assume one has access to both the client
and server applications but since the web service is remote, I can only
control the client side.

Any help in learning how to accomplish this will be greatly appreciated.

Thanks,
Tom
 
A

Ashot Geodakov

Send a regular HTTP POST request to the web server that will call the
appropriate web service, and read the returned XML as a stream.

Do you have the URL of the web service? If yes, try to type that URL in your
web browser. It will display the proper format of the HTTP request, so you
can use it.
 
T

Tom Bean

Ashot,

I call the methods using a WebReference object, created from a WSDL file,
not with HTTP POST. There is a charge for each call to the web service so I
can't afford to make the call both ways to get the data and the XML.

Tom
 
R

RYoung

Check out the MSDN docs:
ms-help://MS.MSSDK.1033/MS.NETFX30SDK.1033/cpref26/html/T_System_Web_Services_Protocols_SoapExtension.htm

Shows using a SoapExtension on the client side (your windows service) to
write outgoing and incoming XML between client and service.

Ron
 
S

Steven Cheng[MSFT]

Hello Tom,

Normally, for interactive diagnostic, we will use some trace utiilty like
tcptrace , soaptoolkit+traceutility. For your scenario, if you want to
programmatically capture the message and log it, you can consider using
soapExtension as Ron suggested. Soap Extension can be used at both
clientside and server-side of webservice. Here are some reference articles
you can have a look.

#Fun with SOAP Extensions
http://msdn2.microsoft.com/en-us/library/ms972353.aspx

#Using SOAP Extensions in ASP.NET
http://msdn.microsoft.com/msdnmag/issues/04/03/ASPColumn/default.aspx

#How to: Implement a SOAP Extension
http://msdn2.microsoft.com/en-us/library/7w06t139.aspx

Hope this helps.

Sincerely,

Steven Cheng

Microsoft MSDN Online Support Lead


This posting is provided "AS IS" with no warranties, and confers no rights.
 
T

Tom Bean

Ron,

I had looked at the TraceExtension sample before I submitted my post and
have tried to implement it in my application. The problem I'm having is
that I couldn't find the code that uses TraceExtension. How do you
implement it and make sure it gets called during the web service request and
response?

Also, I notice the document says "SOAP extensions for XML Web services
created using ASP.NET." I am not creating a web service and I am not using
ASP.NET. My application is a Windows application, so how do I implement
something like TraceExtension in it?

Tom
 
T

Tom Bean

Steven,

All the documentation I've seen, including the articles you sent, say SOAP
extensions are implemented on the web site. I have no access to the web
site where the web service runs. I need to capture the XML in the request
and response on the client.

I must be missing something but I haven't seen anything that shows how to
implement a SOAP extension in a Windows client application. Is it possible
to do this without having access to the web service?

Tom
 
S

Steven Cheng[MSFT]

Hello Tom,

Of course, soap extension can be applied on both sides(client and server)
of .NET implemented webservice. For client-side proxy, you also have two
means to apply a soapextension on it:

** through application's App.config file
** directly add soapextension attribute on a certain webmethod in the
generated proxy code file

Here are some web article and former thread discussing on this:

http://blogs.infosupport.com/marcelv/archive/2006/07/05/8615.aspx

http://www.thescripts.com/forum/thread427659.html

Hope this also helps.

Sincerely,

Steven Cheng

Microsoft MSDN Online Support Lead


This posting is provided "AS IS" with no warranties, and confers no rights.
 
T

Tom Bean

Steven,

Thanks for tolerating my lack of understanding. I followed your
instructions in the http://www.thescripts.com/forum/thread427659.html and
was able to get TraceExtension to write the SoapRequest and SoapResponse to
a file in my application.

I want to capture the raw XML especially from the SoapResponse and save it
in a table in a SQL Server database. Since the soap extension runs as part
of the proxy, how can I access the XML in TraceExtension from my client app
rather than saving it to a file?

Thanks again,
Tom
 
S

Steven Cheng[MSFT]

Thanks for the reply Tom,

As for the SoapExtension, though it is applied on our webservice webmethod
or client proxy's webmethod, the execution of the extension is separated
from our client application(who call the webservice proxy)'s code.
Therefore, there is no directly channel for us to control or access the
soapextension intances in client application's code(or from proxy). Do you
think it is possible that you directly add the code in soapextension to
store the response SOAP XML message into database? You can put the database
connection info in the soapextension attribute so that you can flexiblly
configure it through soapextension attribute or in app.config file.

Sincerely,

Steven Cheng

Microsoft MSDN Online Support Lead


This posting is provided "AS IS" with no warranties, and confers no rights.
 
T

Tom Bean

Steven,

Because of the separation, the SoapExtension does not know the entity the
XML is for so it couldn't save it properly. Besides, the table column where
the XML needs to be saved is part of the record that is populated by the web
service object.

Tom
 
T

Tom Bean

Steven,

I added two static string properties to the SoapExtension, one for the
SoapRequest and the other for the SoapResponse. I set these properties in
the SoapMessage Stages AfterSerialize and BeforeDeserialization. When I
return from a call to a web service method, I access these properties to get
the XML.

Seems to work.

Tom
 
S

Steven Cheng[MSFT]

Thanks for your reply Tom,

As for the "static variable on SoapExtension class" approach you mentioned,
it does be a possible approach. However, there is a potential problem with
this approach. That's the concurrent access/thread safety of these static
members when there're multiple threads call the webservice proxy methods.

If your scenario does care on the above issue(client application may have
multiple threads calling the webservice proxy methods), you can consider
adding some additional thread-safe protection for your static variables on
the SoapExtension class. For example, you can use the "CallContext"
class's storage to store call-context specific data:

=================
public static string CurrentData
{
get
{
return CallContext.GetData("logdata") as string;
}
set
{
CallContext.SetData("logdata", value);
}
}
=====================

This can help prevent concurrent access on your webservice proxy.

Hope this also helps.

Sincerely,

Steven Cheng

Microsoft MSDN Online Support Lead



This posting is provided "AS IS" with no warranties, and confers no rights.
 
Joined
Aug 30, 2012
Messages
1
Reaction score
0
Hi ,
I'm getting the log and i can save it to a file ,
But when i'm trying to insert the log into database the session were ended and return the "erro 400 - bad request" .
Its seems like session ended when we try to access database .
Any idea ?

Thank in advance ,

Poli .
 
Joined
Nov 12, 2013
Messages
2
Reaction score
0
Hello, Look at this blog, we provide another example of how to do that.
www . systemdeveloper . info / 2013 / 11 / trace-soap-requestresponse-xml-with . html
I hope you find it helpful
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top