ServiceDescriptionImporter.cs 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  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. using System;
  11. using System.CodeDom;
  12. using System.Web.Services;
  13. using System.Web.Services.Protocols;
  14. using System.Web.Services.Description;
  15. using System.Xml.Serialization;
  16. using System.Xml;
  17. using System.Collections;
  18. using System.Configuration;
  19. namespace System.Web.Services.Description {
  20. public class ServiceDescriptionImporter {
  21. #region Fields
  22. string protocolName;
  23. XmlSchemas schemas;
  24. ServiceDescriptionCollection serviceDescriptions;
  25. ServiceDescriptionImportStyle style;
  26. ArrayList importInfo = new ArrayList ();
  27. #endregion // Fields
  28. #region Constructors
  29. public ServiceDescriptionImporter ()
  30. {
  31. protocolName = String.Empty;
  32. schemas = new XmlSchemas ();
  33. serviceDescriptions = new ServiceDescriptionCollection ();
  34. style = ServiceDescriptionImportStyle.Client;
  35. }
  36. #endregion // Constructors
  37. #region Properties
  38. public string ProtocolName {
  39. get { return protocolName; }
  40. set { protocolName = value; }
  41. }
  42. public XmlSchemas Schemas {
  43. get { return schemas; }
  44. }
  45. public ServiceDescriptionCollection ServiceDescriptions {
  46. get { return serviceDescriptions; }
  47. }
  48. public ServiceDescriptionImportStyle Style {
  49. get { return style; }
  50. set { style = value; }
  51. }
  52. #endregion // Properties
  53. #region Methods
  54. public void AddServiceDescription (ServiceDescription serviceDescription, string appSettingUrlKey, string appSettingBaseUrl)
  55. {
  56. if (appSettingUrlKey != null && appSettingUrlKey == string.Empty && style == ServiceDescriptionImportStyle.Server)
  57. throw new InvalidOperationException ("Cannot set appSettingUrlKey if Style is Server");
  58. ImportInfo info = new ImportInfo ();
  59. info.ServiceDescription = serviceDescription;
  60. info.AppSettingUrlKey = appSettingUrlKey;
  61. info.AppSettingBaseUrl = appSettingBaseUrl;
  62. importInfo.Add (info);
  63. serviceDescriptions.Add (serviceDescription);
  64. if (serviceDescription.Types != null)
  65. schemas.Add (serviceDescription.Types.Schemas);
  66. }
  67. public ServiceDescriptionImportWarnings Import (CodeNamespace codeNamespace, CodeCompileUnit codeCompileUnit)
  68. {
  69. ServiceDescriptionImportWarnings warns = 0;
  70. ProtocolImporter importer = GetImporter ();
  71. if (!importer.Import (this, codeNamespace, codeCompileUnit, importInfo))
  72. throw new Exception ("None of the supported bindings was found");
  73. return importer.Warnings;
  74. }
  75. ProtocolImporter GetImporter ()
  76. {
  77. ArrayList importers = GetSupportedImporters ();
  78. if (protocolName == null || protocolName == "") protocolName = "Soap";
  79. foreach (ProtocolImporter importer in importers) {
  80. if (importer.ProtocolName == protocolName)
  81. return importer;
  82. }
  83. throw new Exception ("Protocol " + protocolName + " not supported");
  84. }
  85. ArrayList GetSupportedImporters ()
  86. {
  87. ArrayList list = new ArrayList ();
  88. list.Add (new SoapProtocolImporter ());
  89. list.Add (new HttpGetProtocolImporter ());
  90. list.Add (new HttpPostProtocolImporter ());
  91. return list;
  92. }
  93. #endregion
  94. }
  95. internal class ImportInfo
  96. {
  97. public ServiceDescription ServiceDescription;
  98. public string AppSettingUrlKey;
  99. public string AppSettingBaseUrl;
  100. }
  101. }