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));
114 foreach (Type ifaceType in Type.GetInterfaces ()) {
115 o = ifaceType.GetCustomAttributes (typeof (WebServiceBindingAttribute), false);
117 defaultBindingName = ifaceType.Name + ProtocolName;
118 foreach (WebServiceBindingAttribute at in o)
119 AddBinding (new BindingInfo (at, defaultBindingName, LogicalType.WebServiceNamespace));
126 public WsiProfiles WsiClaims {
128 return (((BindingInfo) Bindings [0]).WebServiceBindingAttribute != null) ?
129 ((BindingInfo) Bindings [0]).WebServiceBindingAttribute.ConformsTo : WsiProfiles.None;
134 public LogicalTypeInfo LogicalType
136 get { return logicalType; }
141 get { return logicalType.Type; }
144 public string DefaultBinding
146 get { return defaultBinding; }
149 public virtual XmlReflectionImporter XmlImporter
154 public virtual SoapReflectionImporter SoapImporter
159 public virtual string ProtocolName
164 public XmlSerializer GetSerializer (int n)
166 return serializers [n];
169 public int RegisterSerializer (XmlMapping map)
171 if (mappings == null) mappings = new ArrayList ();
172 return mappings.Add (map);
175 public void Initialize ()
179 if (mappings != null)
181 // Build all the serializers at once
182 XmlMapping[] maps = (XmlMapping[]) mappings.ToArray(typeof(XmlMapping));
183 serializers = XmlSerializer.FromMappings (maps);
188 // Extract all method information
190 protected virtual void BuildTypeMethods ()
192 bool isClientProxy = typeof(WebClientProtocol).IsAssignableFrom (Type);
194 ArrayList metStubs = new ArrayList ();
195 foreach (LogicalMethodInfo mi in logicalType.LogicalMethods)
197 if (!isClientProxy && mi.CustomAttributeProvider.GetCustomAttributes (typeof(WebMethodAttribute), true).Length == 0)
200 MethodStubInfo msi = CreateMethodStubInfo (this, mi, isClientProxy);
205 if (name_to_method.ContainsKey (msi.Name)) {
206 string msg = "Both " + msi.MethodInfo.ToString () + " and " + GetMethod (msi.Name).MethodInfo + " use the message name '" + msi.Name + "'. ";
207 msg += "Use the MessageName property of WebMethod custom attribute to specify unique message names for the methods";
208 throw new InvalidOperationException (msg);
211 name_to_method [msi.Name] = msi;
214 methods = (MethodStubInfo[]) metStubs.ToArray (typeof (MethodStubInfo));
217 protected abstract MethodStubInfo CreateMethodStubInfo (TypeStubInfo typeInfo, LogicalMethodInfo methodInfo, bool isClientProxy);
219 public MethodStubInfo GetMethod (string name)
221 return (MethodStubInfo) name_to_method [name];
224 public MethodStubInfo[] Methods
226 get { return methods; }
229 internal ArrayList Bindings
231 get { return bindings; }
234 internal void AddBinding (BindingInfo info)
239 internal void AddBindingAt (int pos, BindingInfo info)
241 bindings.Insert (pos, info);
244 internal BindingInfo GetBinding (string name)
246 if (name == null || name.Length == 0) return (BindingInfo) bindings[0];
248 for (int n = 0; n < bindings.Count; n++)
249 if (((BindingInfo)bindings[n]).Name == name) return (BindingInfo)bindings[n];
254 internal class BindingInfo
256 public BindingInfo (WebServiceBindingAttribute at, string name, string ns)
262 Namespace = at.Namespace;
263 Location = at.Location;
264 WebServiceBindingAttribute = at;
267 if (Name == null || Name.Length == 0)
270 if (Namespace == null || Namespace.Length == 0)
274 public readonly string Name;
275 public readonly string Namespace;
276 public readonly string Location;
277 public readonly WebServiceBindingAttribute WebServiceBindingAttribute;
281 // Manages type stubs
283 internal class TypeStubManager
286 static Hashtable type_to_manager;
288 const string type_to_manager_key = "TypeStubManager.type_to_manager";
289 static Hashtable type_to_manager {
291 Hashtable hash = (Hashtable)AppDomain.CurrentDomain.GetData(type_to_manager_key);
296 lock(type_to_manager_key) {
297 AppDomain.CurrentDomain.SetData(type_to_manager_key, new Hashtable());
300 return (Hashtable)AppDomain.CurrentDomain.GetData(type_to_manager_key);
303 //do nothing: we manage our type_to_manager per domain
308 static TypeStubManager ()
310 type_to_manager = new Hashtable ();
313 static internal TypeStubInfo GetTypeStub (Type t, string protocolName)
315 LogicalTypeInfo tm = GetLogicalTypeInfo (t);
316 return tm.GetTypeStub (protocolName);
320 // This needs to be thread safe
322 static internal LogicalTypeInfo GetLogicalTypeInfo (Type t)
324 lock (type_to_manager)
326 LogicalTypeInfo tm = (LogicalTypeInfo) type_to_manager [t];
331 tm = new LogicalTypeInfo (t);
332 type_to_manager [t] = tm;