ServiceDescriptionImporter.cs 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291
  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. public static StringCollection GenerateWebReferences (
  139. WebReferenceCollection webReferences,
  140. CodeGenerationOptions options,
  141. ServiceDescriptionImportStyle style,
  142. CodeDomProvider codeGenerator)
  143. {
  144. CodeCompileUnit codeCompileUnit = new CodeCompileUnit ();
  145. return GenerateWebReferences (webReferences, options, style, codeGenerator, codeCompileUnit, false);
  146. }
  147. [MonoTODO ("verbose?")]
  148. public static StringCollection GenerateWebReferences (
  149. WebReferenceCollection webReferences,
  150. CodeGenerationOptions options,
  151. ServiceDescriptionImportStyle style,
  152. CodeDomProvider codeGenerator,
  153. CodeCompileUnit codeCompileUnit,
  154. bool verbose)
  155. {
  156. StringCollection allWarnings = new StringCollection ();
  157. ImportContext context = new ImportContext (new CodeIdentifiers(), true);
  158. foreach (WebReference reference in webReferences)
  159. {
  160. ServiceDescriptionImporter importer = new ServiceDescriptionImporter ();
  161. importer.CodeGenerator = codeGenerator;
  162. importer.CodeGenerationOptions = options;
  163. importer.Context = context;
  164. importer.Style = style;
  165. importer.ProtocolName = reference.ProtocolName;
  166. importer.AddReference (reference);
  167. reference.Warnings = importer.Import (reference.ProxyCode, codeCompileUnit);
  168. reference.SetValidationWarnings (context.Warnings);
  169. foreach (string s in context.Warnings)
  170. allWarnings.Add (s);
  171. context.Warnings.Clear ();
  172. }
  173. return allWarnings;
  174. }
  175. internal void AddReference (WebReference reference)
  176. {
  177. foreach (object doc in reference.Documents.Values)
  178. {
  179. if (doc is ServiceDescription) {
  180. ServiceDescription service = (ServiceDescription) doc;
  181. ImportInfo info = new ImportInfo (service, reference);
  182. importInfo.Add (info);
  183. serviceDescriptions.Add (service);
  184. if (service.Types != null)
  185. schemas.Add (service.Types.Schemas);
  186. }
  187. else if (doc is XmlSchema) {
  188. schemas.Add ((XmlSchema) doc);
  189. }
  190. }
  191. }
  192. #endif
  193. #endregion
  194. }
  195. internal class ImportInfo
  196. {
  197. string _appSettingUrlKey;
  198. string _appSettingBaseUrl;
  199. ServiceDescription _serviceDescription;
  200. public WebReference _reference;
  201. public ImportInfo (ServiceDescription serviceDescription, string appSettingUrlKey, string appSettingBaseUrl)
  202. {
  203. _serviceDescription = serviceDescription;
  204. _appSettingUrlKey = appSettingUrlKey;
  205. _appSettingBaseUrl = appSettingBaseUrl;
  206. }
  207. public ImportInfo (ServiceDescription serviceDescription, WebReference reference)
  208. {
  209. _reference = reference;
  210. _serviceDescription = serviceDescription;
  211. }
  212. public WebReference Reference {
  213. get { return _reference; }
  214. }
  215. public ServiceDescription ServiceDescription {
  216. get { return _serviceDescription; }
  217. }
  218. public string AppSettingUrlKey {
  219. get {
  220. if (_reference != null) return _reference.AppSettingUrlKey;
  221. else return _appSettingUrlKey;
  222. }
  223. set {
  224. _appSettingUrlKey = value;
  225. }
  226. }
  227. public string AppSettingBaseUrl {
  228. get {
  229. if (_reference != null) return _reference.AppSettingBaseUrl;
  230. else return _appSettingBaseUrl;
  231. }
  232. set {
  233. _appSettingBaseUrl = value;
  234. }
  235. }
  236. }
  237. }