how do i obtain soap request and response from web service - c#

I want to obtain a soap request in xml format of a web service (not wcf). I tried the msdn example of soap trace extension.
It works, but I don't want to log it , I want to obtain it in a string and display in a web browser control, in winforms.

Even though your service is ASMX, you can use a Service Reference so that the client is using WCF. One of the benefits of that is that you can use WCF features like tracing and message inspection.
I know you say that you don't want to use tracing, but using tracing (in particular, message logging) with a custom trace listener may be a simple solution. If not, then message inspection should take care of anything you might need.

Related

log soap request and response from Wcf web service to a text file using log4net

Is there there any simple way of getting a WCF Service to log SOAP requests and responses to a text file.
I want to log the messages on server, just to clarify.
If you can change the service code to add a behavior, you can hook up an IDispatchMessageInspector and use whichever logging framework to log at the messages there. You can also look at the WCF Message Logging feature, which uses the System.Diagnostic framework to perform the logging (I haven't used log4net yet, maybe they have some trace listener which can be used for this as well).

How can I capture the SOAP request generated on localhost?

I have a simple windows console app that I have added a service reference to and I am trying to call a method and make a request! I am using the proxy classes generated by .Net when I added the service reference.
The web service is not on my machine but externally hosted by the client!
I am getting an error so I would like to see the HTTP request that is being sent.
I can't see the request on fiddler, why not?
Can I seen the generated request by debugging the console app?
You can see localhost traffic with Fiddler. See this article in the Fiddler FAQ.
You could also turn on WCF Tracing.
Fiddler bypass local traffic. If you want to sniff local address in the client config set .localhost address (with dot prefix).
Aside of Fiddler and embedded tracing tool there is another approach to see generated XML SOAP - custom MessageEncoder. It allows to interceprt original byte content including any malformed xml.
In order to implement tracing using this approach you need to wrap a standard textMessageEncoding with custom message encoder as new binding element and apply that custom binding to endpoint in your config.
Also you can see as example how I did it in my project -
wrapping textMessageEncoding, logging encoder, custom binding element and config.

How do I get the SOAP headers as XML?

How can I get the SOAP header of my web service function as xml?
I need an XML version of my working SOAP header to use as an example for someone with broken SOAP headers. He's using a different programming language, but since the headers are sent in XML, we're hoping we can compare those easily. I'm using C# and ToString() just gives me the name of the object. After googling, I tried Fiddler, but that didn't catch anything, and SOAP Extensions look way too complicated for what seems like a really simple task.
There is no easy way to do this with ASMX web services. If you were using WCF, it would be a trivial matter of turning on message logging.
Fiddler should catch everything, and it's well worth learning how to use. Be sure to look in the online documentation to learn how to capture traffic from localhost to localhost.
The accepted answer to this question mentions that you can capture traffic to a web service using soapUI. You can use it to act as a proxy or HTTP tunnel in order to capture messages sent to your web service. I think using it in HTTP Tunnel mode makes sense, because then you won't have to do anything with your client other than to point the URL to soapUI instead of your actual service.
The soapUI website talks about the SOAP Monitor feature, though I don't think it offers a good explanation for setting up the HTTP tunnel.
Here are the steps involved (based on soapUI 3.0.1):
Open Soap UI
create a New soapUI Project...
2.1 Give your project a name
2.2 Enter the path to your service's WSDL (http://localhost/.../YourService.asmx?WSDL)
Right-click on the project you just created
Select "Launch SOAP Monitor"
Choose "HTTP Tunnel"
Specify an unused port number for "Port". The default will probably work.
Enter the URL to your web service in "Set endpoint for HTTP Tunnel"
If you are using SSL, then you may need to fill in some of the other fields; otherwise, click OK
The HTTP tunnel is running, so now just reconfigure a client to use the HTTP tunnel instead of the actual web service. So if your actual service is "http://localhost:1234/YourService.asmx", then reconfigure your client to use "http://localhost:{Port}/YourService.asmx" where {Port} is the number you entered in step #6.
Now just run your client normally. You'll see the traffic logged in the soapUI interface. Select a message and click on the "Message Content" button at the bottom of the window to view the actual SOAP message.

Tracing the SOAP envelope that is being sent by a .NET web service client possible?

A C# client application we developed calls an external SOAP web service of a third party company.
Now I want to trace the exact SOAP request envelopes that are being generated by the VS.NET-generated SOAP proxy in our application and transfered to the external SOAP web service.
The method of using a network monitor like Wireshark (as described here) or using Microsoft Network Monitor is no option since the externl SOAP web service only provides an SSL/HTTPS URL, so the payload of the HTTP packages are encrypted and not viewable.
My question is:
Is there a way of configuring the .NET-built-in web service client classes to output/trace/log their underlying HTTP requests and responses that are being generated/received? (or maybe some kind of event to subscribe to?)
If you're using a standard web service (not WFC), you can extend the SoapExtension class as described here:
http://www.blog.encoresystems.net/articles/how-to-capture-soap-envelopes-when-consuming-a-web-service.aspx
If you're using WFC, then you can implement IEndpointBehavior and IClientMessageBehavior as described here:
http://weblogs.asp.net/paolopia/archive/2007/08/23/writing-a-wcf-message-inspector.aspx
I use both, depending on whether I'm using web or service references.
You can use fiddler, perhaps, as a proxy and thus monitor HTTP/HTTPS requests. This won't require you to write any code on your part.
Fiddler Web Debugger
It is worth noting that there are caveats to debugging HTTPS requests with Fiddler. This page explains how it can be done.
Fiddler Web Debugger - Debugging HTTPS traffic with Fiddler2
You can use Fiddler, or System.Net tracing.
http://blogs.msdn.com/dgorti/archive/2005/09/18/471003.aspx
http://msdn.microsoft.com/en-us/library/bb203855(BTS.10).aspx

Implementing custom logging of WCF messages client-side

A collegue of mine needs to implement custom logging of WCF messages, for debugging purposes, but also for traceability (logged data will be saved for future reference and verification).
Examples like this one, Capture XML In WCF Service, shows how to do this for the host, but he needs it for the client application.
Can anyone help me/him with some pointers to what to look for, or where to start?
Since we need to talk to a database to log the data, a simple configuration change to log to a file on disk, like this MSDN example, Configure Message Logging, is not enough for our needs.
This is a C# 3.0 application for .NET 3.5.
The example you cited: it is very similar/dual for the client. Instead of IDispatchMessageInspector, there's IClientMessageInspector. Instead of IServiceBehavior, use IEndpointBehavior, etc. The OM will steer you in the right direction.

Categories

Resources