ServiceDescriptionImporter.cs 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  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.CodeDom.Compiler;
  33. using System.Web.Services;
  34. using System.Web.Services.Protocols;
  35. using System.Web.Services.Description;
  36. using System.Xml.Serialization;
  37. using System.Xml;
  38. using System.Xml.Schema;
  39. using System.Collections;
  40. using System.Collections.Specialized;
  41. using System.Configuration;
  42. namespace System.Web.Services.Description {
  43. public class ServiceDescriptionImporter {
  44. #region Fields
  45. string protocolName;
  46. XmlSchemas schemas;
  47. ServiceDescriptionCollection serviceDescriptions;
  48. ServiceDescriptionImportStyle style;
  49. #if NET_2_0
  50. CodeGenerationOptions options;
  51. CodeDomProvider codeGenerator;
  52. ImportContext context;
  53. #endif
  54. ArrayList importInfo = new ArrayList ();
  55. #endregion // Fields
  56. #region Constructors
  57. public ServiceDescriptionImporter ()
  58. {
  59. protocolName = String.Empty;
  60. schemas = new XmlSchemas ();
  61. serviceDescriptions = new ServiceDescriptionCollection ();
  62. serviceDescriptions.SetImporter (this);
  63. style = ServiceDescriptionImportStyle.Client;
  64. }
  65. #endregion // Constructors
  66. #region Properties
  67. public string ProtocolName {
  68. get { return protocolName; }
  69. set { protocolName = value; }
  70. }
  71. public XmlSchemas Schemas {
  72. get { return schemas; }
  73. }
  74. public ServiceDescriptionCollection ServiceDescriptions {
  75. get { return serviceDescriptions; }
  76. }
  77. public ServiceDescriptionImportStyle Style {
  78. get { return style; }
  79. set { style = value; }
  80. }
  81. #if NET_2_0
  82. [System.Runtime.InteropServices.ComVisible(false)]
  83. public CodeGenerationOptions CodeGenerationOptions {
  84. get { return options; }
  85. set { options = value; }
  86. }
  87. [System.Runtime.InteropServices.ComVisible(false)]
  88. public CodeDomProvider CodeGenerator {
  89. get { return codeGenerator; }
  90. set { codeGenerator = value; }
  91. }
  92. internal ImportContext Context {
  93. get { return context; }
  94. set { context = value; }
  95. }
  96. #endif
  97. #endregion // Properties
  98. #region Methods
  99. public void AddServiceDescription (ServiceDescription serviceDescription, string appSettingUrlKey, string appSettingBaseUrl)
  100. {
  101. if (appSettingUrlKey != null && appSettingUrlKey == string.Empty && style == ServiceDescriptionImportStyle.Server)
  102. throw new InvalidOperationException ("Cannot set appSettingUrlKey if Style is Server");
  103. serviceDescriptions.Add (serviceDescription, appSettingUrlKey, appSettingBaseUrl);
  104. }
  105. internal void OnServiceDescriptionAdded (ServiceDescription serviceDescription, string appSettingUrlKey, string appSettingBaseUrl)
  106. {
  107. ImportInfo info = new ImportInfo (serviceDescription, appSettingUrlKey, appSettingBaseUrl);
  108. importInfo.Add (info);
  109. if (serviceDescription.Types != null)
  110. schemas.Add (serviceDescription.Types.Schemas);
  111. }
  112. public ServiceDescriptionImportWarnings Import (CodeNamespace codeNamespace, CodeCompileUnit codeCompileUnit)
  113. {
  114. ProtocolImporter importer = GetImporter ();
  115. if (!importer.Import (this, codeNamespace, codeCompileUnit, importInfo))
  116. throw new Exception ("None of the supported bindings was found");
  117. return importer.Warnings;
  118. }
  119. ProtocolImporter GetImporter ()
  120. {
  121. ArrayList importers = GetSupportedImporters ();
  122. if (protocolName == null || protocolName == "") protocolName = "Soap";
  123. foreach (ProtocolImporter importer in importers) {
  124. if (importer.ProtocolName == protocolName)
  125. return importer;
  126. }
  127. throw new Exception ("Protocol " + protocolName + " not supported");
  128. }
  129. ArrayList GetSupportedImporters ()
  130. {
  131. ArrayList list = new ArrayList ();
  132. list.Add (new SoapProtocolImporter ());
  133. list.Add (new HttpGetProtocolImporter ());
  134. list.Add (new HttpPostProtocolImporter ());
  135. return list;
  136. }
  137. #if NET_2_0
  138. [MonoTODO] // where to use Verbose and Extensions in options?
  139. public static StringCollection GenerateWebReferences (
  140. WebReferenceCollection webReferences,
  141. CodeDomProvider codeGenerator,
  142. CodeCompileUnit codeCompileUnit,
  143. WebReferenceOptions options)
  144. {
  145. StringCollection allWarnings = new StringCollection ();
  146. ImportContext context = new ImportContext (new CodeIdentifiers(), true);
  147. foreach (WebReference reference in webReferences)
  148. {
  149. ServiceDescriptionImporter importer = new ServiceDescriptionImporter ();
  150. importer.CodeGenerator = codeGenerator;
  151. importer.CodeGenerationOptions = options.CodeGenerationOptions;
  152. importer.Context = context;
  153. importer.Style = options.Style;
  154. importer.ProtocolName = reference.ProtocolName;
  155. importer.AddReference (reference);
  156. reference.Warnings = importer.Import (reference.ProxyCode, codeCompileUnit);
  157. reference.SetValidationWarnings (context.Warnings);
  158. foreach (string s in context.Warnings)
  159. allWarnings.Add (s);
  160. context.Warnings.Clear ();
  161. }
  162. return allWarnings;
  163. }
  164. internal void AddReference (WebReference reference)
  165. {
  166. foreach (object doc in reference.Documents.Values)
  167. {
  168. if (doc is ServiceDescription) {
  169. ServiceDescription service = (ServiceDescription) doc;
  170. ImportInfo info = new ImportInfo (service, reference);
  171. importInfo.Add (info);
  172. serviceDescriptions.Add (service);
  173. if (service.Types != null)
  174. schemas.Add (service.Types.Schemas);
  175. }
  176. else if (doc is XmlSchema) {
  177. schemas.Add ((XmlSchema) doc);
  178. }
  179. }
  180. }
  181. #endif
  182. #endregion
  183. }
  184. internal class ImportInfo
  185. {
  186. string _appSettingUrlKey;
  187. string _appSettingBaseUrl;
  188. ServiceDescription _serviceDescription;
  189. public WebReference _reference;
  190. public ImportInfo (ServiceDescription serviceDescription, string appSettingUrlKey, string appSettingBaseUrl)
  191. {
  192. _serviceDescription = serviceDescription;
  193. _appSettingUrlKey = appSettingUrlKey;
  194. _appSettingBaseUrl = appSettingBaseUrl;
  195. }
  196. public ImportInfo (ServiceDescription serviceDescription, WebReference reference)
  197. {
  198. _reference = reference;
  199. _serviceDescription = serviceDescription;
  200. }
  201. public WebReference Reference {
  202. get { return _reference; }
  203. }
  204. public ServiceDescription ServiceDescription {
  205. get { return _serviceDescription; }
  206. }
  207. public string AppSettingUrlKey {
  208. get {
  209. if (_reference != null) return _reference.AppSettingUrlKey;
  210. else return _appSettingUrlKey;
  211. }
  212. set {
  213. _appSettingUrlKey = value;
  214. }
  215. }
  216. public string AppSettingBaseUrl {
  217. get {
  218. if (_reference != null) return _reference.AppSettingBaseUrl;
  219. else return _appSettingBaseUrl;
  220. }
  221. set {
  222. _appSettingBaseUrl = value;
  223. }
  224. }
  225. }
  226. }