ServiceDescription.cs 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  1. //------------------------------------------------------------
  2. // Copyright (c) Microsoft Corporation. All rights reserved.
  3. //------------------------------------------------------------
  4. namespace System.ServiceModel.Description
  5. {
  6. using System.Collections.Generic;
  7. using System.ServiceModel;
  8. using System.ServiceModel.Activation;
  9. using System.Collections.ObjectModel;
  10. using System.Runtime;
  11. using System.Runtime.Serialization;
  12. using System.Reflection;
  13. using System.Diagnostics;
  14. using System.Security;
  15. using System.Security.Permissions;
  16. [DebuggerDisplay("ServiceType={serviceType}")]
  17. public class ServiceDescription
  18. {
  19. KeyedByTypeCollection<IServiceBehavior> behaviors = new KeyedByTypeCollection<IServiceBehavior>();
  20. string configurationName;
  21. ServiceEndpointCollection endpoints = new ServiceEndpointCollection();
  22. Type serviceType;
  23. XmlName serviceName;
  24. string serviceNamespace = NamingHelper.DefaultNamespace;
  25. public ServiceDescription()
  26. {
  27. }
  28. internal ServiceDescription(String serviceName)
  29. {
  30. if (String.IsNullOrEmpty(serviceName))
  31. throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull("serviceName");
  32. this.Name = serviceName;
  33. }
  34. public ServiceDescription(IEnumerable<ServiceEndpoint> endpoints)
  35. : this()
  36. {
  37. if (endpoints == null)
  38. throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull("endpoints");
  39. foreach (ServiceEndpoint endpoint in endpoints)
  40. this.endpoints.Add(endpoint);
  41. }
  42. public string Name
  43. {
  44. get
  45. {
  46. if (serviceName != null)
  47. return serviceName.EncodedName;
  48. else if (ServiceType != null)
  49. return NamingHelper.XmlName(ServiceType.Name);
  50. else
  51. return NamingHelper.DefaultServiceName;
  52. }
  53. set
  54. {
  55. if (string.IsNullOrEmpty(value))
  56. {
  57. serviceName = null;
  58. }
  59. else
  60. {
  61. // the XmlName ctor validate the value
  62. serviceName = new XmlName(value, true /*isEncoded*/);
  63. }
  64. }
  65. }
  66. public string Namespace
  67. {
  68. get
  69. {
  70. return serviceNamespace;
  71. }
  72. set
  73. {
  74. serviceNamespace = value;
  75. }
  76. }
  77. public KeyedByTypeCollection<IServiceBehavior> Behaviors
  78. {
  79. get { return this.behaviors; }
  80. }
  81. public string ConfigurationName
  82. {
  83. get { return this.configurationName; }
  84. set
  85. {
  86. if (value == null)
  87. {
  88. throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull("value");
  89. }
  90. this.configurationName = value;
  91. }
  92. }
  93. public ServiceEndpointCollection Endpoints
  94. {
  95. get { return this.endpoints; }
  96. }
  97. public Type ServiceType
  98. {
  99. get { return this.serviceType; }
  100. set { this.serviceType = value; }
  101. }
  102. static void AddBehaviors(ServiceDescription serviceDescription)
  103. {
  104. Type type = serviceDescription.ServiceType;
  105. System.ServiceModel.Description.TypeLoader.ApplyServiceInheritance<IServiceBehavior, KeyedByTypeCollection<IServiceBehavior>>(
  106. type, serviceDescription.Behaviors, ServiceDescription.GetIServiceBehaviorAttributes);
  107. ServiceBehaviorAttribute serviceBehavior = EnsureBehaviorAttribute(serviceDescription);
  108. if (serviceBehavior.Name != null)
  109. serviceDescription.Name = new XmlName(serviceBehavior.Name).EncodedName;
  110. if (serviceBehavior.Namespace != null)
  111. serviceDescription.Namespace = serviceBehavior.Namespace;
  112. if (String.IsNullOrEmpty(serviceBehavior.ConfigurationName))
  113. {
  114. serviceDescription.ConfigurationName = type.FullName;
  115. }
  116. else
  117. {
  118. serviceDescription.ConfigurationName = serviceBehavior.ConfigurationName;
  119. }
  120. AspNetEnvironment.Current.EnsureCompatibilityRequirements(serviceDescription);
  121. }
  122. internal static object CreateImplementation(Type serviceType)
  123. {
  124. ConstructorInfo constructor = serviceType.GetConstructor(
  125. TypeLoader.DefaultBindingFlags, null, Type.EmptyTypes, null);
  126. if (constructor == null)
  127. {
  128. throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException(
  129. SR.GetString(SR.SFxNoDefaultConstructor)));
  130. }
  131. // Stop the partially trusted callers to use the ServiceDescription.GetService(Type) method to
  132. // instantiate types in this assembly that are not public or have a non-public default constructor.
  133. if ((!PartialTrustHelpers.AppDomainFullyTrusted) &&
  134. (serviceType.IsNotPublic || (!constructor.IsPublic)) &&
  135. (serviceType.Assembly == typeof(ServiceDescription).Assembly))
  136. {
  137. PartialTrustHelpers.DemandForFullTrust();
  138. }
  139. try
  140. {
  141. object implementation = constructor.Invoke(
  142. TypeLoader.DefaultBindingFlags, null, null, System.Globalization.CultureInfo.InvariantCulture);
  143. return implementation;
  144. }
  145. catch (MethodAccessException methodAccessException)
  146. {
  147. SecurityException securityException = methodAccessException.InnerException as SecurityException;
  148. if (securityException != null && securityException.PermissionType.Equals(typeof(ReflectionPermission)))
  149. {
  150. DiagnosticUtility.TraceHandledException(methodAccessException, TraceEventType.Warning);
  151. throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(
  152. new SecurityException(SR.GetString(
  153. SR.PartialTrustServiceCtorNotVisible,
  154. serviceType.FullName)));
  155. }
  156. else
  157. {
  158. throw;
  159. }
  160. }
  161. }
  162. static ServiceBehaviorAttribute EnsureBehaviorAttribute(ServiceDescription description)
  163. {
  164. ServiceBehaviorAttribute attr = description.Behaviors.Find<ServiceBehaviorAttribute>();
  165. if (attr == null)
  166. {
  167. attr = new ServiceBehaviorAttribute();
  168. description.Behaviors.Insert(0, attr);
  169. }
  170. return attr;
  171. }
  172. // This method ensures that the description object graph is structurally sound and that none
  173. // of the fundamental SFx framework assumptions have been violated.
  174. internal void EnsureInvariants()
  175. {
  176. for (int i = 0; i < this.Endpoints.Count; i++)
  177. {
  178. ServiceEndpoint endpoint = this.Endpoints[i];
  179. if (endpoint == null)
  180. {
  181. throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException(SR.GetString(SR.AChannelServiceEndpointIsNull0)));
  182. }
  183. endpoint.EnsureInvariants();
  184. }
  185. }
  186. static void GetIServiceBehaviorAttributes(Type currentServiceType, KeyedByTypeCollection<IServiceBehavior> behaviors)
  187. {
  188. foreach (IServiceBehavior behaviorAttribute in ServiceReflector.GetCustomAttributes(currentServiceType, typeof(IServiceBehavior)))
  189. {
  190. behaviors.Add(behaviorAttribute);
  191. }
  192. }
  193. public static ServiceDescription GetService(Type serviceType)
  194. {
  195. if (serviceType == null)
  196. {
  197. throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull("serviceType");
  198. }
  199. if (!serviceType.IsClass)
  200. {
  201. throw new ArgumentException(SR.GetString(SR.SFxServiceHostNeedsClass));
  202. }
  203. ServiceDescription description = new ServiceDescription();
  204. description.ServiceType = serviceType;
  205. AddBehaviors(description);
  206. SetupSingleton(description, null, false);
  207. return description;
  208. }
  209. public static ServiceDescription GetService(object serviceImplementation)
  210. {
  211. if (serviceImplementation == null)
  212. {
  213. throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull("serviceImplementation");
  214. }
  215. Type serviceType = serviceImplementation.GetType();
  216. ServiceDescription description = new ServiceDescription();
  217. description.ServiceType = serviceType;
  218. if (serviceImplementation is IServiceBehavior)
  219. {
  220. description.Behaviors.Add((IServiceBehavior)serviceImplementation);
  221. }
  222. AddBehaviors(description);
  223. SetupSingleton(description, serviceImplementation, true);
  224. return description;
  225. }
  226. static void SetupSingleton(ServiceDescription serviceDescription, object implementation, bool isWellKnown)
  227. {
  228. ServiceBehaviorAttribute serviceBehavior = EnsureBehaviorAttribute(serviceDescription);
  229. Type type = serviceDescription.ServiceType;
  230. if ((implementation == null) && (serviceBehavior.InstanceContextMode == InstanceContextMode.Single))
  231. {
  232. implementation = CreateImplementation(type);
  233. }
  234. if (isWellKnown)
  235. {
  236. serviceBehavior.SetWellKnownSingleton(implementation);
  237. }
  238. else if ((implementation != null) && (serviceBehavior.InstanceContextMode == InstanceContextMode.Single))
  239. {
  240. serviceBehavior.SetHiddenSingleton(implementation);
  241. }
  242. }
  243. }
  244. }