ServiceDescriptionImporter.cs 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. //
  2. // System.Web.Services.Description.ServiceDescriptionImporter.cs
  3. //
  4. // Author:
  5. // Tim Coleman ([email protected])
  6. // Lluis Sanchez Gual ([email protected])
  7. //
  8. // Copyright (C) Tim Coleman, 2002
  9. //
  10. //
  11. // Permission is hereby granted, free of charge, to any person obtaining
  12. // a copy of this software and associated documentation files (the
  13. // "Software"), to deal in the Software without restriction, including
  14. // without limitation the rights to use, copy, modify, merge, publish,
  15. // distribute, sublicense, and/or sell copies of the Software, and to
  16. // permit persons to whom the Software is furnished to do so, subject to
  17. // the following conditions:
  18. //
  19. // The above copyright notice and this permission notice shall be
  20. // included in all copies or substantial portions of the Software.
  21. //
  22. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  23. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  24. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  25. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  26. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  27. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  28. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  29. //
  30. using System;
  31. using System.CodeDom;
  32. using System.Web.Services;
  33. using System.Web.Services.Protocols;
  34. using System.Web.Services.Description;
  35. using System.Xml.Serialization;
  36. using System.Xml;
  37. using System.Collections;
  38. using System.Configuration;
  39. namespace System.Web.Services.Description {
  40. public class ServiceDescriptionImporter {
  41. #region Fields
  42. string protocolName;
  43. XmlSchemas schemas;
  44. ServiceDescriptionCollection serviceDescriptions;
  45. ServiceDescriptionImportStyle style;
  46. ArrayList importInfo = new ArrayList ();
  47. #endregion // Fields
  48. #region Constructors
  49. public ServiceDescriptionImporter ()
  50. {
  51. protocolName = String.Empty;
  52. schemas = new XmlSchemas ();
  53. serviceDescriptions = new ServiceDescriptionCollection ();
  54. style = ServiceDescriptionImportStyle.Client;
  55. }
  56. #endregion // Constructors
  57. #region Properties
  58. public string ProtocolName {
  59. get { return protocolName; }
  60. set { protocolName = value; }
  61. }
  62. public XmlSchemas Schemas {
  63. get { return schemas; }
  64. }
  65. public ServiceDescriptionCollection ServiceDescriptions {
  66. get { return serviceDescriptions; }
  67. }
  68. public ServiceDescriptionImportStyle Style {
  69. get { return style; }
  70. set { style = value; }
  71. }
  72. #endregion // Properties
  73. #region Methods
  74. public void AddServiceDescription (ServiceDescription serviceDescription, string appSettingUrlKey, string appSettingBaseUrl)
  75. {
  76. if (appSettingUrlKey != null && appSettingUrlKey == string.Empty && style == ServiceDescriptionImportStyle.Server)
  77. throw new InvalidOperationException ("Cannot set appSettingUrlKey if Style is Server");
  78. ImportInfo info = new ImportInfo ();
  79. info.ServiceDescription = serviceDescription;
  80. info.AppSettingUrlKey = appSettingUrlKey;
  81. info.AppSettingBaseUrl = appSettingBaseUrl;
  82. importInfo.Add (info);
  83. serviceDescriptions.Add (serviceDescription);
  84. if (serviceDescription.Types != null)
  85. schemas.Add (serviceDescription.Types.Schemas);
  86. }
  87. public ServiceDescriptionImportWarnings Import (CodeNamespace codeNamespace, CodeCompileUnit codeCompileUnit)
  88. {
  89. ProtocolImporter importer = GetImporter ();
  90. if (!importer.Import (this, codeNamespace, codeCompileUnit, importInfo))
  91. throw new Exception ("None of the supported bindings was found");
  92. return importer.Warnings;
  93. }
  94. ProtocolImporter GetImporter ()
  95. {
  96. ArrayList importers = GetSupportedImporters ();
  97. if (protocolName == null || protocolName == "") protocolName = "Soap";
  98. foreach (ProtocolImporter importer in importers) {
  99. if (importer.ProtocolName == protocolName)
  100. return importer;
  101. }
  102. throw new Exception ("Protocol " + protocolName + " not supported");
  103. }
  104. ArrayList GetSupportedImporters ()
  105. {
  106. ArrayList list = new ArrayList ();
  107. list.Add (new SoapProtocolImporter ());
  108. list.Add (new HttpGetProtocolImporter ());
  109. list.Add (new HttpPostProtocolImporter ());
  110. return list;
  111. }
  112. #endregion
  113. }
  114. internal class ImportInfo
  115. {
  116. public ServiceDescription ServiceDescription;
  117. public string AppSettingUrlKey;
  118. public string AppSettingBaseUrl;
  119. }
  120. }