1: 2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16: 17: 18: 19: 20: 21: 22: 23: 24: 25: 26: 27: 28: 29: 30: 31: 32: 33: 34: 35: 36: 37: 38: 39: 40: 41: 42: 43: 44: 45: 46: 47: 48: 49: 50: 51: 52: 53: 54: 55: 56: 57: 58: 59: 60: 61: 62: 63: 64: 65: 66: 67: 68: 69: 70: 71: 72: 73: 74: 75: 76: 77: 78: 79: 80: 81: 82: 83: 84: 85: 86: 87: 88: 89: 90: 91: 92: 93: 94: 95: 96: 97: 98: 99: 100: 101: 102: 103: 104: 105: 106: 107: 108: 109: 110: 111: 112: 113: 114: 115: 116: 117: 118: 119: 120: 121: 122:
| public class myOutputMessageInspector { internal class OutputMsgInterceptor : IClientMessageInspector, IDispatchMessageInspector { private readonly bool Online; private readonly bool LogMessages; private readonly String OutFileName; private readonly String InFileName;
public OutputMsgInterceptor(String outFileName, String inFileName, bool doLog, bool isOnline) { this.Online = isOnline; LogMessages = doLog; this.OutFileName = outFileName; this.InFileName = inFileName; }
public object BeforeSendRequest(ref Message request, IClientChannel channel) { SaveMessage(ref request, OutFileName); return null; }
public void AfterReceiveReply(ref Message reply, object correlationState) { SaveMessage(ref reply, InFileName); }
private void SaveMessage(ref Message message, string filename) { if (LogMessages && !String.IsNullOrEmpty(filename)) { MessageBuffer buffer = message.CreateBufferedCopy(Int32.MaxValue);
message = buffer.CreateMessage();
Message originalMessage = buffer.CreateMessage();
FileStream fileStream = new FileStream(@filename, FileMode.Create); StreamWriter m_streamWriter = new StreamWriter(fileStream); m_streamWriter.BaseStream.Seek(0, SeekOrigin.End); m_streamWriter.Write(originalMessage.ToString()); m_streamWriter.Flush(); } }
#region IDispatchMessageInspector Members
public object AfterReceiveRequest(ref Message request, IClientChannel channel, InstanceContext instanceContext) { throw new NotImplementedException(); }
public void BeforeSendReply(ref Message reply, object correlationState) { throw new NotImplementedException(); }
#endregion }
internal class OutputMsgBehavior : IEndpointBehavior { private readonly bool enabled; private readonly bool Online; private readonly bool LogMessages; private readonly String OutFileName; private readonly String InFileName;
internal OutputMsgBehavior(bool enabled, String outFileName, String inFileName, bool doLog, bool isOnline) { this.enabled = enabled; this.OutFileName = outFileName; this.InFileName = inFileName; Online = isOnline; LogMessages = doLog; }
public void Validate(ServiceEndpoint endpoint) { }
public void AddBindingParameters(ServiceEndpoint serviceEndpoint, BindingParameterCollection bindingParameters) { }
public void ApplyClientBehavior( ServiceEndpoint endpoint, ClientRuntime clientRuntime) { if (false == enabled) { return; } myOutputMessageInspector.OutputMsgInterceptor interceptor = new myOutputMessageInspector.OutputMsgInterceptor(OutFileName,InFileName,LogMessages,Online); clientRuntime.MessageInspectors.Add(interceptor); }
public void ApplyDispatchBehavior( ServiceEndpoint endpoint, EndpointDispatcher endpointDispatcher) { if (false == enabled) { return; } myOutputMessageInspector.OutputMsgInterceptor interceptor = new myOutputMessageInspector.OutputMsgInterceptor(OutFileName, InFileName, LogMessages, Online); endpointDispatcher.DispatchRuntime.MessageInspectors.Add(interceptor); }
}
} |