ContractDescription.cs 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  1. //------------------------------------------------------------
  2. // Copyright (c) Microsoft Corporation. All rights reserved.
  3. //------------------------------------------------------------
  4. namespace System.ServiceModel.Description
  5. {
  6. using System;
  7. using System.Collections.Generic;
  8. using System.Collections.ObjectModel;
  9. using System.ComponentModel;
  10. using System.Diagnostics;
  11. using System.Net.Security;
  12. using System.ServiceModel.Security;
  13. [DebuggerDisplay("Name={name}, Namespace={ns}, ContractType={contractType}")]
  14. public class ContractDescription
  15. {
  16. Type callbackContractType;
  17. string configurationName;
  18. Type contractType;
  19. XmlName name;
  20. string ns;
  21. OperationDescriptionCollection operations;
  22. SessionMode sessionMode;
  23. KeyedByTypeCollection<IContractBehavior> behaviors = new KeyedByTypeCollection<IContractBehavior>();
  24. ProtectionLevel protectionLevel;
  25. bool hasProtectionLevel;
  26. public ContractDescription(string name)
  27. : this(name, null)
  28. {
  29. }
  30. public ContractDescription(string name, string ns)
  31. {
  32. // the property setter validates given value
  33. this.Name = name;
  34. if (!string.IsNullOrEmpty(ns))
  35. NamingHelper.CheckUriParameter(ns, "ns");
  36. this.operations = new OperationDescriptionCollection();
  37. this.ns = ns ?? NamingHelper.DefaultNamespace; // ns can be ""
  38. }
  39. internal string CodeName
  40. {
  41. get { return this.name.DecodedName; }
  42. }
  43. [DefaultValue(null)]
  44. public string ConfigurationName
  45. {
  46. get { return this.configurationName; }
  47. set { this.configurationName = value; }
  48. }
  49. public Type ContractType
  50. {
  51. get { return this.contractType; }
  52. set { this.contractType = value; }
  53. }
  54. public Type CallbackContractType
  55. {
  56. get { return this.callbackContractType; }
  57. set { this.callbackContractType = value; }
  58. }
  59. public string Name
  60. {
  61. get { return this.name.EncodedName; }
  62. set
  63. {
  64. if (value == null)
  65. {
  66. throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull("value");
  67. }
  68. if (value.Length == 0)
  69. {
  70. throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(
  71. new ArgumentOutOfRangeException("value", SR.GetString(SR.SFxContractDescriptionNameCannotBeEmpty)));
  72. }
  73. this.name = new XmlName(value, true /*isEncoded*/);
  74. }
  75. }
  76. public string Namespace
  77. {
  78. get { return this.ns; }
  79. set
  80. {
  81. if (!string.IsNullOrEmpty(value))
  82. NamingHelper.CheckUriProperty(value, "Namespace");
  83. this.ns = value;
  84. }
  85. }
  86. public OperationDescriptionCollection Operations
  87. {
  88. get { return this.operations; }
  89. }
  90. public ProtectionLevel ProtectionLevel
  91. {
  92. get { return this.protectionLevel; }
  93. set
  94. {
  95. if (!ProtectionLevelHelper.IsDefined(value))
  96. throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ArgumentOutOfRangeException("value"));
  97. this.protectionLevel = value;
  98. this.hasProtectionLevel = true;
  99. }
  100. }
  101. public bool ShouldSerializeProtectionLevel()
  102. {
  103. return this.HasProtectionLevel;
  104. }
  105. public bool HasProtectionLevel
  106. {
  107. get { return this.hasProtectionLevel; }
  108. }
  109. [DefaultValue(SessionMode.Allowed)]
  110. public SessionMode SessionMode
  111. {
  112. get { return this.sessionMode; }
  113. set
  114. {
  115. if (!SessionModeHelper.IsDefined(value))
  116. {
  117. throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ArgumentOutOfRangeException("value"));
  118. }
  119. this.sessionMode = value;
  120. }
  121. }
  122. public KeyedCollection<Type, IContractBehavior> ContractBehaviors
  123. {
  124. get { return this.Behaviors; }
  125. }
  126. [EditorBrowsable(EditorBrowsableState.Never)]
  127. public KeyedByTypeCollection<IContractBehavior> Behaviors
  128. {
  129. get { return this.behaviors; }
  130. }
  131. public static ContractDescription GetContract(Type contractType)
  132. {
  133. if (contractType == null)
  134. throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull("contractType");
  135. TypeLoader typeLoader = new TypeLoader();
  136. return typeLoader.LoadContractDescription(contractType);
  137. }
  138. public static ContractDescription GetContract(Type contractType, Type serviceType)
  139. {
  140. if (contractType == null)
  141. throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull("contractType");
  142. if (serviceType == null)
  143. throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull("serviceType");
  144. TypeLoader typeLoader = new TypeLoader();
  145. ContractDescription description = typeLoader.LoadContractDescription(contractType, serviceType);
  146. return description;
  147. }
  148. public static ContractDescription GetContract(Type contractType, object serviceImplementation)
  149. {
  150. if (contractType == null)
  151. throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull("contractType");
  152. if (serviceImplementation == null)
  153. throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull("serviceImplementation");
  154. TypeLoader typeLoader = new TypeLoader();
  155. Type serviceType = serviceImplementation.GetType();
  156. ContractDescription description = typeLoader.LoadContractDescription(contractType, serviceType, serviceImplementation);
  157. return description;
  158. }
  159. public Collection<ContractDescription> GetInheritedContracts()
  160. {
  161. Collection<ContractDescription> result = new Collection<ContractDescription>();
  162. for (int i = 0; i < Operations.Count; i++)
  163. {
  164. OperationDescription od = Operations[i];
  165. if (od.DeclaringContract != this)
  166. {
  167. ContractDescription inheritedContract = od.DeclaringContract;
  168. if (!result.Contains(inheritedContract))
  169. {
  170. result.Add(inheritedContract);
  171. }
  172. }
  173. }
  174. return result;
  175. }
  176. internal void EnsureInvariants()
  177. {
  178. if (string.IsNullOrEmpty(this.Name))
  179. {
  180. throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException(
  181. SR.GetString(SR.AChannelServiceEndpointSContractSNameIsNull0)));
  182. }
  183. if (this.Namespace == null)
  184. {
  185. throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException(
  186. SR.GetString(SR.AChannelServiceEndpointSContractSNamespace0)));
  187. }
  188. if (this.Operations.Count == 0)
  189. {
  190. throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException(
  191. SR.GetString(SR.SFxContractHasZeroOperations, this.Name)));
  192. }
  193. bool thereIsAtLeastOneInitiatingOperation = false;
  194. for (int i = 0; i < this.Operations.Count; i++)
  195. {
  196. OperationDescription operationDescription = this.Operations[i];
  197. operationDescription.EnsureInvariants();
  198. if (operationDescription.IsInitiating)
  199. thereIsAtLeastOneInitiatingOperation = true;
  200. if ((!operationDescription.IsInitiating || operationDescription.IsTerminating)
  201. && (this.SessionMode != SessionMode.Required))
  202. {
  203. throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException(
  204. SR.GetString(SR.ContractIsNotSelfConsistentItHasOneOrMore2, this.Name)));
  205. }
  206. }
  207. if (!thereIsAtLeastOneInitiatingOperation)
  208. {
  209. throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException(
  210. SR.GetString(SR.SFxContractHasZeroInitiatingOperations, this.Name)));
  211. }
  212. }
  213. internal bool IsDuplex()
  214. {
  215. for (int i = 0; i < this.operations.Count; ++i)
  216. {
  217. if (this.operations[i].IsServerInitiated())
  218. {
  219. return true;
  220. }
  221. }
  222. return false;
  223. }
  224. }
  225. }