This repository was archived by the owner on Jul 1, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathGlimpseWcfClientInspector.cs
More file actions
109 lines (94 loc) · 3.78 KB
/
GlimpseWcfClientInspector.cs
File metadata and controls
109 lines (94 loc) · 3.78 KB
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
using System;
using System.IO;
using System.ServiceModel;
using System.ServiceModel.Channels;
using System.ServiceModel.Dispatcher;
using System.Text;
using System.Xml;
using Glimpse.Core.Extensibility;
// http://blogs.msdn.com/b/carlosfigueira/archive/2011/04/19/wcf-extensibility-message-inspectors.aspx
// http://www.universalthread.com/ViewPageArticle.aspx?ID=191
namespace Glimpse.Wcf
{
public class GlimpseWcfClientInspector : IClientMessageInspector
{
private static IMessageBroker _messageBroker;
private static Func<IExecutionTimer> _timerStrategy;
public static void Initialize(IMessageBroker messageBroker, Func<IExecutionTimer> timerStrategy)
{
_messageBroker = messageBroker;
_timerStrategy = timerStrategy;
}
private string ActionStr(string action)
{
// http://tempuri.org/ISessionService/BeginSession
if (action.StartsWith("http://tempuri.org/"))
action = action.Substring(19);
return action.Replace('/', '.');
}
public object BeforeSendRequest(ref Message request, IClientChannel channel)
{
// copy message and re-create
MessageBuffer buffer = request.CreateBufferedCopy(Int32.MaxValue);
request = buffer.CreateMessage();
var entry = new WcfEntry
{
MessageId = request.Headers.MessageId.ToString(),
EventName = ActionStr(request.Headers.Action),
RequestBody = MessageToString(buffer.CreateMessage()),
//EventSubText = "test"
// ExternalId = externalId
};
if (_timerStrategy != null && _messageBroker != null)
{
IExecutionTimer timer = _timerStrategy();
if (timer != null)
entry.Timer = timer;
}
return entry;
}
public void AfterReceiveReply(ref Message reply, object correlationState)
{
var entry = correlationState as WcfEntry;
if (entry != null)
{
entry.CalcDuration();
// copy message and re-create
MessageBuffer buffer = reply.CreateBufferedCopy(Int32.MaxValue);
reply = buffer.CreateMessage();
entry.ResponseBody = MessageToString(buffer.CreateMessage());
entry.Cleanup();
_messageBroker.Publish(entry);
}
}
private string MessageToString(Message message)
{
using (var ms = new MemoryStream())
{
using (var w = XmlWriter.Create(ms, new XmlWriterSettings
{
Indent = true,
IndentChars = " ",
OmitXmlDeclaration = true,
}))
{
XmlDictionaryReader bodyReader = message.GetReaderAtBodyContents();
while (bodyReader.NodeType != XmlNodeType.EndElement && bodyReader.LocalName != "Body" &&
bodyReader.NamespaceURI != "http://schemas.xmlsoap.org/soap/envelope/")
{
if (bodyReader.NodeType != XmlNodeType.Whitespace)
{
w.WriteNode(bodyReader, true);
}
else
{
bodyReader.Read(); // ignore whitespace; maintain if you want
}
}
w.Flush();
return Encoding.UTF8.GetString(ms.ToArray());
}
}
}
}
}