TransactionValidationBehavior.cs 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386
  1. //------------------------------------------------------------
  2. // Copyright (c) Microsoft Corporation. All rights reserved.
  3. //------------------------------------------------------------
  4. namespace System.ServiceModel.Dispatcher
  5. {
  6. using System.Collections.ObjectModel;
  7. using System.ServiceModel.Channels;
  8. using System.ServiceModel;
  9. using System.ServiceModel.Description;
  10. using System.Collections.Generic;
  11. using System.Runtime.CompilerServices;
  12. using System.ServiceModel.Transactions;
  13. class TransactionValidationBehavior : IEndpointBehavior, IServiceBehavior
  14. {
  15. static TransactionValidationBehavior instance;
  16. internal static TransactionValidationBehavior Instance
  17. {
  18. get
  19. {
  20. if (instance == null)
  21. instance = new TransactionValidationBehavior();
  22. return instance;
  23. }
  24. }
  25. TransactionValidationBehavior() { }
  26. void ValidateTransactionFlowRequired(string resource, string name, ServiceEndpoint endpoint)
  27. {
  28. bool anOperationRequiresTxFlow = false;
  29. for (int i = 0; i < endpoint.Contract.Operations.Count; i++)
  30. {
  31. OperationDescription operationDescription = endpoint.Contract.Operations[i];
  32. TransactionFlowAttribute transactionFlow = operationDescription.Behaviors.Find<TransactionFlowAttribute>();
  33. if (transactionFlow != null && transactionFlow.Transactions == TransactionFlowOption.Mandatory)
  34. {
  35. anOperationRequiresTxFlow = true;
  36. break;
  37. }
  38. }
  39. if (anOperationRequiresTxFlow)
  40. {
  41. CustomBinding binding = new CustomBinding(endpoint.Binding);
  42. TransactionFlowBindingElement transactionFlowBindingElement =
  43. binding.Elements.Find<TransactionFlowBindingElement>();
  44. if (transactionFlowBindingElement == null || !transactionFlowBindingElement.Transactions)
  45. {
  46. throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException(
  47. String.Format(Globalization.CultureInfo.CurrentCulture, SR.GetString(resource), name, binding.Name)));
  48. }
  49. }
  50. }
  51. void IEndpointBehavior.Validate(ServiceEndpoint serviceEndpoint)
  52. {
  53. if (serviceEndpoint == null)
  54. throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull("serviceEndpoint");
  55. ValidateTransactionFlowRequired(SR.ChannelHasAtLeastOneOperationWithTransactionFlowEnabled,
  56. serviceEndpoint.Contract.Name,
  57. serviceEndpoint);
  58. EnsureNoOneWayTransactions(serviceEndpoint);
  59. ValidateNoMSMQandTransactionFlow(serviceEndpoint);
  60. ValidateCallbackBehaviorAttributeWithNoScopeRequired(serviceEndpoint);
  61. OperationDescription autoCompleteFalseOperation = GetAutoCompleteFalseOperation(serviceEndpoint);
  62. if (autoCompleteFalseOperation != null)
  63. {
  64. throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException(
  65. SR.GetString(SR.SFxTransactionAutoCompleteFalseOnCallbackContract, autoCompleteFalseOperation.Name, serviceEndpoint.Contract.Name)));
  66. }
  67. }
  68. void ValidateCallbackBehaviorAttributeWithNoScopeRequired(ServiceEndpoint endpoint)
  69. {
  70. // If the endpoint has no operations with TransactionScopeRequired=true, disallow any
  71. // transaction-related properties on the CallbackBehaviorAttribute
  72. if (!HasTransactedOperations(endpoint))
  73. {
  74. CallbackBehaviorAttribute attribute = endpoint.Behaviors.Find<CallbackBehaviorAttribute>();
  75. if (attribute != null)
  76. {
  77. if (attribute.TransactionTimeoutSet)
  78. {
  79. throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException(
  80. SR.GetString(SR.SFxTransactionTransactionTimeoutNeedsScope, endpoint.Contract.Name)));
  81. }
  82. if (attribute.IsolationLevelSet)
  83. {
  84. throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException(
  85. SR.GetString(SR.SFxTransactionIsolationLevelNeedsScope, endpoint.Contract.Name)));
  86. }
  87. }
  88. }
  89. }
  90. void IEndpointBehavior.AddBindingParameters(ServiceEndpoint serviceEndpoint, BindingParameterCollection bindingParameters)
  91. {
  92. }
  93. void IEndpointBehavior.ApplyDispatchBehavior(ServiceEndpoint serviceEndpoint, EndpointDispatcher endpointDispatcher)
  94. {
  95. }
  96. void IEndpointBehavior.ApplyClientBehavior(ServiceEndpoint serviceEndpoint, ClientRuntime behavior)
  97. {
  98. }
  99. void IServiceBehavior.AddBindingParameters(ServiceDescription description, ServiceHostBase serviceHostBase, Collection<ServiceEndpoint> endpoints, BindingParameterCollection parameters)
  100. {
  101. }
  102. void IServiceBehavior.ApplyDispatchBehavior(ServiceDescription service, ServiceHostBase serviceHostBase)
  103. {
  104. }
  105. void IServiceBehavior.Validate(ServiceDescription service, ServiceHostBase serviceHostBase)
  106. {
  107. if (service == null)
  108. throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull("service");
  109. ValidateNotConcurrentWhenReleaseServiceInstanceOnTxComplete(service);
  110. bool singleThreaded = IsSingleThreaded(service);
  111. for (int i = 0; i < service.Endpoints.Count; i++)
  112. {
  113. ServiceEndpoint endpoint = service.Endpoints[i];
  114. ValidateTransactionFlowRequired(SR.ServiceHasAtLeastOneOperationWithTransactionFlowEnabled,
  115. service.Name,
  116. endpoint);
  117. EnsureNoOneWayTransactions(endpoint);
  118. ValidateNoMSMQandTransactionFlow(endpoint);
  119. ContractDescription contract = endpoint.Contract;
  120. for (int j = 0; j < contract.Operations.Count; j++)
  121. {
  122. OperationDescription operation = contract.Operations[j];
  123. ValidateScopeRequiredAndAutoComplete(operation, singleThreaded, contract.Name);
  124. }
  125. ValidateAutoCompleteFalseRequirements(service, endpoint);
  126. }
  127. ValidateServiceBehaviorAttributeWithNoScopeRequired(service);
  128. ValidateTransactionAutoCompleteOnSessionCloseHasSession(service);
  129. }
  130. void ValidateAutoCompleteFalseRequirements(ServiceDescription service, ServiceEndpoint endpoint)
  131. {
  132. OperationDescription autoCompleteFalseOperation = GetAutoCompleteFalseOperation(endpoint);
  133. if (autoCompleteFalseOperation != null)
  134. {
  135. // Does the service have InstanceContextMode.PerSession or Shareable?
  136. ServiceBehaviorAttribute serviceBehavior = service.Behaviors.Find<ServiceBehaviorAttribute>();
  137. if (serviceBehavior != null)
  138. {
  139. InstanceContextMode instanceMode = serviceBehavior.InstanceContextMode;
  140. if (instanceMode != InstanceContextMode.PerSession)
  141. {
  142. throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException(
  143. SR.GetString(SR.SFxTransactionAutoCompleteFalseAndInstanceContextMode,
  144. endpoint.Contract.Name, autoCompleteFalseOperation.Name)));
  145. }
  146. }
  147. // Does the binding support sessions?
  148. if (!autoCompleteFalseOperation.IsInsideTransactedReceiveScope)
  149. {
  150. if (!RequiresSessions(endpoint))
  151. {
  152. throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException(
  153. SR.GetString(SR.SFxTransactionAutoCompleteFalseAndSupportsSession,
  154. endpoint.Contract.Name, autoCompleteFalseOperation.Name)));
  155. }
  156. }
  157. }
  158. }
  159. OperationDescription GetAutoCompleteFalseOperation(ServiceEndpoint endpoint)
  160. {
  161. foreach (OperationDescription operation in endpoint.Contract.Operations)
  162. {
  163. if (!IsAutoComplete(operation))
  164. {
  165. return operation;
  166. }
  167. }
  168. return null;
  169. }
  170. void ValidateTransactionAutoCompleteOnSessionCloseHasSession(ServiceDescription service)
  171. {
  172. ServiceBehaviorAttribute serviceBehavior = service.Behaviors.Find<ServiceBehaviorAttribute>();
  173. if (serviceBehavior != null)
  174. {
  175. InstanceContextMode instanceMode = serviceBehavior.InstanceContextMode;
  176. if (serviceBehavior.TransactionAutoCompleteOnSessionClose &&
  177. instanceMode != InstanceContextMode.PerSession)
  178. {
  179. throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException(
  180. SR.GetString(SR.SFxTransactionAutoCompleteOnSessionCloseNoSession, service.Name)));
  181. }
  182. }
  183. }
  184. void ValidateServiceBehaviorAttributeWithNoScopeRequired(ServiceDescription service)
  185. {
  186. // If the service has no operations with TransactionScopeRequired=true, disallow any
  187. // transaction-related properties on the ServiceBehaviorAttribute
  188. if (!HasTransactedOperations(service))
  189. {
  190. ServiceBehaviorAttribute attribute = service.Behaviors.Find<ServiceBehaviorAttribute>();
  191. if (attribute != null)
  192. {
  193. if (attribute.TransactionTimeoutSet)
  194. {
  195. throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException(
  196. SR.GetString(SR.SFxTransactionTransactionTimeoutNeedsScope, service.Name)));
  197. }
  198. if (attribute.IsolationLevelSet)
  199. {
  200. throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException(
  201. SR.GetString(SR.SFxTransactionIsolationLevelNeedsScope, service.Name)));
  202. }
  203. if (attribute.ReleaseServiceInstanceOnTransactionCompleteSet)
  204. {
  205. throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException(
  206. SR.GetString(SR.SFxTransactionReleaseServiceInstanceOnTransactionCompleteNeedsScope, service.Name)));
  207. }
  208. if (attribute.TransactionAutoCompleteOnSessionCloseSet)
  209. {
  210. throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException(
  211. SR.GetString(SR.SFxTransactionTransactionAutoCompleteOnSessionCloseNeedsScope, service.Name)));
  212. }
  213. }
  214. }
  215. }
  216. void EnsureNoOneWayTransactions(ServiceEndpoint endpoint)
  217. {
  218. CustomBinding binding = new CustomBinding(endpoint.Binding);
  219. TransactionFlowBindingElement txFlowBindingElement = binding.Elements.Find<TransactionFlowBindingElement>();
  220. if (txFlowBindingElement != null)
  221. {
  222. for (int i = 0; i < endpoint.Contract.Operations.Count; i++)
  223. {
  224. OperationDescription operation = endpoint.Contract.Operations[i];
  225. if (operation.IsOneWay)
  226. {
  227. TransactionFlowAttribute tfbp = operation.Behaviors.Find<TransactionFlowAttribute>();
  228. TransactionFlowOption transactions;
  229. if (tfbp != null)
  230. {
  231. transactions = tfbp.Transactions;
  232. }
  233. else
  234. {
  235. transactions = TransactionFlowOption.NotAllowed;
  236. }
  237. if (TransactionFlowOptionHelper.AllowedOrRequired(transactions))
  238. {
  239. throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException(
  240. SR.GetString(SR.SFxOneWayAndTransactionsIncompatible, endpoint.Contract.Name, operation.Name)));
  241. }
  242. }
  243. }
  244. }
  245. }
  246. bool HasTransactedOperations(ServiceDescription service)
  247. {
  248. for (int i = 0; i < service.Endpoints.Count; i++)
  249. {
  250. if (HasTransactedOperations(service.Endpoints[i]))
  251. {
  252. return true;
  253. }
  254. }
  255. return false;
  256. }
  257. bool HasTransactedOperations(ServiceEndpoint endpoint)
  258. {
  259. for (int j = 0; j < endpoint.Contract.Operations.Count; j++)
  260. {
  261. OperationDescription operation = endpoint.Contract.Operations[j];
  262. OperationBehaviorAttribute attribute = operation.Behaviors.Find<OperationBehaviorAttribute>();
  263. if (attribute != null && attribute.TransactionScopeRequired)
  264. {
  265. return true;
  266. }
  267. }
  268. return false;
  269. }
  270. bool IsSingleThreaded(ServiceDescription service)
  271. {
  272. ServiceBehaviorAttribute attribute = service.Behaviors.Find<ServiceBehaviorAttribute>();
  273. if (attribute != null)
  274. {
  275. return (attribute.ConcurrencyMode == ConcurrencyMode.Single);
  276. }
  277. // The default is ConcurrencyMode.Single
  278. return true;
  279. }
  280. bool IsAutoComplete(OperationDescription operation)
  281. {
  282. OperationBehaviorAttribute attribute = operation.Behaviors.Find<OperationBehaviorAttribute>();
  283. if (attribute != null)
  284. {
  285. return attribute.TransactionAutoComplete;
  286. }
  287. // The default is TransactionAutoComplete=true
  288. return true;
  289. }
  290. bool RequiresSessions(ServiceEndpoint endpoint)
  291. {
  292. return endpoint.Contract.SessionMode == SessionMode.Required;
  293. }
  294. void ValidateScopeRequiredAndAutoComplete(OperationDescription operation,
  295. bool singleThreaded,
  296. string contractName)
  297. {
  298. OperationBehaviorAttribute attribute = operation.Behaviors.Find<OperationBehaviorAttribute>();
  299. if (attribute != null)
  300. {
  301. if (!singleThreaded && !attribute.TransactionAutoComplete)
  302. {
  303. string id = SR.SFxTransactionNonConcurrentOrAutoComplete2;
  304. throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException(
  305. SR.GetString(id, contractName, operation.Name)));
  306. }
  307. }
  308. }
  309. void ValidateNoMSMQandTransactionFlow(ServiceEndpoint endpoint)
  310. {
  311. BindingElementCollection bindingElements = endpoint.Binding.CreateBindingElements();
  312. if (bindingElements.Find<TransactionFlowBindingElement>() != null &&
  313. bindingElements.Find<MsmqTransportBindingElement>() != null)
  314. {
  315. throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException(
  316. SR.GetString(SR.SFxTransactionFlowAndMSMQ, endpoint.Address.Uri.AbsoluteUri)));
  317. }
  318. }
  319. void ValidateNotConcurrentWhenReleaseServiceInstanceOnTxComplete(ServiceDescription service)
  320. {
  321. ServiceBehaviorAttribute attribute = service.Behaviors.Find<ServiceBehaviorAttribute>();
  322. if (attribute != null && HasTransactedOperations(service))
  323. {
  324. if (attribute.ReleaseServiceInstanceOnTransactionComplete && !IsSingleThreaded(service))
  325. {
  326. throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(
  327. new InvalidOperationException(SR.GetString(
  328. SR.SFxTransactionNonConcurrentOrReleaseServiceInstanceOnTxComplete, service.Name)));
  329. }
  330. }
  331. }
  332. }
  333. }