OperationBehaviorAttribute.cs 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. //------------------------------------------------------------
  2. // Copyright (c) Microsoft Corporation. All rights reserved.
  3. //------------------------------------------------------------
  4. namespace System.ServiceModel
  5. {
  6. using System.Reflection;
  7. using System.ServiceModel.Channels;
  8. using System.ServiceModel.Dispatcher;
  9. using System.ServiceModel.Description;
  10. using System.Transactions;
  11. using System.ServiceModel.Security;
  12. using System.Security.Principal;
  13. using System.Collections.Generic;
  14. [AttributeUsage(ServiceModelAttributeTargets.OperationBehavior)]
  15. public sealed class OperationBehaviorAttribute : Attribute, IOperationBehavior
  16. {
  17. internal const ImpersonationOption DefaultImpersonationOption = ImpersonationOption.NotAllowed;
  18. bool autoCompleteTransaction = true;
  19. bool autoEnlistTransaction = false;
  20. bool autoDisposeParameters = true;
  21. bool preferAsyncInvocation = false;
  22. ImpersonationOption impersonation = ImpersonationOption.NotAllowed;
  23. ReleaseInstanceMode releaseInstance = ReleaseInstanceMode.None;
  24. public bool TransactionAutoComplete
  25. {
  26. get { return this.autoCompleteTransaction; }
  27. set { this.autoCompleteTransaction = value; }
  28. }
  29. public bool TransactionScopeRequired
  30. {
  31. get { return this.autoEnlistTransaction; }
  32. set { this.autoEnlistTransaction = value; }
  33. }
  34. public bool AutoDisposeParameters
  35. {
  36. get { return this.autoDisposeParameters; }
  37. set { this.autoDisposeParameters = value; }
  38. }
  39. public ImpersonationOption Impersonation
  40. {
  41. get
  42. {
  43. return this.impersonation;
  44. }
  45. set
  46. {
  47. if (!ImpersonationOptionHelper.IsDefined(value))
  48. {
  49. throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ArgumentOutOfRangeException("value"));
  50. }
  51. this.impersonation = value;
  52. }
  53. }
  54. public ReleaseInstanceMode ReleaseInstanceMode
  55. {
  56. get { return this.releaseInstance; }
  57. set
  58. {
  59. if (!ReleaseInstanceModeHelper.IsDefined(value))
  60. {
  61. throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ArgumentOutOfRangeException("value"));
  62. }
  63. this.releaseInstance = value;
  64. }
  65. }
  66. internal bool PreferAsyncInvocation
  67. {
  68. get { return this.preferAsyncInvocation; }
  69. set { this.preferAsyncInvocation = value; }
  70. }
  71. void IOperationBehavior.Validate(OperationDescription description)
  72. {
  73. }
  74. void IOperationBehavior.AddBindingParameters(OperationDescription description, BindingParameterCollection parameters)
  75. {
  76. }
  77. void IOperationBehavior.ApplyDispatchBehavior(OperationDescription description, DispatchOperation dispatch)
  78. {
  79. if (description == null)
  80. {
  81. throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull("description");
  82. }
  83. if (dispatch == null)
  84. {
  85. throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull("dispatch");
  86. }
  87. if (description.IsServerInitiated() && this.releaseInstance != ReleaseInstanceMode.None)
  88. {
  89. throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException(
  90. SR.GetString(SR.SFxOperationBehaviorAttributeReleaseInstanceModeDoesNotApplyToCallback,
  91. description.Name)));
  92. }
  93. dispatch.TransactionRequired = this.autoEnlistTransaction;
  94. dispatch.TransactionAutoComplete = this.autoCompleteTransaction;
  95. dispatch.AutoDisposeParameters = this.autoDisposeParameters;
  96. dispatch.ReleaseInstanceBeforeCall = (this.releaseInstance & ReleaseInstanceMode.BeforeCall) != 0;
  97. dispatch.ReleaseInstanceAfterCall = (this.releaseInstance & ReleaseInstanceMode.AfterCall) != 0;
  98. dispatch.Impersonation = this.Impersonation;
  99. }
  100. void IOperationBehavior.ApplyClientBehavior(OperationDescription description, ClientOperation proxy)
  101. {
  102. }
  103. }
  104. }