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 string defaultBindingName = logicalType.WebServiceName + ProtocolName;
101 foreach (WebServiceBindingAttribute at in o)
102 AddBinding (new BindingInfo (at, defaultBindingName, LogicalType.WebServiceNamespace));
104 AddBinding (new BindingInfo (null, defaultBindingName, logicalType.WebServiceNamespace));
107 foreach (Type ifaceType in Type.GetInterfaces ()) {
108 o = ifaceType.GetCustomAttributes (typeof (WebServiceBindingAttribute), false);
110 defaultBindingName = ifaceType.Name + ProtocolName;
111 foreach (WebServiceBindingAttribute at in o)
112 AddBinding (new BindingInfo (at, defaultBindingName, LogicalType.WebServiceNamespace));
119 public WsiProfiles WsiClaims {
121 return (((BindingInfo) Bindings [0]).WebServiceBindingAttribute != null) ?
122 ((BindingInfo) Bindings [0]).WebServiceBindingAttribute.ConformsTo : WsiProfiles.None;
127 public LogicalTypeInfo LogicalType
129 get { return logicalType; }
134 get { return logicalType.Type; }
137 public string DefaultBinding
139 get { return defaultBinding; }
142 public virtual XmlReflectionImporter XmlImporter
147 public virtual SoapReflectionImporter SoapImporter
152 public virtual string ProtocolName
157 public XmlSerializer GetSerializer (int n)
159 return serializers [n];
162 public int RegisterSerializer (XmlMapping map)
164 if (mappings == null) mappings = new ArrayList ();
165 return mappings.Add (map);
168 public void Initialize ()
172 if (mappings != null)
174 // Build all the serializers at once
175 XmlMapping[] maps = (XmlMapping[]) mappings.ToArray(typeof(XmlMapping));
176 serializers = XmlSerializer.FromMappings (maps);
181 // Extract all method information
183 protected virtual void BuildTypeMethods ()
185 bool isClientProxy = typeof(WebClientProtocol).IsAssignableFrom (Type);
187 ArrayList metStubs = new ArrayList ();
188 foreach (LogicalMethodInfo mi in logicalType.LogicalMethods)
190 if (!isClientProxy && mi.CustomAttributeProvider.GetCustomAttributes (typeof(WebMethodAttribute), true).Length == 0)
193 MethodStubInfo msi = CreateMethodStubInfo (this, mi, isClientProxy);
198 if (name_to_method.ContainsKey (msi.Name)) {
199 string msg = "Both " + msi.MethodInfo.ToString () + " and " + GetMethod (msi.Name).MethodInfo + " use the message name '" + msi.Name + "'. ";
200 msg += "Use the MessageName property of WebMethod custom attribute to specify unique message names for the methods";
201 throw new InvalidOperationException (msg);
204 name_to_method [msi.Name] = msi;
207 methods = (MethodStubInfo[]) metStubs.ToArray (typeof (MethodStubInfo));
210 protected abstract MethodStubInfo CreateMethodStubInfo (TypeStubInfo typeInfo, LogicalMethodInfo methodInfo, bool isClientProxy);
212 public MethodStubInfo GetMethod (string name)
214 return (MethodStubInfo) name_to_method [name];
217 public MethodStubInfo[] Methods
219 get { return methods; }
222 internal ArrayList Bindings
224 get { return bindings; }
227 internal void AddBinding (BindingInfo info)
232 internal BindingInfo GetBinding (string name)
234 if (name == null || name.Length == 0) return (BindingInfo) bindings[0];
236 for (int n = 0; n < bindings.Count; n++)
237 if (((BindingInfo)bindings[n]).Name == name) return (BindingInfo)bindings[n];
242 internal class BindingInfo
244 public BindingInfo (WebServiceBindingAttribute at, string name, string ns)
250 Namespace = at.Namespace;
251 Location = at.Location;
252 WebServiceBindingAttribute = at;
255 if (Name == null || Name.Length == 0)
258 if (Namespace == null || Namespace.Length == 0)
262 public readonly string Name;
263 public readonly string Namespace;
264 public readonly string Location;
265 public readonly WebServiceBindingAttribute WebServiceBindingAttribute;
269 // Manages type stubs
271 internal class TypeStubManager
274 static Hashtable type_to_manager;
276 const string type_to_manager_key = "TypeStubManager.type_to_manager";
277 static Hashtable type_to_manager {
279 Hashtable hash = (Hashtable)AppDomain.CurrentDomain.GetData(type_to_manager_key);
284 lock(type_to_manager_key) {
285 AppDomain.CurrentDomain.SetData(type_to_manager_key, new Hashtable());
288 return (Hashtable)AppDomain.CurrentDomain.GetData(type_to_manager_key);
291 //do nothing: we manage our type_to_manager per domain
296 static TypeStubManager ()
298 type_to_manager = new Hashtable ();
301 static internal TypeStubInfo GetTypeStub (Type t, string protocolName)
303 LogicalTypeInfo tm = GetLogicalTypeInfo (t);
304 return tm.GetTypeStub (protocolName);
308 // This needs to be thread safe
310 static internal LogicalTypeInfo GetLogicalTypeInfo (Type t)
312 lock (type_to_manager)
314 LogicalTypeInfo tm = (LogicalTypeInfo) type_to_manager [t];
319 tm = new LogicalTypeInfo (t);
320 type_to_manager [t] = tm;