Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I get the XML SOAP request of an WCF Web service request?

Tags:

c#

.net

soap

xml

wcf

I'm calling this web service within code and I would like to see the XML, but I can't find a property that exposes it.

like image 449
Diskdrive Avatar asked Mar 30 '11 23:03

Diskdrive


People also ask

How do I get a SOAP request?

To make SOAP requests to the SOAP API endpoint, use the "Content-Type: application/soap+xml" request header, which tells the server that the request body contains a SOAP envelope. The server informs the client that it has returned a SOAP envelope with a "Content-Type: application/soap+xml" response header.

What is the SOAP XML request?

A SOAP message is an ordinary XML document containing the following elements: An Envelope element that identifies the XML document as a SOAP message. A Header element that contains header information. A Body element that contains call and response information.

Are WCF services SOAP?

Normally, a WCF service will use SOAP, but if you build a REST service, clients will be accessing your service with a different architectural style (calls, serialization like JSON, etc.).

What is SOAP message in WCF?

SOAP stands for simple object access protocol. In WCF the main thing is that the communication between the server and client. The communication takes place by messages with some transport layer. The main need of calling a service is to do the data transfer between the server and client.


2 Answers

I think you meant that you want to see the XML at the client, not trace it at the server. In that case, your answer is in the question I linked above, and also at How to Inspect or Modify Messages on the Client. But, since the .NET 4 version of that article is missing its C#, and the .NET 3.5 example has some confusion (if not a bug) in it, here it is expanded for your purpose.

You can intercept the message before it goes out using an IClientMessageInspector:

using System.ServiceModel.Dispatcher; public class MyMessageInspector : IClientMessageInspector { } 

The methods in that interface, BeforeSendRequest and AfterReceiveReply, give you access to the request and reply. To use the inspector, you need to add it to an IEndpointBehavior:

using System.ServiceModel.Description; public class InspectorBehavior : IEndpointBehavior {     public void ApplyClientBehavior(ServiceEndpoint endpoint, ClientRuntime clientRuntime)     {         clientRuntime.MessageInspectors.Add(new MyMessageInspector());     } } 

You can leave the other methods of that interface as empty implementations, unless you want to use their functionality, too. Read the how-to for more details.

After you instantiate the client, add the behavior to the endpoint. Using default names from the sample WCF project:

ServiceReference1.Service1Client client = new ServiceReference1.Service1Client(); client.Endpoint.Behaviors.Add(new InspectorBehavior()); client.GetData(123); 

Set a breakpoint in MyMessageInspector.BeforeSendRequest(); request.ToString() is overloaded to show the XML.

If you are going to manipulate the messages at all, you have to work on a copy of the message. See Using the Message Class for details.

Thanks to Zach Bonham's answer at another question for finding these links.

like image 52
Kimberly Avatar answered Sep 21 '22 13:09

Kimberly


Option 1

Use message tracing/logging.

Have a look here and here.


Option 2

You can always use Fiddler to see the HTTP requests and response.


Option 3

Use System.Net tracing.

like image 29
Aliostad Avatar answered Sep 21 '22 13:09

Aliostad