OperationContractAttribute.cs 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. //------------------------------------------------------------
  2. // Copyright (c) Microsoft Corporation. All rights reserved.
  3. //------------------------------------------------------------
  4. namespace System.ServiceModel
  5. {
  6. using System.Net.Security;
  7. using System.Reflection;
  8. using System.ServiceModel.Description;
  9. using System.ServiceModel.Security;
  10. [AttributeUsage(ServiceModelAttributeTargets.OperationContract)]
  11. public sealed class OperationContractAttribute : Attribute
  12. {
  13. string name = null;
  14. string action = null;
  15. string replyAction = null;
  16. bool asyncPattern = false;
  17. bool isInitiating = true;
  18. bool isTerminating = false;
  19. bool isOneWay = false;
  20. ProtectionLevel protectionLevel = ProtectionLevel.None;
  21. bool hasProtectionLevel = false;
  22. public string Name
  23. {
  24. get { return this.name; }
  25. set
  26. {
  27. if (value == null)
  28. {
  29. throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull("value");
  30. }
  31. if (value == "")
  32. {
  33. throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ArgumentOutOfRangeException("value",
  34. SR.GetString(SR.SFxNameCannotBeEmpty)));
  35. }
  36. this.name = value;
  37. }
  38. }
  39. internal const string ActionPropertyName = "Action";
  40. public string Action
  41. {
  42. get { return this.action; }
  43. set
  44. {
  45. if (value == null)
  46. {
  47. throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull("value");
  48. }
  49. this.action = value;
  50. }
  51. }
  52. internal const string ProtectionLevelPropertyName = "ProtectionLevel";
  53. public ProtectionLevel ProtectionLevel
  54. {
  55. get
  56. {
  57. return this.protectionLevel;
  58. }
  59. set
  60. {
  61. if (!ProtectionLevelHelper.IsDefined(value))
  62. throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ArgumentOutOfRangeException("value"));
  63. this.protectionLevel = value;
  64. this.hasProtectionLevel = true;
  65. }
  66. }
  67. public bool HasProtectionLevel
  68. {
  69. get { return this.hasProtectionLevel; }
  70. }
  71. internal const string ReplyActionPropertyName = "ReplyAction";
  72. public string ReplyAction
  73. {
  74. get { return this.replyAction; }
  75. set
  76. {
  77. if (value == null)
  78. {
  79. throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull("value");
  80. }
  81. this.replyAction = value;
  82. }
  83. }
  84. public bool AsyncPattern
  85. {
  86. get { return this.asyncPattern; }
  87. set { this.asyncPattern = value; }
  88. }
  89. public bool IsOneWay
  90. {
  91. get { return this.isOneWay; }
  92. set { this.isOneWay = value; }
  93. }
  94. public bool IsInitiating
  95. {
  96. get { return this.isInitiating; }
  97. set { this.isInitiating = value; }
  98. }
  99. public bool IsTerminating
  100. {
  101. get { return this.isTerminating; }
  102. set { this.isTerminating = value; }
  103. }
  104. internal bool IsSessionOpenNotificationEnabled
  105. {
  106. get
  107. {
  108. return this.Action == OperationDescription.SessionOpenedAction;
  109. }
  110. }
  111. internal void EnsureInvariants(MethodInfo methodInfo, string operationName)
  112. {
  113. if (this.IsSessionOpenNotificationEnabled)
  114. {
  115. if (!this.IsOneWay
  116. || !this.IsInitiating
  117. || methodInfo.GetParameters().Length > 0)
  118. {
  119. throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException(
  120. SR.GetString(SR.ContractIsNotSelfConsistentWhenIsSessionOpenNotificationEnabled, operationName, "Action", OperationDescription.SessionOpenedAction, "IsOneWay", "IsInitiating")));
  121. }
  122. }
  123. }
  124. }
  125. }