BufferedRequestContext.cs 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. //----------------------------------------------------------------------------
  2. // Copyright (c) Microsoft Corporation. All rights reserved.
  3. //----------------------------------------------------------------------------
  4. namespace System.ServiceModel.Channels
  5. {
  6. using System;
  7. using System.Diagnostics;
  8. using System.Runtime;
  9. using System.ServiceModel;
  10. using System.ServiceModel.Diagnostics;
  11. class BufferedRequestContext : RequestContext
  12. {
  13. bool delayClose;
  14. object thisLock;
  15. RequestContext innerRequestContext;
  16. public BufferedRequestContext(RequestContext requestContext)
  17. {
  18. this.innerRequestContext = requestContext;
  19. this.thisLock = new object();
  20. }
  21. public override Message RequestMessage
  22. {
  23. get
  24. {
  25. return innerRequestContext.RequestMessage;
  26. }
  27. }
  28. public RequestContext InnerRequestContext
  29. {
  30. get
  31. {
  32. return innerRequestContext;
  33. }
  34. }
  35. public void DelayClose(bool delay)
  36. {
  37. lock (this.thisLock)
  38. {
  39. this.delayClose = delay;
  40. }
  41. }
  42. public void ReInitialize(Message requestMessage)
  43. {
  44. // things might ---- up here if a custom channel is using any properties on the original message...
  45. // we should consider creating a virtual method in Dev11 on RequestContext to allow authors of custom
  46. // channels reset the state of the request context before retrying a message.
  47. RequestContextBase requestContextBase = this.innerRequestContext as RequestContextBase;
  48. if (requestContextBase != null)
  49. {
  50. requestContextBase.ReInitialize(requestMessage);
  51. }
  52. }
  53. public override void Abort()
  54. {
  55. lock (this.thisLock)
  56. {
  57. if (this.delayClose)
  58. {
  59. // Only delay the first attempt at Close/Abort
  60. this.delayClose = false;
  61. return;
  62. }
  63. }
  64. this.innerRequestContext.Abort();
  65. }
  66. public override void Close()
  67. {
  68. lock (this.thisLock)
  69. {
  70. if (this.delayClose)
  71. {
  72. // Only delay the first attempt at Close/Abort
  73. this.delayClose = false;
  74. return;
  75. }
  76. }
  77. this.innerRequestContext.Close();
  78. }
  79. public override void Close(TimeSpan timeout)
  80. {
  81. lock (this.thisLock)
  82. {
  83. if (this.delayClose)
  84. {
  85. // Only delay the first attempt at Close/Abort
  86. this.delayClose = false;
  87. return;
  88. }
  89. }
  90. this.innerRequestContext.Close(timeout);
  91. }
  92. public override void Reply(Message message)
  93. {
  94. this.innerRequestContext.Reply(message);
  95. }
  96. public override void Reply(Message message, TimeSpan timeout)
  97. {
  98. this.innerRequestContext.Reply(message, timeout);
  99. }
  100. public override IAsyncResult BeginReply(Message message, AsyncCallback callback, object state)
  101. {
  102. return this.innerRequestContext.BeginReply(message, callback, state);
  103. }
  104. public override IAsyncResult BeginReply(Message message, TimeSpan timeout, AsyncCallback callback, object state)
  105. {
  106. return this.innerRequestContext.BeginReply(message, timeout, callback, state);
  107. }
  108. public override void EndReply(IAsyncResult result)
  109. {
  110. this.innerRequestContext.EndReply(result);
  111. }
  112. }
  113. }