InstanceBehavior.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314
  1. //-----------------------------------------------------------------------------
  2. // Copyright (c) Microsoft Corporation. All rights reserved.
  3. //-----------------------------------------------------------------------------
  4. namespace System.ServiceModel.Dispatcher
  5. {
  6. using System;
  7. using System.Diagnostics;
  8. using System.Reflection;
  9. using System.Runtime;
  10. using System.ServiceModel;
  11. using System.ServiceModel.Channels;
  12. using System.ServiceModel.Diagnostics;
  13. using System.ServiceModel.Diagnostics.Application;
  14. class InstanceBehavior
  15. {
  16. const BindingFlags DefaultBindingFlags = BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.Public;
  17. bool useSession;
  18. ServiceHostBase host;
  19. IInstanceContextInitializer[] initializers;
  20. IInstanceContextProvider instanceContextProvider;
  21. IInstanceProvider provider;
  22. InstanceContext singleton;
  23. bool transactionAutoCompleteOnSessionClose;
  24. bool releaseServiceInstanceOnTransactionComplete = true;
  25. bool isSynchronized;
  26. ImmutableDispatchRuntime immutableRuntime;
  27. internal InstanceBehavior(DispatchRuntime dispatch, ImmutableDispatchRuntime immutableRuntime)
  28. {
  29. this.useSession = dispatch.ChannelDispatcher.Session;
  30. this.immutableRuntime = immutableRuntime;
  31. this.host = (dispatch.ChannelDispatcher == null) ? null : dispatch.ChannelDispatcher.Host;
  32. this.initializers = EmptyArray<IInstanceContextInitializer>.ToArray(dispatch.InstanceContextInitializers);
  33. this.provider = dispatch.InstanceProvider;
  34. this.singleton = dispatch.SingletonInstanceContext;
  35. this.transactionAutoCompleteOnSessionClose = dispatch.TransactionAutoCompleteOnSessionClose;
  36. this.releaseServiceInstanceOnTransactionComplete = dispatch.ReleaseServiceInstanceOnTransactionComplete;
  37. this.isSynchronized = (dispatch.ConcurrencyMode != ConcurrencyMode.Multiple);
  38. this.instanceContextProvider = dispatch.InstanceContextProvider;
  39. if (this.provider == null)
  40. {
  41. ConstructorInfo constructor = null;
  42. if (dispatch.Type != null)
  43. {
  44. constructor = InstanceBehavior.GetConstructor(dispatch.Type);
  45. }
  46. if (this.singleton == null)
  47. {
  48. if (dispatch.Type != null && (dispatch.Type.IsAbstract || dispatch.Type.IsInterface))
  49. {
  50. throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException(SR.GetString(SR.SFxServiceTypeNotCreatable)));
  51. }
  52. if (constructor == null)
  53. {
  54. throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException(SR.GetString(SR.SFxNoDefaultConstructor)));
  55. }
  56. }
  57. if (constructor != null)
  58. {
  59. if (this.singleton == null || !this.singleton.IsWellKnown)
  60. {
  61. InvokerUtil util = new InvokerUtil();
  62. CreateInstanceDelegate creator = util.GenerateCreateInstanceDelegate(dispatch.Type, constructor);
  63. this.provider = new InstanceProvider(creator);
  64. }
  65. }
  66. }
  67. if (this.singleton != null)
  68. {
  69. this.singleton.Behavior = this;
  70. }
  71. }
  72. internal bool TransactionAutoCompleteOnSessionClose
  73. {
  74. get
  75. {
  76. return this.transactionAutoCompleteOnSessionClose;
  77. }
  78. }
  79. internal bool ReleaseServiceInstanceOnTransactionComplete
  80. {
  81. get
  82. {
  83. return this.releaseServiceInstanceOnTransactionComplete;
  84. }
  85. }
  86. internal IInstanceContextProvider InstanceContextProvider
  87. {
  88. get
  89. {
  90. return this.instanceContextProvider;
  91. }
  92. }
  93. internal void AfterReply(ref MessageRpc rpc, ErrorBehavior error)
  94. {
  95. InstanceContext context = rpc.InstanceContext;
  96. if (context != null)
  97. {
  98. try
  99. {
  100. if (rpc.Operation.ReleaseInstanceAfterCall)
  101. {
  102. if (context.State == CommunicationState.Opened)
  103. {
  104. context.ReleaseServiceInstance();
  105. }
  106. }
  107. else if (releaseServiceInstanceOnTransactionComplete &&
  108. this.isSynchronized &&
  109. rpc.transaction != null &&
  110. (rpc.transaction.IsCompleted || (rpc.Error != null)))
  111. {
  112. if (context.State == CommunicationState.Opened)
  113. {
  114. context.ReleaseServiceInstance();
  115. }
  116. if (DiagnosticUtility.ShouldTraceInformation)
  117. {
  118. TraceUtility.TraceEvent(TraceEventType.Information,
  119. TraceCode.TxReleaseServiceInstanceOnCompletion,
  120. SR.GetString(SR.TraceCodeTxReleaseServiceInstanceOnCompletion, "*"));
  121. }
  122. }
  123. }
  124. catch (Exception e)
  125. {
  126. if (Fx.IsFatal(e))
  127. {
  128. throw;
  129. }
  130. error.HandleError(e);
  131. }
  132. try
  133. {
  134. context.UnbindRpc(ref rpc);
  135. }
  136. catch (Exception e)
  137. {
  138. if (Fx.IsFatal(e))
  139. {
  140. throw;
  141. }
  142. error.HandleError(e);
  143. }
  144. }
  145. }
  146. internal bool CanUnload(InstanceContext instanceContext)
  147. {
  148. if (InstanceContextProviderBase.IsProviderSingleton(this.instanceContextProvider))
  149. return false;
  150. if (InstanceContextProviderBase.IsProviderPerCall(this.instanceContextProvider) ||
  151. InstanceContextProviderBase.IsProviderSessionful(this.instanceContextProvider))
  152. return true;
  153. //User provided InstanceContextProvider. Call the provider to check for idle.
  154. if (!this.instanceContextProvider.IsIdle(instanceContext))
  155. {
  156. this.instanceContextProvider.NotifyIdle(InstanceContext.NotifyIdleCallback, instanceContext);
  157. return false;
  158. }
  159. return true;
  160. }
  161. internal void EnsureInstanceContext(ref MessageRpc rpc)
  162. {
  163. if (rpc.InstanceContext == null)
  164. {
  165. rpc.InstanceContext = new InstanceContext(rpc.Host, false);
  166. rpc.InstanceContext.ServiceThrottle = rpc.channelHandler.InstanceContextServiceThrottle;
  167. rpc.MessageRpcOwnsInstanceContextThrottle = false;
  168. }
  169. rpc.OperationContext.SetInstanceContext(rpc.InstanceContext);
  170. rpc.InstanceContext.Behavior = this;
  171. if (rpc.InstanceContext.State == CommunicationState.Created)
  172. {
  173. lock (rpc.InstanceContext.ThisLock)
  174. {
  175. if (rpc.InstanceContext.State == CommunicationState.Created)
  176. {
  177. rpc.InstanceContext.Open(rpc.Channel.CloseTimeout);
  178. }
  179. }
  180. }
  181. rpc.InstanceContext.BindRpc(ref rpc);
  182. }
  183. static ConstructorInfo GetConstructor(Type type)
  184. {
  185. return type.GetConstructor(DefaultBindingFlags, null, Type.EmptyTypes, null);
  186. }
  187. internal object GetInstance(InstanceContext instanceContext)
  188. {
  189. if (this.provider == null)
  190. {
  191. throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException(SR.GetString(SR.SFxNoDefaultConstructor)));
  192. }
  193. return this.provider.GetInstance(instanceContext);
  194. }
  195. internal object GetInstance(InstanceContext instanceContext, Message request)
  196. {
  197. if (this.provider == null)
  198. {
  199. throw TraceUtility.ThrowHelperError(new InvalidOperationException(SR.GetString(SR.SFxNoDefaultConstructor)), request);
  200. }
  201. return this.provider.GetInstance(instanceContext, request);
  202. }
  203. internal void Initialize(InstanceContext instanceContext)
  204. {
  205. OperationContext current = OperationContext.Current;
  206. Message message = (current != null) ? current.IncomingMessage : null;
  207. if (current != null && current.InternalServiceChannel != null)
  208. {
  209. IContextChannel transparentProxy = (IContextChannel)current.InternalServiceChannel.Proxy;
  210. this.instanceContextProvider.InitializeInstanceContext(instanceContext, message, transparentProxy);
  211. }
  212. for (int i = 0; i < this.initializers.Length; i++)
  213. this.initializers[i].Initialize(instanceContext, message);
  214. }
  215. internal void EnsureServiceInstance(ref MessageRpc rpc)
  216. {
  217. if (rpc.Operation.ReleaseInstanceBeforeCall)
  218. {
  219. rpc.InstanceContext.ReleaseServiceInstance();
  220. }
  221. if (TD.GetServiceInstanceStartIsEnabled())
  222. {
  223. TD.GetServiceInstanceStart(rpc.EventTraceActivity);
  224. }
  225. rpc.Instance = rpc.InstanceContext.GetServiceInstance(rpc.Request);
  226. if (TD.GetServiceInstanceStopIsEnabled())
  227. {
  228. TD.GetServiceInstanceStop(rpc.EventTraceActivity);
  229. }
  230. }
  231. internal void ReleaseInstance(InstanceContext instanceContext, object instance)
  232. {
  233. if (this.provider != null)
  234. {
  235. try
  236. {
  237. this.provider.ReleaseInstance(instanceContext, instance);
  238. }
  239. catch (Exception e)
  240. {
  241. if (Fx.IsFatal(e))
  242. {
  243. throw;
  244. }
  245. this.immutableRuntime.ErrorBehavior.HandleError(e);
  246. }
  247. }
  248. }
  249. }
  250. class InstanceProvider : IInstanceProvider
  251. {
  252. CreateInstanceDelegate creator;
  253. internal InstanceProvider(CreateInstanceDelegate creator)
  254. {
  255. if (creator == null)
  256. throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull("creator");
  257. this.creator = creator;
  258. }
  259. public object GetInstance(InstanceContext instanceContext)
  260. {
  261. return this.creator();
  262. }
  263. public object GetInstance(InstanceContext instanceContext, Message message)
  264. {
  265. return this.creator();
  266. }
  267. public void ReleaseInstance(InstanceContext instanceContext, object instance)
  268. {
  269. IDisposable dispose = instance as IDisposable;
  270. if (dispose != null)
  271. dispose.Dispose();
  272. }
  273. }
  274. }