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 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388
|
//-----------------------------------------------------------------------------
// Copyright (c) Microsoft Corporation. All rights reserved.
//-----------------------------------------------------------------------------
namespace System.ServiceModel.Dispatcher
{
using System;
using System.Diagnostics;
using System.Reflection;
using System.Runtime.Diagnostics;
using System.Security;
using System.ServiceModel.Description;
using System.ServiceModel.Diagnostics;
using System.ServiceModel.Diagnostics.Application;
using System.Runtime;
class AsyncMethodInvoker : IOperationInvoker
{
MethodInfo beginMethod;
MethodInfo endMethod;
InvokeBeginDelegate invokeBeginDelegate;
InvokeEndDelegate invokeEndDelegate;
int inputParameterCount;
int outputParameterCount;
public AsyncMethodInvoker(MethodInfo beginMethod, MethodInfo endMethod)
{
if (beginMethod == null)
throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ArgumentNullException("beginMethod"));
if (endMethod == null)
throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ArgumentNullException("endMethod"));
this.beginMethod = beginMethod;
this.endMethod = endMethod;
}
public MethodInfo BeginMethod
{
get { return this.beginMethod; }
}
public MethodInfo EndMethod
{
get { return this.endMethod; }
}
public bool IsSynchronous
{
get { return false; }
}
public object[] AllocateInputs()
{
return EmptyArray.Allocate(this.InputParameterCount);
}
public object Invoke(object instance, object[] inputs, out object[] outputs)
{
throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new NotImplementedException());
}
internal static void CreateActivityInfo(ref ServiceModelActivity activity, ref Activity boundActivity)
{
if (DiagnosticUtility.ShouldUseActivity)
{
activity = ServiceModelActivity.CreateAsyncActivity();
TraceUtility.UpdateAsyncOperationContextWithActivity(activity);
boundActivity = ServiceModelActivity.BoundOperation(activity, true);
}
else if (TraceUtility.MessageFlowTracingOnly)
{
Guid activityId = TraceUtility.GetReceivedActivityId(OperationContext.Current);
if (activityId != Guid.Empty)
{
DiagnosticTraceBase.ActivityId = activityId;
}
}
else if (TraceUtility.ShouldPropagateActivity)
{
//Message flow tracing only scenarios use a light-weight ActivityID management logic
Guid activityId = ActivityIdHeader.ExtractActivityId(OperationContext.Current.IncomingMessage);
if (activityId != Guid.Empty)
{
boundActivity = Activity.CreateActivity(activityId);
}
TraceUtility.UpdateAsyncOperationContextWithActivity(activityId);
}
}
public IAsyncResult InvokeBegin(object instance, object[] inputs, AsyncCallback callback, object state)
{
if (instance == null)
throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException(SR.GetString(SR.SFxNoServiceObject)));
if (inputs == null)
{
if (this.InputParameterCount > 0)
throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException(SR.GetString(SR.SFxInputParametersToServiceNull, this.InputParameterCount)));
}
else if (inputs.Length != this.InputParameterCount)
throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException(SR.GetString(SR.SFxInputParametersToServiceInvalid, this.InputParameterCount, inputs.Length)));
StartOperationInvokePerformanceCounters(this.beginMethod.Name.Substring(ServiceReflector.BeginMethodNamePrefix.Length));
IAsyncResult returnValue;
bool callFailed = true;
bool callFaulted = false;
ServiceModelActivity activity = null;
try
{
Activity boundActivity = null;
CreateActivityInfo(ref activity, ref boundActivity);
StartOperationInvokeTrace(this.beginMethod.Name);
using (boundActivity)
{
if (DiagnosticUtility.ShouldUseActivity)
{
string activityName = null;
if (this.endMethod == null)
{
activityName = SR.GetString(SR.ActivityExecuteMethod,
this.beginMethod.DeclaringType.FullName, this.beginMethod.Name);
}
else
{
activityName = SR.GetString(SR.ActivityExecuteAsyncMethod,
this.beginMethod.DeclaringType.FullName, this.beginMethod.Name,
this.endMethod.DeclaringType.FullName, this.endMethod.Name);
}
ServiceModelActivity.Start(activity, activityName, ActivityType.ExecuteUserCode);
}
returnValue = this.InvokeBeginDelegate(instance, inputs, callback, state);
callFailed = false;
}
}
catch (System.Security.SecurityException e)
{
DiagnosticUtility.TraceHandledException(e, TraceEventType.Warning);
throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(AuthorizationBehavior.CreateAccessDeniedFaultException());
}
catch (Exception e)
{
TraceUtility.TraceUserCodeException(e, this.beginMethod);
if (e is FaultException)
{
callFaulted = true;
callFailed = false;
}
throw;
}
finally
{
ServiceModelActivity.Stop(activity);
// An exception during the InvokeBegin will not call InvokeEnd,
// so we complete the trace and performance counters here.
if (callFailed || callFaulted)
{
StopOperationInvokeTrace(callFailed, callFaulted, this.EndMethod.Name);
StopOperationInvokePerformanceCounters(callFailed, callFaulted, endMethod.Name.Substring(ServiceReflector.EndMethodNamePrefix.Length));
}
}
return returnValue;
}
internal static void GetActivityInfo(ref ServiceModelActivity activity, ref Activity boundOperation)
{
if (TraceUtility.MessageFlowTracingOnly)
{
if (null != OperationContext.Current)
{
Guid activityId = TraceUtility.GetReceivedActivityId(OperationContext.Current);
if (activityId != Guid.Empty)
{
DiagnosticTraceBase.ActivityId = activityId;
}
}
}
else if (DiagnosticUtility.ShouldUseActivity || TraceUtility.ShouldPropagateActivity)
{
object activityInfo = TraceUtility.ExtractAsyncOperationContextActivity();
if (activityInfo != null)
{
if (DiagnosticUtility.ShouldUseActivity)
{
activity = activityInfo as ServiceModelActivity;
boundOperation = ServiceModelActivity.BoundOperation(activity, true);
}
else if (TraceUtility.ShouldPropagateActivity)
{
if (activityInfo is Guid)
{
Guid activityId = (Guid)activityInfo;
boundOperation = Activity.CreateActivity(activityId);
}
}
}
}
}
public object InvokeEnd(object instance, out object[] outputs, IAsyncResult result)
{
object returnVal;
if (instance == null)
throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException(SR.GetString(SR.SFxNoServiceObject)));
outputs = EmptyArray.Allocate(this.OutputParameterCount);
bool callFailed = true;
bool callFaulted = false;
ServiceModelActivity activity = null;
try
{
Activity boundOperation = null;
GetActivityInfo(ref activity, ref boundOperation);
using (boundOperation)
{
returnVal = this.InvokeEndDelegate(instance, outputs, result);
callFailed = false;
}
}
catch (SecurityException e)
{
DiagnosticUtility.TraceHandledException(e, TraceEventType.Warning);
throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(AuthorizationBehavior.CreateAccessDeniedFaultException());
}
catch (FaultException)
{
callFaulted = true;
callFailed = false;
throw;
}
finally
{
ServiceModelActivity.Stop(activity);
StopOperationInvokeTrace(callFailed, callFaulted, this.endMethod.Name);
StopOperationInvokePerformanceCounters(callFailed, callFaulted, this.endMethod.Name.Substring(ServiceReflector.EndMethodNamePrefix.Length));
}
return returnVal;
}
internal static void StartOperationInvokeTrace(string methodName)
{
if (TD.OperationInvokedIsEnabled())
{
OperationContext context = OperationContext.Current;
EventTraceActivity eventTraceActivity = null;
if (context != null && context.IncomingMessage != null)
{
eventTraceActivity = EventTraceActivityHelper.TryExtractActivity(context.IncomingMessage);
}
if (TD.OperationInvokedIsEnabled())
{
TD.OperationInvoked(eventTraceActivity, methodName, TraceUtility.GetCallerInfo(OperationContext.Current));
}
if (TD.OperationCompletedIsEnabled() || TD.OperationFaultedIsEnabled() || TD.OperationFailedIsEnabled())
{
TraceUtility.UpdateAsyncOperationContextWithStartTime(eventTraceActivity, DateTime.UtcNow.Ticks);
}
}
}
internal static void StopOperationInvokeTrace(bool callFailed, bool callFaulted, string methodName)
{
if (!(TD.OperationCompletedIsEnabled() ||
TD.OperationFaultedIsEnabled() ||
TD.OperationFailedIsEnabled()))
{
return;
}
EventTraceActivity eventTraceActivity;
long startTime;
TraceUtility.ExtractAsyncOperationStartTime(out eventTraceActivity, out startTime);
long duration = TraceUtility.GetUtcBasedDurationForTrace(startTime);
if (callFailed)
{
if (TD.OperationFailedIsEnabled())
{
TD.OperationFailed(eventTraceActivity, methodName, duration);
}
}
else if (callFaulted)
{
if (TD.OperationFaultedIsEnabled())
{
TD.OperationFaulted(eventTraceActivity, methodName, duration);
}
}
else
{
if (TD.OperationCompletedIsEnabled())
{
TD.OperationCompleted(eventTraceActivity, methodName, duration);
}
}
}
internal static void StartOperationInvokePerformanceCounters(string methodName)
{
if (PerformanceCounters.PerformanceCountersEnabled)
{
PerformanceCounters.MethodCalled(methodName);
}
}
internal static void StopOperationInvokePerformanceCounters(bool callFailed, bool callFaulted, string methodName)
{
if (PerformanceCounters.PerformanceCountersEnabled)
{
if (callFailed)
{
PerformanceCounters.MethodReturnedError(methodName);
}
else if (callFaulted)
{
PerformanceCounters.MethodReturnedFault(methodName);
}
else
{
PerformanceCounters.MethodReturnedSuccess(methodName);
}
}
}
InvokeBeginDelegate InvokeBeginDelegate
{
get
{
EnsureIsInitialized();
return invokeBeginDelegate;
}
}
InvokeEndDelegate InvokeEndDelegate
{
get
{
EnsureIsInitialized();
return invokeEndDelegate;
}
}
int InputParameterCount
{
get
{
EnsureIsInitialized();
return this.inputParameterCount;
}
}
int OutputParameterCount
{
get
{
EnsureIsInitialized();
return this.outputParameterCount;
}
}
void EnsureIsInitialized()
{
if (this.invokeBeginDelegate == null)
{
// Only pass locals byref because InvokerUtil may store temporary results in the byref.
// If two threads both reference this.count, temporary results may interact.
int inputParameterCount;
InvokeBeginDelegate invokeBeginDelegate = new InvokerUtil().GenerateInvokeBeginDelegate(this.beginMethod, out inputParameterCount);
this.inputParameterCount = inputParameterCount;
int outputParameterCount;
InvokeEndDelegate invokeEndDelegate = new InvokerUtil().GenerateInvokeEndDelegate(this.endMethod, out outputParameterCount);
this.outputParameterCount = outputParameterCount;
this.invokeEndDelegate = invokeEndDelegate;
this.invokeBeginDelegate = invokeBeginDelegate; // must set this last due to ----
}
}
}
}
|