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.
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.