InstanceContext.cs 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. //
  2. // InstanceContext.cs
  3. //
  4. // Author:
  5. // Atsushi Enomoto <[email protected]>
  6. //
  7. // Copyright (C) 2005-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.Collections.Generic;
  30. using System.ServiceModel.Channels;
  31. using System.ServiceModel.Dispatcher;
  32. namespace System.ServiceModel
  33. {
  34. [MonoTODO]
  35. public sealed class InstanceContext : CommunicationObject,
  36. IExtensibleObject<InstanceContext>
  37. {
  38. ServiceHostBase host;
  39. object implementation;
  40. int manual_flow_limit;
  41. InstanceBehavior _behavior;
  42. bool is_user_instance_provider;
  43. bool is_user_context_provider;
  44. static InstanceContextIdleCallback idle_callback = new InstanceContextIdleCallback(NotifyIdle);
  45. public InstanceContext (object implementation)
  46. : this (null, implementation)
  47. {
  48. }
  49. public InstanceContext (ServiceHostBase host)
  50. : this (host, null)
  51. {
  52. }
  53. public InstanceContext (ServiceHostBase host,
  54. object implementation) : this(host, implementation, true)
  55. {}
  56. internal InstanceContext(ServiceHostBase host,
  57. object implementation, bool userContextProvider)
  58. {
  59. this.host = host;
  60. this.implementation = implementation;
  61. is_user_context_provider = userContextProvider;
  62. }
  63. internal bool IsUserProvidedInstance {
  64. get {
  65. return is_user_instance_provider;
  66. }
  67. }
  68. internal bool IsUserProvidedContext {
  69. get {
  70. return is_user_context_provider;
  71. }
  72. }
  73. internal InstanceBehavior Behavior {
  74. get {
  75. return _behavior;
  76. }
  77. set {
  78. _behavior = value;
  79. }
  80. }
  81. protected internal override TimeSpan DefaultCloseTimeout {
  82. get { return host.DefaultCloseTimeout; }
  83. }
  84. protected internal override TimeSpan DefaultOpenTimeout {
  85. get { return host.DefaultOpenTimeout; }
  86. }
  87. public IExtensionCollection<InstanceContext> Extensions {
  88. get { throw new NotImplementedException (); }
  89. }
  90. public ServiceHostBase Host {
  91. get { return host; }
  92. }
  93. public ICollection<IChannel> IncomingChannels {
  94. get { throw new NotImplementedException (); }
  95. }
  96. public int ManualFlowControlLimit {
  97. get { return manual_flow_limit; }
  98. set { manual_flow_limit = value; }
  99. }
  100. public ICollection<IChannel> OutgoingChannels {
  101. get { throw new NotImplementedException (); }
  102. }
  103. public object GetServiceInstance ()
  104. {
  105. return GetServiceInstance (null);
  106. }
  107. public object GetServiceInstance (Message message)
  108. {
  109. if (implementation == null && Behavior != null) {
  110. implementation = Behavior.GetServiceInstance (this, message, ref is_user_instance_provider);
  111. }
  112. return implementation;
  113. }
  114. public void IncrementManualFlowControlLimit (int incrementBy)
  115. {
  116. throw new NotImplementedException ();
  117. }
  118. internal void CloseIfIdle () {
  119. if (Behavior.InstanceContextProvider != null && !IsUserProvidedContext) {
  120. if (!Behavior.InstanceContextProvider.IsIdle (this)) {
  121. Behavior.InstanceContextProvider.NotifyIdle (IdleCallback, this);
  122. }
  123. else {
  124. if (State != CommunicationState.Closed)
  125. Close ();
  126. }
  127. }
  128. }
  129. static void NotifyIdle (InstanceContext ctx) {
  130. ctx.CloseIfIdle ();
  131. }
  132. internal InstanceContextIdleCallback IdleCallback {
  133. get {
  134. return idle_callback;
  135. }
  136. }
  137. public void ReleaseServiceInstance ()
  138. {
  139. Behavior.ReleaseServiceInstance (this, implementation);
  140. implementation = null;
  141. }
  142. protected override void OnAbort ()
  143. {
  144. }
  145. protected override void OnClosed ()
  146. {
  147. base.OnClosed ();
  148. }
  149. protected override void OnOpened ()
  150. {
  151. }
  152. protected override void OnOpening () {
  153. if (Behavior != null)
  154. Behavior.Initialize (this);
  155. base.OnOpening ();
  156. }
  157. protected override IAsyncResult OnBeginOpen (
  158. TimeSpan timeout, AsyncCallback callback, object state)
  159. {
  160. throw new NotImplementedException ();
  161. }
  162. protected override void OnEndOpen (IAsyncResult result)
  163. {
  164. }
  165. protected override void OnOpen (TimeSpan timeout)
  166. {
  167. }
  168. protected override IAsyncResult OnBeginClose (
  169. TimeSpan timeout, AsyncCallback callback, object state)
  170. {
  171. throw new NotImplementedException ();
  172. }
  173. protected override void OnEndClose (IAsyncResult result)
  174. {
  175. }
  176. protected override void OnClose (TimeSpan timeout)
  177. {
  178. }
  179. }
  180. }