DebugBindingElement.cs 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. //
  2. // DebugBindingElement.cs
  3. //
  4. // Author:
  5. // Atsushi Enomoto <[email protected]>
  6. //
  7. // Copyright (C) 2006 Novell, Inc. http://www.novell.com
  8. //
  9. // Permission is hereby granted, free of charge, to any person obtaining
  10. // a copy of this software and associated documentation files (the
  11. // "Software"), to deal in the Software without restriction, including
  12. // without limitation the rights to use, copy, modify, merge, publish,
  13. // distribute, sublicense, and/or sell copies of the Software, and to
  14. // permit persons to whom the Software is furnished to do so, subject to
  15. // the following conditions:
  16. //
  17. // The above copyright notice and this permission notice shall be
  18. // included in all copies or substantial portions of the Software.
  19. //
  20. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  21. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  22. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  23. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  24. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  25. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  26. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  27. //
  28. using System;
  29. using System.ServiceModel;
  30. using System.ServiceModel.Channels;
  31. using System.ServiceModel.Description;
  32. using System.Xml;
  33. using NUnit.Framework;
  34. // This binding element class is for testing some bindings to intercept
  35. // messages.
  36. namespace MonoTests.System.ServiceModel.Channels
  37. {
  38. class DebugBindingElement : BindingElement
  39. {
  40. public override IChannelFactory<TChannel> BuildChannelFactory<TChannel> (BindingContext context)
  41. {
  42. return new DebugChannelFactory<TChannel> (context.BuildInnerChannelFactory<TChannel> ());
  43. }
  44. public override BindingElement Clone ()
  45. {
  46. return new DebugBindingElement ();
  47. }
  48. public override T GetProperty<T> (BindingContext context)
  49. {
  50. return context.GetInnerProperty<T> ();
  51. }
  52. }
  53. class DebugChannelFactory<TChannel> : ChannelFactoryBase<TChannel>
  54. {
  55. IChannelFactory<TChannel> inner;
  56. public DebugChannelFactory (IChannelFactory<TChannel> inner)
  57. {
  58. this.inner = inner;
  59. }
  60. public override T GetProperty<T> ()
  61. {
  62. return inner.GetProperty<T> ();
  63. }
  64. protected override void OnOpen (TimeSpan timeout)
  65. {
  66. inner.Open (timeout);
  67. }
  68. protected override IAsyncResult OnBeginOpen (TimeSpan timeout, AsyncCallback callback, object state)
  69. {
  70. throw new NotSupportedException ();
  71. }
  72. protected override void OnEndOpen (IAsyncResult result)
  73. {
  74. throw new NotSupportedException ();
  75. }
  76. protected override TChannel OnCreateChannel (EndpointAddress ep, Uri via)
  77. {
  78. if (typeof (TChannel) == typeof (IRequestChannel))
  79. return (TChannel) (object) new DebugRequestChannel (this, (IRequestChannel) (object) inner.CreateChannel (ep, via));
  80. throw new Exception ("huh?");
  81. }
  82. }
  83. class DebugRequestChannel : RequestChannelBase
  84. {
  85. ChannelFactoryBase source;
  86. IRequestChannel inner;
  87. public DebugRequestChannel (ChannelFactoryBase source, IRequestChannel inner)
  88. : base (source)
  89. {
  90. this.source = source;
  91. this.inner = inner;
  92. }
  93. public override EndpointAddress RemoteAddress {
  94. get { return inner.RemoteAddress; }
  95. }
  96. public override Uri Via {
  97. get { return inner.Via; }
  98. }
  99. protected override void OnAbort ()
  100. {
  101. inner.Abort ();
  102. }
  103. protected override void OnOpen (TimeSpan timeout)
  104. {
  105. inner.Open (timeout);
  106. }
  107. protected override IAsyncResult OnBeginOpen (TimeSpan timeout, AsyncCallback callback, object state)
  108. {
  109. throw new NotSupportedException ();
  110. }
  111. protected override void OnEndOpen (IAsyncResult result)
  112. {
  113. throw new NotSupportedException ();
  114. }
  115. protected override void OnClose (TimeSpan timeout)
  116. {
  117. inner.Close (timeout);
  118. }
  119. protected override IAsyncResult OnBeginClose (TimeSpan timeout, AsyncCallback callback, object state)
  120. {
  121. throw new NotSupportedException ();
  122. }
  123. protected override void OnEndClose (IAsyncResult result)
  124. {
  125. throw new NotSupportedException ();
  126. }
  127. public override Message Request (Message req, TimeSpan timeout)
  128. {
  129. XmlWriterSettings settings = new XmlWriterSettings ();
  130. settings.Indent = true;
  131. MessageBuffer buf = req.CreateBufferedCopy (0x10000);
  132. using (XmlWriter w = XmlWriter.Create (Console.Error, settings)) {
  133. buf.CreateMessage ().WriteMessage (w);
  134. }
  135. Console.Error.WriteLine ("******************** Debug Request() ********************");
  136. Console.Error.Flush ();
  137. return inner.Request (buf.CreateMessage (), timeout);
  138. }
  139. public override IAsyncResult BeginRequest (Message req, TimeSpan timeout, AsyncCallback callback, object state)
  140. {
  141. throw new NotSupportedException ();
  142. }
  143. public override Message EndRequest (IAsyncResult result)
  144. {
  145. throw new NotSupportedException ();
  146. }
  147. }
  148. }