2 // System.Web.Services.Description.ServiceDescriptionImporter.cs
\r
5 // Tim Coleman (tim@timcoleman.com)
\r
6 // Lluis Sanchez Gual (lluis@ximian.com)
\r
8 // Copyright (C) Tim Coleman, 2002
\r
12 // Permission is hereby granted, free of charge, to any person obtaining
13 // a copy of this software and associated documentation files (the
14 // "Software"), to deal in the Software without restriction, including
15 // without limitation the rights to use, copy, modify, merge, publish,
16 // distribute, sublicense, and/or sell copies of the Software, and to
17 // permit persons to whom the Software is furnished to do so, subject to
18 // the following conditions:
20 // The above copyright notice and this permission notice shall be
21 // included in all copies or substantial portions of the Software.
23 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
24 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
25 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
26 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
27 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
28 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
29 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
33 using System.CodeDom;
\r
34 using System.CodeDom.Compiler;
\r
35 using System.Web.Services;
\r
36 using System.Web.Services.Protocols;
\r
37 using System.Web.Services.Description;
\r
38 using System.Xml.Serialization;
\r
40 using System.Xml.Schema;
\r
41 using System.Collections;
\r
42 using System.Collections.Specialized;
\r
43 using System.Configuration;
\r
45 namespace System.Web.Services.Description {
\r
46 public class ServiceDescriptionImporter {
\r
50 string protocolName;
\r
52 ServiceDescriptionCollection serviceDescriptions;
\r
53 ServiceDescriptionImportStyle style;
\r
56 CodeGenerationOptions options;
\r
57 CodeDomProvider codeGenerator;
\r
58 ImportContext context;
\r
61 ArrayList importInfo = new ArrayList ();
\r
64 #endregion // Fields
\r
66 #region Constructors
\r
68 public ServiceDescriptionImporter ()
\r
70 protocolName = String.Empty;
\r
71 schemas = new XmlSchemas ();
\r
72 serviceDescriptions = new ServiceDescriptionCollection ();
73 serviceDescriptions.SetImporter (this);
\r
74 style = ServiceDescriptionImportStyle.Client;
\r
77 #endregion // Constructors
\r
81 public string ProtocolName {
\r
82 get { return protocolName; }
\r
83 set { protocolName = value; }
\r
86 public XmlSchemas Schemas {
\r
87 get { return schemas; }
\r
90 public ServiceDescriptionCollection ServiceDescriptions {
\r
91 get { return serviceDescriptions; }
\r
94 public ServiceDescriptionImportStyle Style {
\r
95 get { return style; }
\r
96 set { style = value; }
\r
100 [System.Runtime.InteropServices.ComVisible(false)]
\r
101 public CodeGenerationOptions CodeGenerationOptions {
\r
102 get { return options; }
\r
103 set { options = value; }
\r
106 [System.Runtime.InteropServices.ComVisible(false)]
\r
107 public CodeDomProvider CodeGenerator {
\r
108 get { return codeGenerator; }
\r
109 set { codeGenerator = value; }
\r
113 internal ImportContext Context {
\r
114 get { return context; }
\r
115 set { context = value; }
\r
119 #endregion // Properties
\r
123 public void AddServiceDescription (ServiceDescription serviceDescription, string appSettingUrlKey, string appSettingBaseUrl)
\r
125 if (appSettingUrlKey != null && appSettingUrlKey == string.Empty && style == ServiceDescriptionImportStyle.Server)
\r
126 throw new InvalidOperationException ("Cannot set appSettingUrlKey if Style is Server");
\r
128 serviceDescriptions.Add (serviceDescription, appSettingUrlKey, appSettingBaseUrl);
\r
131 internal void OnServiceDescriptionAdded (ServiceDescription serviceDescription, string appSettingUrlKey, string appSettingBaseUrl)
\r
133 ImportInfo info = new ImportInfo (serviceDescription, appSettingUrlKey, appSettingBaseUrl);
\r
134 importInfo.Add (info);
\r
136 if (serviceDescription.Types != null)
\r
137 schemas.Add (serviceDescription.Types.Schemas);
\r
140 public ServiceDescriptionImportWarnings Import (CodeNamespace codeNamespace, CodeCompileUnit codeCompileUnit)
\r
142 ProtocolImporter importer = GetImporter ();
\r
144 if (!importer.Import (this, codeNamespace, codeCompileUnit, importInfo))
\r
145 throw new Exception ("None of the supported bindings was found");
\r
147 return importer.Warnings;
\r
150 ProtocolImporter GetImporter ()
\r
152 ArrayList importers = GetSupportedImporters ();
\r
153 if (protocolName == null || protocolName == "") protocolName = "Soap";
\r
154 foreach (ProtocolImporter importer in importers) {
\r
155 if (importer.ProtocolName == protocolName)
\r
159 throw new Exception ("Protocol " + protocolName + " not supported");
\r
162 ArrayList GetSupportedImporters ()
\r
164 ArrayList list = new ArrayList ();
\r
165 list.Add (new SoapProtocolImporter ());
\r
166 list.Add (new HttpGetProtocolImporter ());
\r
167 list.Add (new HttpPostProtocolImporter ());
\r
173 public static StringCollection GenerateWebReferences (
\r
174 WebReferenceCollection webReferences,
\r
175 CodeGenerationOptions options,
\r
176 ServiceDescriptionImportStyle style,
\r
177 CodeDomProvider codeGenerator)
\r
179 CodeCompileUnit codeCompileUnit = new CodeCompileUnit ();
\r
180 return GenerateWebReferences (webReferences, options, style, codeGenerator, codeCompileUnit, false);
\r
183 [MonoTODO ("verbose?")]
\r
184 public static StringCollection GenerateWebReferences (
\r
185 WebReferenceCollection webReferences,
\r
186 CodeGenerationOptions options,
\r
187 ServiceDescriptionImportStyle style,
\r
188 CodeDomProvider codeGenerator,
\r
189 CodeCompileUnit codeCompileUnit,
\r
192 StringCollection allWarnings = new StringCollection ();
\r
193 ImportContext context = new ImportContext (new CodeIdentifiers(), true);
\r
195 foreach (WebReference reference in webReferences)
\r
197 ServiceDescriptionImporter importer = new ServiceDescriptionImporter ();
\r
198 importer.CodeGenerator = codeGenerator;
\r
199 importer.CodeGenerationOptions = options;
\r
200 importer.Context = context;
\r
201 importer.Style = style;
\r
202 importer.ProtocolName = reference.ProtocolName;
\r
204 importer.AddReference (reference);
\r
206 reference.Warnings = importer.Import (reference.ProxyCode, codeCompileUnit);
\r
207 reference.SetValidationWarnings (context.Warnings);
\r
208 foreach (string s in context.Warnings)
\r
209 allWarnings.Add (s);
\r
211 context.Warnings.Clear ();
\r
214 return allWarnings;
\r
217 internal void AddReference (WebReference reference)
\r
219 foreach (object doc in reference.Documents.Values)
\r
221 if (doc is ServiceDescription) {
\r
222 ServiceDescription service = (ServiceDescription) doc;
\r
223 ImportInfo info = new ImportInfo (service, reference);
\r
224 importInfo.Add (info);
\r
225 serviceDescriptions.Add (service);
\r
227 if (service.Types != null)
\r
228 schemas.Add (service.Types.Schemas);
\r
230 else if (doc is XmlSchema) {
\r
231 schemas.Add ((XmlSchema) doc);
\r
241 internal class ImportInfo
\r
243 string _appSettingUrlKey;
\r
244 string _appSettingBaseUrl;
\r
245 ServiceDescription _serviceDescription;
\r
247 public WebReference _reference;
\r
249 public ImportInfo (ServiceDescription serviceDescription, string appSettingUrlKey, string appSettingBaseUrl)
\r
251 _serviceDescription = serviceDescription;
\r
252 _appSettingUrlKey = appSettingUrlKey;
\r
253 _appSettingBaseUrl = appSettingBaseUrl;
\r
256 public ImportInfo (ServiceDescription serviceDescription, WebReference reference)
\r
258 _reference = reference;
\r
259 _serviceDescription = serviceDescription;
\r
262 public WebReference Reference {
\r
263 get { return _reference; }
\r
266 public ServiceDescription ServiceDescription {
\r
267 get { return _serviceDescription; }
\r
270 public string AppSettingUrlKey {
\r
272 if (_reference != null) return _reference.AppSettingUrlKey;
\r
273 else return _appSettingUrlKey;
\r
276 _appSettingUrlKey = value;
\r
280 public string AppSettingBaseUrl {
\r
282 if (_reference != null) return _reference.AppSettingBaseUrl;
\r
283 else return _appSettingBaseUrl;
\r
286 _appSettingBaseUrl = value;
\r