ContextReplyChannel.cs 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. //------------------------------------------------------------
  2. // Copyright (c) Microsoft Corporation. All rights reserved.
  3. //------------------------------------------------------------
  4. namespace System.ServiceModel.Channels
  5. {
  6. using System;
  7. using System.Collections.Generic;
  8. using System.ServiceModel;
  9. class ContextReplyChannel : LayeredChannel<IReplyChannel>, IReplyChannel
  10. {
  11. ContextExchangeMechanism contextExchangeMechanism;
  12. public ContextReplyChannel(ChannelManagerBase channelManager, IReplyChannel innerChannel, ContextExchangeMechanism contextExchangeMechanism)
  13. : base(channelManager, innerChannel)
  14. {
  15. this.contextExchangeMechanism = contextExchangeMechanism;
  16. }
  17. public EndpointAddress LocalAddress
  18. {
  19. get { return this.InnerChannel.LocalAddress; }
  20. }
  21. public IAsyncResult BeginReceiveRequest(TimeSpan timeout, AsyncCallback callback, object state)
  22. {
  23. return this.InnerChannel.BeginReceiveRequest(timeout, callback, state);
  24. }
  25. public IAsyncResult BeginReceiveRequest(AsyncCallback callback, object state)
  26. {
  27. return this.InnerChannel.BeginReceiveRequest(callback, state);
  28. }
  29. public IAsyncResult BeginTryReceiveRequest(TimeSpan timeout, AsyncCallback callback, object state)
  30. {
  31. return this.InnerChannel.BeginTryReceiveRequest(timeout, callback, state);
  32. }
  33. public IAsyncResult BeginWaitForRequest(TimeSpan timeout, AsyncCallback callback, object state)
  34. {
  35. return this.InnerChannel.BeginWaitForRequest(timeout, callback, state);
  36. }
  37. public RequestContext EndReceiveRequest(IAsyncResult result)
  38. {
  39. RequestContext innerContext = this.InnerChannel.EndReceiveRequest(result);
  40. if (innerContext == null)
  41. {
  42. return null;
  43. }
  44. else
  45. {
  46. return this.CreateContextChannelRequestContext(innerContext);
  47. }
  48. }
  49. public bool EndTryReceiveRequest(IAsyncResult result, out RequestContext context)
  50. {
  51. context = null;
  52. RequestContext innerContext;
  53. if (this.InnerChannel.EndTryReceiveRequest(result, out innerContext))
  54. {
  55. if (innerContext != null)
  56. {
  57. context = this.CreateContextChannelRequestContext(innerContext);
  58. }
  59. return true;
  60. }
  61. else
  62. {
  63. return false;
  64. }
  65. }
  66. public bool EndWaitForRequest(IAsyncResult result)
  67. {
  68. return this.InnerChannel.EndWaitForRequest(result);
  69. }
  70. public RequestContext ReceiveRequest(TimeSpan timeout)
  71. {
  72. RequestContext innerContext = this.InnerChannel.ReceiveRequest(timeout);
  73. if (innerContext == null)
  74. {
  75. return null;
  76. }
  77. else
  78. {
  79. return this.CreateContextChannelRequestContext(innerContext);
  80. }
  81. }
  82. public RequestContext ReceiveRequest()
  83. {
  84. RequestContext innerContext = this.InnerChannel.ReceiveRequest();
  85. if (innerContext == null)
  86. {
  87. return null;
  88. }
  89. else
  90. {
  91. return this.CreateContextChannelRequestContext(innerContext);
  92. }
  93. }
  94. public bool TryReceiveRequest(TimeSpan timeout, out RequestContext context)
  95. {
  96. RequestContext innerContext;
  97. if (this.InnerChannel.TryReceiveRequest(timeout, out innerContext))
  98. {
  99. context = this.CreateContextChannelRequestContext(innerContext);
  100. return true;
  101. }
  102. else
  103. {
  104. context = null;
  105. return false;
  106. }
  107. }
  108. public bool WaitForRequest(TimeSpan timeout)
  109. {
  110. return this.InnerChannel.WaitForRequest(timeout);
  111. }
  112. ContextChannelRequestContext CreateContextChannelRequestContext(RequestContext innerContext)
  113. {
  114. ServiceContextProtocol contextProtocol = new ServiceContextProtocol(this.contextExchangeMechanism);
  115. contextProtocol.OnIncomingMessage(innerContext.RequestMessage);
  116. return new ContextChannelRequestContext(innerContext, contextProtocol, this.DefaultSendTimeout);
  117. }
  118. }
  119. }