ComPlusServiceLoader.cs 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. //-----------------------------------------------------------------------------
  2. // Copyright (c) Microsoft Corporation. All rights reserved.
  3. //-----------------------------------------------------------------------------
  4. namespace System.ServiceModel.ComIntegration
  5. {
  6. using System;
  7. using System.ServiceModel.Description;
  8. using System.ServiceModel;
  9. using System.ServiceModel.Activation;
  10. using System.Diagnostics;
  11. using System.ServiceModel.Diagnostics;
  12. class ComPlusServiceLoader
  13. {
  14. ServiceInfo info;
  15. ConfigLoader configLoader;
  16. ComPlusTypeLoader typeLoader;
  17. public ComPlusServiceLoader (ServiceInfo info)
  18. {
  19. this.info = info;
  20. this.typeLoader = new ComPlusTypeLoader(info);
  21. this.configLoader = new ConfigLoader(typeLoader);
  22. }
  23. public ServiceDescription Load(ServiceHostBase host)
  24. {
  25. ServiceDescription service = new ServiceDescription(this.info.ServiceName);
  26. // ServiceBehaviorAttribute needs to go first in the behaviors collection (before config stuff)
  27. AddBehaviors(service);
  28. this.configLoader.LoadServiceDescription(host, service, this.info.ServiceElement, host.LoadConfigurationSectionHelper);
  29. ValidateConfigInstanceSettings(service);
  30. ComPlusServiceHostTrace.Trace(TraceEventType.Information, TraceCode.ComIntegrationServiceHostCreatedServiceEndpoint,
  31. SR.TraceCodeComIntegrationServiceHostCreatedServiceEndpoint, this.info, service.Endpoints);
  32. return service;
  33. }
  34. void AddBehaviors(ServiceDescription service)
  35. {
  36. // The philosophy here is to respect settings from configuration
  37. // At the moment, none of the settings we care about can be modified
  38. // through configuration. That may change in the future.
  39. // However, we never want to silently overwrite a user's configuration.
  40. // So we should either accept overrides or reject them, but never
  41. // silently update them.
  42. //
  43. ServiceBehaviorAttribute serviceBehavior = EnsureBehaviorAttribute(service);
  44. serviceBehavior.InstanceProvider = new ComPlusInstanceProvider(this.info);
  45. serviceBehavior.InstanceContextMode = InstanceContextMode.Single;
  46. // SHOULD: There is no reason to not allow concurrency at this level
  47. serviceBehavior.ConcurrencyMode = ConcurrencyMode.Multiple;
  48. serviceBehavior.UseSynchronizationContext = false;
  49. service.Behaviors.Add(new SecurityCookieModeValidator());
  50. if (AspNetEnvironment.Enabled)
  51. {
  52. AspNetCompatibilityRequirementsAttribute aspNetCompatibilityRequirements = service.Behaviors.Find<AspNetCompatibilityRequirementsAttribute>();
  53. if (aspNetCompatibilityRequirements == null)
  54. {
  55. aspNetCompatibilityRequirements = new AspNetCompatibilityRequirementsAttribute();
  56. service.Behaviors.Add(aspNetCompatibilityRequirements);
  57. }
  58. }
  59. }
  60. ServiceBehaviorAttribute EnsureBehaviorAttribute(ServiceDescription service)
  61. {
  62. ServiceBehaviorAttribute serviceBehavior;
  63. if (service.Behaviors.Contains(typeof(ServiceBehaviorAttribute)))
  64. {
  65. serviceBehavior = (ServiceBehaviorAttribute)service.Behaviors[typeof(ServiceBehaviorAttribute)];
  66. }
  67. else
  68. {
  69. serviceBehavior = new ServiceBehaviorAttribute();
  70. service.Behaviors.Insert(0, serviceBehavior);
  71. }
  72. return serviceBehavior;
  73. }
  74. void ValidateConfigInstanceSettings(ServiceDescription service)
  75. {
  76. ServiceBehaviorAttribute serviceBehavior = EnsureBehaviorAttribute(service);
  77. foreach (ServiceEndpoint endpoint in service.Endpoints)
  78. {
  79. if (endpoint != null && !endpoint.InternalIsSystemEndpoint(service))
  80. {
  81. if (endpoint.Contract.SessionMode == SessionMode.Required)
  82. {
  83. if (serviceBehavior.InstanceContextMode == InstanceContextMode.PerCall)
  84. throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(System.ServiceModel.ComIntegration.Error.InconsistentSessionRequirements());
  85. serviceBehavior.InstanceContextMode = InstanceContextMode.PerSession;
  86. }
  87. else
  88. {
  89. if (serviceBehavior.InstanceContextMode == InstanceContextMode.PerSession)
  90. throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(System.ServiceModel.ComIntegration.Error.InconsistentSessionRequirements());
  91. serviceBehavior.InstanceContextMode = InstanceContextMode.PerCall;
  92. }
  93. }
  94. }
  95. if (serviceBehavior.InstanceContextMode == InstanceContextMode.Single)
  96. serviceBehavior.InstanceContextMode = InstanceContextMode.PerSession;
  97. }
  98. }
  99. }