How to Inspect WCF Message Headers Using IClientMessageInspector
Reading the SOAP message headers for any set of messages can be useful when diagnosing problems. This code allows you to do just that.
To run:
- Copy and paste the code into Visual Studio 2010.
- Add a reference to:
- System.ServiceModel.dll
- System.Runtime.Serialization.dll.
Original posting: http://social.msdn.microsoft.com/Forums/en-US/wcf/thread/fa3f4c55-5835-43f2-892f-737b4bccb598
using System;
using System.Collections.Generic;
using System.ServiceModel;
using System.ServiceModel.Description;
using System.ServiceModel.Dispatcher;
public class MessageInspectorSample
{
static void Main()
{
// This runs the whole show!
Run();
}
[ServiceContract] // A very simple service contract. Echo and Add.
public interface ITest
{
[OperationContract]
string Echo(string text);
[OperationContract]
int Add(int x, int y);
}
public class Service : ITest
{
public string Echo(string text)
{
return text;
}
public int Add(int x, int y)
{
return x + y;
}
}
public class MyInspector : IClientMessageInspector, IEndpointBehavior
{
// These lists store the messages for later retrieval.
public List<string> sentMessages = new List<string>();
public List<string> receivedMessages = new List<string>();
#region IClientMessageInspector Members
public void AfterReceiveReply(ref System.ServiceModel.Channels.Message reply, object correlationState)
{
// Save the reply to the list.
receivedMessages.Add(reply.ToString());
}
public object BeforeSendRequest(ref System.ServiceModel.Channels.Message request, IClientChannel channel)
{
// Save the request to the list.
sentMessages.Add(request.ToString());
return null;
}
#endregion
#region IEndpointBehavior Members
public void AddBindingParameters(ServiceEndpoint endpoint, System.ServiceModel.Channels.BindingParameterCollection bindingParameters)
{}
public void ApplyClientBehavior(ServiceEndpoint endpoint, ClientRuntime clientRuntime)
{
// Add this implementation to the inspectors.
clientRuntime.MessageInspectors.Add(this);
}
public void ApplyDispatchBehavior(ServiceEndpoint endpoint, EndpointDispatcher endpointDispatcher)
{ }
public void Validate(ServiceEndpoint endpoint)
{ }
#endregion
}
public static void Run()
{
string baseAddress = "http://" + Environment.MachineName + ":8000/Service";
ServiceHost host = new ServiceHost(typeof(Service), new Uri(baseAddress));
ServiceEndpoint endpoint = host.AddServiceEndpoint(typeof(ITest), new BasicHttpBinding(), "");
host.Open();
Console.WriteLine("Host opened at " + baseAddress);
MyInspector inspector = new MyInspector();
ChannelFactory<ITest> factory = new ChannelFactory<ITest>(new BasicHttpBinding(), new EndpointAddress(baseAddress));
factory.Endpoint.Behaviors.Add(inspector);
ITest proxy = factory.CreateChannel();
proxy.Echo("Hello world");
proxy.Add(123, 456);
((IClientChannel)proxy).Close();
factory.Close();
host.Close();
Console.WriteLine("Sent messages:");
foreach (string sent in inspector.sentMessages)
{
Console.WriteLine(sent);
Console.WriteLine();
}
Console.WriteLine("*******************************************");
Console.WriteLine("Received messages:");
foreach (string received in inspector.receivedMessages)
{
Console.WriteLine(received);
Console.WriteLine();
}
Console.ReadLine();
}
}
+++++++++++++++++++++++++++++++++++++
To add complexity, use a different binding such as WSHttpBinding with different security requirements.
See Also
Other Languages
This article is also available in the following languages: