2 // Methods.cs: Information about a method and its mapping to a SOAP web service.
6 // Lluis Sanchez Gual (lluis@ximian.com)
8 // (C) 2003 Ximian, Inc.
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.
32 using System.Reflection;
33 using System.Collections;
35 using System.Xml.Serialization;
36 using System.Web.Services;
37 using System.Web.Services.Description;
39 namespace System.Web.Services.Protocols {
42 // This class represents all the information we extract from a MethodInfo
43 // in the WebClientProtocol derivative stub class
45 internal class MethodStubInfo
47 internal LogicalMethodInfo MethodInfo;
48 internal TypeStubInfo TypeStub;
50 // The name used by the stub class to reference this method.
52 internal WebMethodAttribute MethodAttribute;
54 internal string OperationName
56 get { return MethodInfo.Name; }
62 public MethodStubInfo (TypeStubInfo parent, LogicalMethodInfo source)
67 object [] o = source.GetCustomAttributes (typeof (WebMethodAttribute));
70 MethodAttribute = (WebMethodAttribute) o [0];
71 Name = MethodAttribute.MessageName;
72 if (Name == "") Name = source.Name;
80 // Holds the metadata loaded from the type stub, as well as
81 // the metadata for all the methods in the type
83 internal abstract class TypeStubInfo
85 Hashtable name_to_method = new Hashtable ();
86 MethodStubInfo[] methods;
87 ArrayList bindings = new ArrayList ();
88 LogicalTypeInfo logicalType;
89 string defaultBinding;
91 XmlSerializer[] serializers;
93 public TypeStubInfo (LogicalTypeInfo logicalTypeInfo)
95 this.logicalType = logicalTypeInfo;
97 object [] o = Type.GetCustomAttributes (typeof (WebServiceBindingAttribute), false);
99 bool isClientSide = typeof (SoapHttpClientProtocol).IsAssignableFrom (Type);
100 bool defaultAdded = false;
102 string defaultBindingName = logicalType.WebServiceName + ProtocolName;
104 foreach (WebServiceBindingAttribute at in o) {
105 AddBinding (new BindingInfo (at, defaultBindingName, LogicalType.WebServiceNamespace));
106 if ((at.Name == null || at.Name.Length == 0) || (at.Name == defaultBindingName))
110 if (!defaultAdded && !isClientSide)
111 AddBindingAt (0, new BindingInfo (null, defaultBindingName, logicalType.WebServiceNamespace));
113 foreach (Type ifaceType in Type.GetInterfaces ()) {
114 o = ifaceType.GetCustomAttributes (typeof (WebServiceBindingAttribute), false);
116 defaultBindingName = ifaceType.Name + ProtocolName;
117 foreach (WebServiceBindingAttribute at in o)
118 AddBinding (new BindingInfo (at, defaultBindingName, LogicalType.WebServiceNamespace));
123 public WsiProfiles WsiClaims {
125 return (((BindingInfo) Bindings [0]).WebServiceBindingAttribute != null) ?
126 ((BindingInfo) Bindings [0]).WebServiceBindingAttribute.ConformsTo : WsiProfiles.None;
130 public LogicalTypeInfo LogicalType
132 get { return logicalType; }
137 get { return logicalType.Type; }
140 public string DefaultBinding
142 get { return defaultBinding; }
145 public virtual XmlReflectionImporter XmlImporter
150 public virtual SoapReflectionImporter SoapImporter
155 public virtual string ProtocolName
160 public XmlSerializer GetSerializer (int n)
162 return serializers [n];
165 public int RegisterSerializer (XmlMapping map)
167 if (mappings == null) mappings = new ArrayList ();
168 return mappings.Add (map);
171 public void Initialize ()
175 if (mappings != null)
177 // Build all the serializers at once
178 XmlMapping[] maps = (XmlMapping[]) mappings.ToArray(typeof(XmlMapping));
179 serializers = XmlSerializer.FromMappings (maps);
184 // Extract all method information
186 protected virtual void BuildTypeMethods ()
188 bool isClientProxy = typeof(WebClientProtocol).IsAssignableFrom (Type);
190 ArrayList metStubs = new ArrayList ();
191 foreach (LogicalMethodInfo mi in logicalType.LogicalMethods)
193 if (!isClientProxy && mi.CustomAttributeProvider.GetCustomAttributes (typeof(WebMethodAttribute), true).Length == 0)
196 MethodStubInfo msi = CreateMethodStubInfo (this, mi, isClientProxy);
201 if (name_to_method.ContainsKey (msi.Name)) {
202 string msg = "Both " + msi.MethodInfo.ToString () + " and " + GetMethod (msi.Name).MethodInfo + " use the message name '" + msi.Name + "'. ";
203 msg += "Use the MessageName property of WebMethod custom attribute to specify unique message names for the methods";
204 throw new InvalidOperationException (msg);
207 name_to_method [msi.Name] = msi;
210 methods = (MethodStubInfo[]) metStubs.ToArray (typeof (MethodStubInfo));
213 protected abstract MethodStubInfo CreateMethodStubInfo (TypeStubInfo typeInfo, LogicalMethodInfo methodInfo, bool isClientProxy);
215 public MethodStubInfo GetMethod (string name)
217 return (MethodStubInfo) name_to_method [name];
220 public MethodStubInfo[] Methods
222 get { return methods; }
225 internal ArrayList Bindings
227 get { return bindings; }
230 internal void AddBinding (BindingInfo info)
235 internal void AddBindingAt (int pos, BindingInfo info)
237 bindings.Insert (pos, info);
240 internal BindingInfo GetBinding (string name)
242 if (name == null || name.Length == 0) return (BindingInfo) bindings[0];
244 for (int n = 0; n < bindings.Count; n++)
245 if (((BindingInfo)bindings[n]).Name == name) return (BindingInfo)bindings[n];
250 internal class BindingInfo
252 public BindingInfo (WebServiceBindingAttribute at, string name, string ns)
256 Namespace = at.Namespace;
257 Location = at.Location;
258 WebServiceBindingAttribute = at;
261 if (Name == null || Name.Length == 0)
264 if (Namespace == null || Namespace.Length == 0)
268 public readonly string Name;
269 public readonly string Namespace;
270 public readonly string Location;
271 public readonly WebServiceBindingAttribute WebServiceBindingAttribute;
275 // Manages type stubs
277 internal class TypeStubManager
279 static Hashtable type_to_manager;
281 static TypeStubManager ()
283 type_to_manager = new Hashtable ();
286 static internal TypeStubInfo GetTypeStub (Type t, string protocolName)
288 LogicalTypeInfo tm = GetLogicalTypeInfo (t);
289 return tm.GetTypeStub (protocolName);
293 // This needs to be thread safe
295 static internal LogicalTypeInfo GetLogicalTypeInfo (Type t)
297 lock (type_to_manager)
299 LogicalTypeInfo tm = (LogicalTypeInfo) type_to_manager [t];
304 tm = new LogicalTypeInfo (t);
305 type_to_manager [t] = tm;