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 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216
|
//------------------------------------------------------------
// Copyright (c) Microsoft Corporation. All rights reserved.
//------------------------------------------------------------
namespace System.ServiceModel.Channels
{
using System.Runtime;
using System.Runtime.Diagnostics;
using System.ServiceModel.Diagnostics;
using System.Threading;
using System.ServiceModel.Diagnostics.Application;
class TracingConnection : DelegatingConnection
{
ServiceModelActivity activity;
static WaitCallback callback;
public TracingConnection(IConnection connection, ServiceModelActivity activity)
: base(connection)
{
this.activity = activity;
}
public TracingConnection(IConnection connection, bool inheritCurrentActivity)
: base(connection)
{
this.activity = inheritCurrentActivity ?
ServiceModelActivity.CreateActivity(DiagnosticTraceBase.ActivityId, false) :
ServiceModelActivity.CreateActivity();
Fx.Assert(this.activity != null, "");
if (DiagnosticUtility.ShouldUseActivity && !inheritCurrentActivity)
{
if (null != FxTrace.Trace)
{
FxTrace.Trace.TraceTransfer(this.activity.Id);
}
}
}
public override void Abort()
{
try
{
using (ServiceModelActivity.BoundOperation(this.activity))
{
base.Abort();
}
}
finally
{
if (this.activity != null)
{
this.activity.Dispose();
}
}
}
static WaitCallback Callback
{
get
{
if (TracingConnection.callback == null)
{
TracingConnection.callback = new WaitCallback(TracingConnection.WaitCallback);
}
return TracingConnection.callback;
}
}
public override void Close(TimeSpan timeout, bool asyncAndLinger)
{
try
{
using (ServiceModelActivity.BoundOperation(this.activity, true))
{
base.Close(timeout, asyncAndLinger);
}
}
finally
{
if (this.activity != null)
{
this.activity.Dispose();
}
}
}
public override void Shutdown(TimeSpan timeout)
{
using (ServiceModelActivity.BoundOperation(this.activity, true))
{
base.Shutdown(timeout);
}
}
internal void ActivityStart(string name)
{
using (ServiceModelActivity.BoundOperation(this.activity))
{
ServiceModelActivity.Start(this.activity, SR.GetString(SR.ActivityReceiveBytes, name), ActivityType.ReceiveBytes);
}
}
internal void ActivityStart(Uri uri)
{
using (ServiceModelActivity.BoundOperation(this.activity))
{
ServiceModelActivity.Start(this.activity, SR.GetString(SR.ActivityReceiveBytes, uri.ToString()), ActivityType.ReceiveBytes);
}
}
public override AsyncCompletionResult BeginWrite(byte[] buffer, int offset, int size, bool immediate, TimeSpan timeout, WaitCallback callback, object state)
{
using (ServiceModelActivity.BoundOperation(this.activity))
{
return base.BeginWrite(buffer, offset, size, immediate, timeout, callback, state);
}
}
public override void EndWrite()
{
using (ServiceModelActivity.BoundOperation(this.activity))
{
base.EndWrite();
}
}
public override void Write(byte[] buffer, int offset, int size, bool immediate, TimeSpan timeout)
{
using (ServiceModelActivity.BoundOperation(this.activity))
{
base.Write(buffer, offset, size, immediate, timeout);
}
}
public override void Write(byte[] buffer, int offset, int size, bool immediate, TimeSpan timeout, BufferManager bufferManager)
{
using (ServiceModelActivity.BoundOperation(this.activity))
{
base.Write(buffer, offset, size, immediate, timeout, bufferManager);
}
}
public override int Read(byte[] buffer, int offset, int size, TimeSpan timeout)
{
using (ServiceModelActivity.BoundOperation(this.activity))
{
return base.Read(buffer, offset, size, timeout);
}
}
static void WaitCallback(object state)
{
TracingConnectionState tracingData = (TracingConnectionState)state;
tracingData.ExecuteCallback();
}
public override AsyncCompletionResult BeginRead(int offset, int size, TimeSpan timeout, System.Threading.WaitCallback callback, object state)
{
using (ServiceModelActivity.BoundOperation(this.activity))
{
TracingConnectionState completion = new TracingConnectionState(callback, this.activity, state);
return base.BeginRead(offset, size, timeout, TracingConnection.Callback, completion);
}
}
public override int EndRead()
{
int retval = 0;
try
{
if (this.activity != null)
{
ExceptionUtility.UseActivityId(this.activity.Id);
}
retval = base.EndRead();
}
finally
{
ExceptionUtility.ClearActivityId();
}
return retval;
}
public override object DuplicateAndClose(int targetProcessId)
{
using (ServiceModelActivity.BoundOperation(this.activity, true))
{
return base.DuplicateAndClose(targetProcessId);
}
}
class TracingConnectionState
{
object state;
WaitCallback callback;
ServiceModelActivity activity;
internal TracingConnectionState(WaitCallback callback, ServiceModelActivity activity, object state)
{
this.activity = activity;
this.callback = callback;
this.state = state;
}
internal void ExecuteCallback()
{
using (ServiceModelActivity.BoundOperation(this.activity))
{
this.callback(state);
}
}
}
}
}
|