WebHostedComPlusServiceHost.cs 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. //-----------------------------------------------------------------------------
  2. // Copyright (c) Microsoft Corporation. All rights reserved.
  3. //-----------------------------------------------------------------------------
  4. namespace System.ServiceModel.ComIntegration
  5. {
  6. using System;
  7. using System.Collections.Generic;
  8. using System.Runtime.InteropServices;
  9. using System.ServiceModel;
  10. using System.ServiceModel.Configuration;
  11. using System.ServiceModel.Diagnostics;
  12. class WebHostedComPlusServiceHost : ComPlusServiceHost
  13. {
  14. public WebHostedComPlusServiceHost(string webhostParams, Uri[] baseAddresses)
  15. {
  16. foreach (Uri address in baseAddresses)
  17. this.InternalBaseAddresses.Add(address);
  18. // Split up the parameter string into "clsid,appid".
  19. //
  20. string[] parameters = webhostParams.Split(',');
  21. if (parameters.Length != 2)
  22. {
  23. throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(System.ServiceModel.ComIntegration.Error.ListenerInitFailed(
  24. SR.GetString(SR.ServiceStringFormatError,
  25. webhostParams)));
  26. }
  27. Guid clsid;
  28. Guid appId;
  29. if (!DiagnosticUtility.Utility.TryCreateGuid(parameters[0], out clsid))
  30. {
  31. throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(System.ServiceModel.ComIntegration.Error.ListenerInitFailed(
  32. SR.GetString(SR.ServiceStringFormatError,
  33. webhostParams)));
  34. }
  35. if (!DiagnosticUtility.Utility.TryCreateGuid(parameters[1], out appId))
  36. {
  37. throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(System.ServiceModel.ComIntegration.Error.ListenerInitFailed(
  38. SR.GetString(SR.ServiceStringFormatError,
  39. webhostParams)));
  40. }
  41. // "B" == "With dashes and curly braces"
  42. // (The catalog gives us GUIDs in this format)
  43. //
  44. string clsidString = clsid.ToString("B").ToUpperInvariant();
  45. // Look up the COM+ AdminSDK information for this
  46. // AppID/CLSID pair.
  47. //
  48. ComCatalogObject application;
  49. application = CatalogUtil.FindApplication(appId);
  50. if (application == null)
  51. {
  52. throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(System.ServiceModel.ComIntegration.Error.ListenerInitFailed(
  53. SR.GetString(SR.ApplicationNotFound,
  54. appId.ToString("B").ToUpperInvariant())));
  55. }
  56. ComCatalogCollection classes;
  57. classes = application.GetCollection("Components");
  58. ComCatalogObject classObject = null;
  59. foreach (ComCatalogObject tempClassObject in classes)
  60. {
  61. string otherClsid = (string)tempClassObject.GetValue("CLSID");
  62. if (clsidString.Equals(
  63. otherClsid,
  64. StringComparison.OrdinalIgnoreCase))
  65. {
  66. classObject = tempClassObject;
  67. break;
  68. }
  69. }
  70. if (classObject == null)
  71. {
  72. throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(System.ServiceModel.ComIntegration.Error.ListenerInitFailed(
  73. SR.GetString(SR.ClsidNotInApplication,
  74. clsidString,
  75. appId.ToString("B").ToUpperInvariant())));
  76. }
  77. // Load up Indigo configuration, get the configuration for
  78. // this service.
  79. //
  80. ServicesSection services = ServicesSection.GetSection();
  81. ServiceElement service = null;
  82. foreach (ServiceElement serviceInConfig in services.Services)
  83. {
  84. Guid clsidFromConfig = Guid.Empty;
  85. Guid appidFromConfig = Guid.Empty;
  86. string[] serviceParams = serviceInConfig.Name.Split(',');
  87. if (serviceParams.Length != 2)
  88. {
  89. continue;
  90. }
  91. if (!DiagnosticUtility.Utility.TryCreateGuid(serviceParams[0], out appidFromConfig))
  92. {
  93. // We are tolerant of having non COM+ based services
  94. // for webhost.
  95. continue;
  96. }
  97. if (!DiagnosticUtility.Utility.TryCreateGuid(serviceParams[1], out clsidFromConfig))
  98. {
  99. // We are tolerant of having non COM+ based services
  100. // for webhost.
  101. continue;
  102. }
  103. if (clsidFromConfig == clsid && appidFromConfig == appId)
  104. {
  105. service = serviceInConfig;
  106. break;
  107. }
  108. }
  109. if (service == null)
  110. {
  111. throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(System.ServiceModel.ComIntegration.Error.ListenerInitFailed(
  112. SR.GetString(SR.ClsidNotInConfiguration,
  113. clsidString)));
  114. }
  115. // Hosting mode evaluation
  116. //
  117. HostingMode hostingMode;
  118. int activation = (int)application.GetValue("Activation");
  119. if (activation == 0)
  120. {
  121. hostingMode = HostingMode.WebHostInProcess;
  122. }
  123. else
  124. {
  125. hostingMode = HostingMode.WebHostOutOfProcess;
  126. }
  127. // Now we have everything we need, do common
  128. // initialization.
  129. //
  130. Initialize(clsid,
  131. service,
  132. application,
  133. classObject,
  134. hostingMode);
  135. }
  136. }
  137. }