ComPlusSynchronizationContext.cs 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. //-----------------------------------------------------------------------------
  2. // Copyright (c) Microsoft Corporation. All rights reserved.
  3. //-----------------------------------------------------------------------------
  4. namespace System.ServiceModel.ComIntegration
  5. {
  6. using System;
  7. using System.Diagnostics;
  8. using System.Runtime;
  9. using System.Runtime.InteropServices;
  10. using System.ServiceModel.Diagnostics;
  11. using System.Threading;
  12. class ComPlusSynchronizationContext : SynchronizationContext
  13. {
  14. IServiceActivity activity;
  15. bool postSynchronous;
  16. public ComPlusSynchronizationContext(IServiceActivity activity,
  17. bool postSynchronous)
  18. {
  19. this.activity = activity;
  20. this.postSynchronous = postSynchronous;
  21. }
  22. public override void Send(SendOrPostCallback d, Object state)
  23. {
  24. Fx.Assert("Send should never be called");
  25. }
  26. public override void Post(SendOrPostCallback d, Object state)
  27. {
  28. ComPlusActivityTrace.Trace(TraceEventType.Verbose, TraceCode.ComIntegrationEnteringActivity,
  29. SR.TraceCodeComIntegrationEnteringActivity);
  30. ServiceCall call = new ServiceCall(d, state);
  31. if (this.postSynchronous)
  32. {
  33. this.activity.SynchronousCall(call);
  34. }
  35. else
  36. {
  37. this.activity.AsynchronousCall(call);
  38. }
  39. ComPlusActivityTrace.Trace(TraceEventType.Verbose, TraceCode.ComIntegrationLeftActivity,
  40. SR.TraceCodeComIntegrationLeftActivity);
  41. }
  42. public void Dispose()
  43. {
  44. while (Marshal.ReleaseComObject(this.activity) > 0);
  45. }
  46. class ServiceCall : IServiceCall
  47. {
  48. SendOrPostCallback callback;
  49. Object state;
  50. public ServiceCall(SendOrPostCallback callback,
  51. Object state)
  52. {
  53. this.callback = callback;
  54. this.state = state;
  55. }
  56. public void OnCall()
  57. {
  58. ServiceModelActivity activity = null;
  59. try
  60. {
  61. Guid guidLogicalThreadID = Guid.Empty;
  62. if (DiagnosticUtility.ShouldUseActivity)
  63. {
  64. IComThreadingInfo comThreadingInfo;
  65. comThreadingInfo = (IComThreadingInfo)SafeNativeMethods.CoGetObjectContext(ComPlusActivityTrace.IID_IComThreadingInfo);
  66. if (comThreadingInfo != null)
  67. {
  68. comThreadingInfo.GetCurrentLogicalThreadId(out guidLogicalThreadID);
  69. activity = ServiceModelActivity.CreateBoundedActivity(guidLogicalThreadID);
  70. }
  71. ServiceModelActivity.Start(activity, SR.GetString(SR.TransferringToComplus, guidLogicalThreadID.ToString()), ActivityType.TransferToComPlus);
  72. }
  73. ComPlusActivityTrace.Trace(TraceEventType.Verbose, TraceCode.ComIntegrationExecutingCall, SR.TraceCodeComIntegrationExecutingCall);
  74. this.callback(this.state);
  75. }
  76. catch (Exception e)
  77. {
  78. if (Fx.IsFatal(e))
  79. throw;
  80. DiagnosticUtility.InvokeFinalHandler(e);
  81. }
  82. finally
  83. {
  84. if (activity != null)
  85. {
  86. activity.Dispose();
  87. activity = null;
  88. }
  89. }
  90. }
  91. }
  92. }
  93. }