Reputation: 1674
I wrote a ClientMessageInspector modeled after this sample Message Inspection in WCF which I can get to work. I have a service that is a client to other services. It is in a project "Interface12ServiceProject." The MessageInspector is:
namespace MessageListener.Instrumentation
{
public class MessageInspector : IClientMessageInspector
{
private const string LogDir = @"C:\Logs\Interface12Service\";
private Message TraceMessage(MessageBuffer buffer)
{
// Must use a buffer rather than the original message, because the Message's body can be processed only once.
Message msg = buffer.CreateMessage();
//Setup StringWriter to use as input for our StreamWriter
//This is needed in order to capture the body of the message, because the body is streamed
StringWriter stringWriter = new StringWriter();
XmlTextWriter xmlTextWriter = new XmlTextWriter(stringWriter);
msg.WriteMessage(xmlTextWriter);
xmlTextWriter.Flush();
xmlTextWriter.Close();
//Setup filename to write to
if (!Directory.Exists(LogDir))
Directory.CreateDirectory(LogDir);
DateTime now = DateTime.Now;
string datePart = now.Year.ToString() + '-' + now.Month.ToString() + '-' + now.Day.ToString() + '-' + now.Hour + '-' + now.Minute + '-' + now.Second;
string fileName = LogDir + "\\" + datePart + '-' + "SoapEnv.xml";
//Write to file
using (StreamWriter sw = new StreamWriter(fileName))
sw.Write(stringWriter.ToString());
//Return copy of origonal message with unalterd State
return buffer.CreateMessage();
}
public void AfterReceiveReply(ref System.ServiceModel.Channels.Message reply, object correlationState)
{
reply = TraceMessage(reply.CreateBufferedCopy(int.MaxValue));
}
public object BeforeSendRequest(ref System.ServiceModel.Channels.Message request, System.ServiceModel.IClientChannel channel)
{
request = TraceMessage(request.CreateBufferedCopy(int.MaxValue));
return null;
}
}
}
Then I have a LoggingEndpointBehavior:
namespace MessageListener.Instrumentation
{
public class LoggingEndpointBehavior : IEndpointBehavior
{
public void AddBindingParameters(ServiceEndpoint endpoint, System.ServiceModel.Channels.BindingParameterCollection bindingParameters)
{
throw new NotImplementedException();
}
public void ApplyClientBehavior(ServiceEndpoint endpoint, System.ServiceModel.Dispatcher.ClientRuntime clientRuntime)
{
MessageInspector inspector = new MessageInspector();
clientRuntime.MessageInspectors.Add(inspector);
}
public void ApplyDispatchBehavior(ServiceEndpoint endpoint, System.ServiceModel.Dispatcher.EndpointDispatcher endpointDispatcher)
{
throw new NotImplementedException();
}
public void Validate(ServiceEndpoint endpoint)
{
throw new NotImplementedException();
}
}
}
Then a LoggingBehaviorExtenhsionElement:
namespace MessageListener.Instrumentation
{
public class LoggingBehaviorExtensionElement : BehaviorExtensionElement
{
public override Type BehaviorType
{
get { return typeof (LoggingEndpointBehavior); }
}
protected override object CreateBehavior()
{
return new LoggingEndpointBehavior();
}
}
}
Finally, the relevant configuration is:
<client>
<endpoint address="..."
binding="basicHttpBinding" bindingConfiguration="Tier2DocumentEndpointImplServiceSoapBinding"
contract="SSHIPProdInterface12.Tier2DocumentEndpoint" name="Tier2DocumentEndpointImplPort" behaviorConfiguration="messageInspectorBehavior"/>
<endpointBehaviors>
<behavior name="messageInspectorBehavior">
<messageInspector />
</behavior>
</endpointBehaviors>
</behaviors>
<extensions>
<behaviorExtensions>
<add name="messageInspector" type="MessageListener.Instrumentation.LoggingBehaviorExtensionElement, Interface12ServiceProject, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null"/>
</behaviorExtensions>
When I run my Interface12Service and try to call the other service with the endpointBehavior attached:
serviceResponse = client.getTier2Document(authHeader, arg1);
I get "The method or operation is not implemented" exception and my breakpoints in the MessageInspector never get hot. When I take off the endpointBehavior the call to the other service succeeds but without message inspection. What am I doing wrong? Can I attach my behavior to a service in this way?
Upvotes: 1
Views: 201
Reputation: 1674
I found the answer by looking at the stack exception: Validate was throwing a NotImplementedException
Upvotes: 1