5 // Martin Baulig <martin.baulig@xamarin.com>
7 // Copyright (c) 2012 Xamarin, Inc.
9 // Permission is hereby granted, free of charge, to any person obtaining a copy
10 // of this software and associated documentation files (the "Software"), to deal
11 // in the Software without restriction, including without limitation the rights
12 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
13 // copies of the Software, and to permit persons to whom the Software is
14 // furnished to do so, subject to the following conditions:
16 // The above copyright notice and this permission notice shall be included in
17 // all copies or substantial portions of the Software.
19 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
22 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
23 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
24 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
32 using System.Reflection;
33 using System.ServiceModel.Description;
34 using System.Web.Services.Discovery;
35 using System.Runtime.Serialization;
36 using WebServices = System.Web.Services;
39 namespace MonoTests.System.Runtime.Serialization
41 public static class WsdlHelper
44 * This reads a normal .wsdl file from an embedded resource.
46 * You can simply fetch them from your server using
47 * 'curl http://yourserver/YourService.svc?singleWsdl > YourService.wsdl',
48 * add the .wsdl file to Test/Resources/WSDL and add it to `TEST_RESOURCE_FILES'
52 public static MetadataSet GetMetadataSet (string name)
54 var asm = Assembly.GetExecutingAssembly ();
55 using (var stream = asm.GetManifestResourceStream (name)) {
57 throw new InvalidOperationException (string.Format (
58 "Cannot find resource file '{0}'.", name));
59 return GetMetadataSet (stream);
63 public static MetadataSet GetMetadataSet (Stream stream)
65 var dr = new ContractReference ();
66 var doc = (WebServices.Description.ServiceDescription) dr.ReadDocument (stream);
68 var metadata = new MetadataSet ();
69 metadata.MetadataSections.Add (
70 new MetadataSection (MetadataSection.ServiceDescriptionDialect, "", doc));
74 public static CodeCompileUnit Import (MetadataSet metadata, ImportOptions options)
76 var importer = new WsdlImporter (metadata);
77 var xsdImporter = new XsdDataContractImporter ();
78 xsdImporter.Options = options;
79 importer.State.Add (typeof(XsdDataContractImporter), xsdImporter);
81 var contracts = importer.ImportAllContracts ();
83 CodeCompileUnit ccu = new CodeCompileUnit ();
84 var generator = new ServiceContractGenerator (ccu);
86 if (contracts.Count != 1)
87 throw new InvalidOperationException (string.Format (
88 "Metadata import failed: found {0} contracts.", contracts.Count));
90 var contract = contracts.First ();
91 generator.GenerateServiceContractType (contract);
96 public static CodeNamespace Find (this CodeNamespaceCollection collection, string name)
98 foreach (CodeNamespace ns in collection) {
106 public static CodeNamespace FindNamespace (this CodeCompileUnit unit, string name)
108 foreach (CodeNamespace ns in unit.Namespaces) {
116 public static CodeTypeDeclaration FindType (this CodeNamespace ns, string name)
118 foreach (CodeTypeDeclaration type in ns.Types) {
119 if (type.Name == name)
126 public static CodeTypeDeclaration FindType (this CodeCompileUnit unit, string name)
128 foreach (CodeNamespace ns in unit.Namespaces) {
129 foreach (CodeTypeDeclaration type in ns.Types) {
130 if (type.Name == name)
138 public static CodeMemberMethod FindMethod (this CodeTypeDeclaration type, string name)
140 foreach (var member in type.Members) {
141 var method = member as CodeMemberMethod;
144 if (method.Name == name)
151 public static CodeMemberMethod FindMethod (this CodeCompileUnit unit, string typeName,
154 var type = unit.FindType (typeName);
157 return type.FindMethod (methodName);
160 public static CodeAttributeDeclaration FindAttribute (this CodeTypeDeclaration type, string name)
162 foreach (CodeAttributeDeclaration attr in type.CustomAttributes) {
163 if (attr.Name == name)
170 public static CodeAttributeArgument FindArgument (this CodeAttributeDeclaration attr, string name)
172 foreach (CodeAttributeArgument arg in attr.Arguments) {
173 if (arg.Name == name)