ServiceDescriptionImporter.cs 8.1 KB

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