2 // System.Runtime.Remoting.SoapServices.cs
4 // Author: Jaime Anguiano Olarra (jaime@gnome.org)
5 // Lluis Sanchez Gual (lluis@ximian.com)
7 // (c) 2002, Jaime Anguiano Olarra
11 // Copyright (C) 2004 Novell, Inc (http://www.novell.com)
13 // Permission is hereby granted, free of charge, to any person obtaining
14 // a copy of this software and associated documentation files (the
15 // "Software"), to deal in the Software without restriction, including
16 // without limitation the rights to use, copy, modify, merge, publish,
17 // distribute, sublicense, and/or sell copies of the Software, and to
18 // permit persons to whom the Software is furnished to do so, subject to
19 // the following conditions:
21 // The above copyright notice and this permission notice shall be
22 // included in all copies or substantial portions of the Software.
24 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
25 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
26 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
27 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
28 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
29 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
30 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
35 using System.Collections;
36 using System.Runtime.Remoting;
37 using System.Runtime.Remoting.Metadata;
38 using System.Reflection;
39 using System.Runtime.InteropServices;
41 namespace System.Runtime.Remoting {
43 public class SoapServices
45 static Hashtable _xmlTypes = new Hashtable ();
46 static Hashtable _xmlElements = new Hashtable ();
47 static Hashtable _soapActions = new Hashtable ();
48 static Hashtable _soapActionsMethods = new Hashtable ();
49 static Hashtable _typeInfos = new Hashtable ();
53 public Hashtable Attributes;
54 public Hashtable Elements;
57 // Private constructor: nobody instantiates this class
58 private SoapServices () {}
62 public static string XmlNsForClrType
64 get { return "http://schemas.microsoft.com/clr/"; }
67 public static string XmlNsForClrTypeWithAssembly
69 get { return "http://schemas.microsoft.com/clr/assem/"; }
72 public static string XmlNsForClrTypeWithNs
74 get { return "http://schemas.microsoft.com/clr/ns/"; }
77 public static string XmlNsForClrTypeWithNsAndAssembly
79 get { return "http://schemas.microsoft.com/clr/nsassem/"; }
85 public static string CodeXmlNamespaceForClrTypeNamespace (string typeNamespace,
88 // If assemblyName is empty, then use the corlib namespace
90 if (assemblyName == string.Empty)
91 return XmlNsForClrTypeWithNs + typeNamespace;
92 else if (typeNamespace == string.Empty)
93 return EncodeNs (XmlNsForClrTypeWithAssembly + assemblyName);
95 return EncodeNs (XmlNsForClrTypeWithNsAndAssembly + typeNamespace + "/" + assemblyName);
98 public static bool DecodeXmlNamespaceForClrTypeNamespace (string inNamespace,
99 out string typeNamespace,
100 out string assemblyName) {
102 if (inNamespace == null) throw new ArgumentNullException ("inNamespace");
104 inNamespace = DecodeNs (inNamespace);
105 typeNamespace = null;
108 if (inNamespace.StartsWith(XmlNsForClrTypeWithNsAndAssembly))
110 int typePos = XmlNsForClrTypeWithNsAndAssembly.Length;
111 if (typePos >= inNamespace.Length) return false;
112 int assemPos = inNamespace.IndexOf ('/', typePos+1);
113 if (assemPos == -1) return false;
114 typeNamespace = inNamespace.Substring (typePos, assemPos - typePos);
115 assemblyName = inNamespace.Substring (assemPos+1);
118 else if (inNamespace.StartsWith(XmlNsForClrTypeWithNs))
120 int typePos = XmlNsForClrTypeWithNs.Length;
121 typeNamespace = inNamespace.Substring (typePos);
124 else if (inNamespace.StartsWith(XmlNsForClrTypeWithAssembly))
126 int typePos = XmlNsForClrTypeWithAssembly.Length;
127 assemblyName = inNamespace.Substring (typePos);
134 public static void GetInteropFieldTypeAndNameFromXmlAttribute (Type containingType,
135 string xmlAttribute, string xmlNamespace,
136 out Type type, out string name)
138 TypeInfo tf = (TypeInfo) _typeInfos [containingType];
139 Hashtable ht = tf != null ? tf.Attributes : null;
140 GetInteropFieldInfo (ht, xmlAttribute, xmlNamespace, out type, out name);
143 public static void GetInteropFieldTypeAndNameFromXmlElement (Type containingType,
144 string xmlElement, string xmlNamespace,
145 out Type type, out string name)
147 TypeInfo tf = (TypeInfo) _typeInfos [containingType];
148 Hashtable ht = tf != null ? tf.Elements : null;
149 GetInteropFieldInfo (ht, xmlElement, xmlNamespace, out type, out name);
152 static void GetInteropFieldInfo (Hashtable fields,
153 string xmlName, string xmlNamespace,
154 out Type type, out string name)
158 FieldInfo field = (FieldInfo) fields [GetNameKey (xmlName, xmlNamespace)];
161 type = field.FieldType;
170 static string GetNameKey (string name, string namspace)
172 if (namspace == null) return name;
173 else return name + " " + namspace;
176 public static Type GetInteropTypeFromXmlElement (string xmlElement, string xmlNamespace)
178 lock (_xmlElements.SyncRoot)
180 return (Type) _xmlElements [xmlElement + " " + xmlNamespace];
184 public static Type GetInteropTypeFromXmlType (string xmlType, string xmlTypeNamespace)
186 lock (_xmlTypes.SyncRoot)
188 return (Type) _xmlTypes [xmlType + " " + xmlTypeNamespace];
192 private static string GetAssemblyName(MethodBase mb)
194 if (mb.DeclaringType.Assembly == typeof (object).Assembly)
197 return mb.DeclaringType.Assembly.GetName().Name;
200 public static string GetSoapActionFromMethodBase (MethodBase mb)
202 return InternalGetSoapAction (mb);
205 public static bool GetTypeAndMethodNameFromSoapAction (string soapAction,
207 out string methodName)
209 lock (_soapActions.SyncRoot)
211 MethodBase mb = (MethodBase) _soapActionsMethods [soapAction];
214 typeName = mb.DeclaringType.AssemblyQualifiedName;
215 methodName = mb.Name;
226 int i = soapAction.LastIndexOf ('#');
227 if (i == -1) return false;
229 methodName = soapAction.Substring (i+1);
231 if (!DecodeXmlNamespaceForClrTypeNamespace (soapAction.Substring (0,i), out type, out assembly) )
234 if (assembly == null)
235 typeName = type + ", " + typeof (object).Assembly.GetName().Name;
237 typeName = type + ", " + assembly;
242 public static bool GetXmlElementForInteropType (Type type, out string xmlElement, out string xmlNamespace)
244 SoapTypeAttribute att = (SoapTypeAttribute) InternalRemotingServices.GetCachedSoapAttribute (type);
245 if (!att.IsInteropXmlElement)
252 xmlElement = att.XmlElementName;
253 xmlNamespace = att.XmlNamespace;
257 public static string GetXmlNamespaceForMethodCall (MethodBase mb)
259 return CodeXmlNamespaceForClrTypeNamespace (mb.DeclaringType.FullName, GetAssemblyName(mb));
262 public static string GetXmlNamespaceForMethodResponse (MethodBase mb)
264 return CodeXmlNamespaceForClrTypeNamespace (mb.DeclaringType.FullName, GetAssemblyName(mb));
267 public static bool GetXmlTypeForInteropType (Type type, out string xmlType, out string xmlTypeNamespace)
269 SoapTypeAttribute att = (SoapTypeAttribute) InternalRemotingServices.GetCachedSoapAttribute (type);
271 if (!att.IsInteropXmlType)
274 xmlTypeNamespace = null;
278 xmlType = att.XmlTypeName;
279 xmlTypeNamespace = att.XmlTypeNamespace;
283 public static bool IsClrTypeNamespace (string namespaceString)
285 return namespaceString.StartsWith (XmlNsForClrType);
288 public static bool IsSoapActionValidForMethodBase (string soapAction, MethodBase mb)
292 GetTypeAndMethodNameFromSoapAction (soapAction, out typeName, out methodName);
294 if (methodName != mb.Name) return false;
296 string methodClassType = mb.DeclaringType.AssemblyQualifiedName;
297 return typeName == methodClassType;
300 public static void PreLoad (Assembly assembly)
302 foreach (Type t in assembly.GetTypes ())
306 public static void PreLoad (Type type)
308 string name, namspace;
309 TypeInfo tf = _typeInfos [type] as TypeInfo;
310 if (tf != null) return;
312 if (GetXmlTypeForInteropType (type, out name, out namspace))
313 RegisterInteropXmlType (name, namspace, type);
315 if (GetXmlElementForInteropType (type, out name, out namspace))
316 RegisterInteropXmlElement (name, namspace, type);
318 lock (_typeInfos.SyncRoot)
320 tf = new TypeInfo ();
321 FieldInfo[] fields = type.GetFields (BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic);
323 foreach (FieldInfo field in fields)
325 SoapFieldAttribute att = (SoapFieldAttribute) InternalRemotingServices.GetCachedSoapAttribute (field);
326 if (!att.IsInteropXmlElement ()) continue;
328 string key = GetNameKey (att.XmlElementName, att.XmlNamespace);
329 if (att.UseAttribute)
331 if (tf.Attributes == null) tf.Attributes = new Hashtable ();
332 tf.Attributes [key] = field;
336 if (tf.Elements == null) tf.Elements = new Hashtable ();
337 tf.Elements [key] = field;
340 _typeInfos [type] = tf;
344 public static void RegisterInteropXmlElement (string xmlElement, string xmlNamespace, Type type)
346 lock (_xmlElements.SyncRoot)
348 _xmlElements [xmlElement + " " + xmlNamespace] = type;
352 public static void RegisterInteropXmlType (string xmlType, string xmlTypeNamespace, Type type)
354 lock (_xmlTypes.SyncRoot)
356 _xmlTypes [xmlType + " " + xmlTypeNamespace] = type;
360 public static void RegisterSoapActionForMethodBase (MethodBase mb)
362 InternalGetSoapAction (mb);
365 static string InternalGetSoapAction (MethodBase mb)
367 lock (_soapActions.SyncRoot)
369 string action = (string) _soapActions [mb];
372 SoapMethodAttribute att = (SoapMethodAttribute) InternalRemotingServices.GetCachedSoapAttribute (mb);
373 action = att.SoapAction;
374 _soapActions [mb] = action;
375 _soapActionsMethods [action] = mb;
381 public static void RegisterSoapActionForMethodBase (MethodBase mb, string soapAction)
383 lock (_soapActions.SyncRoot)
385 _soapActions [mb] = soapAction;
386 _soapActionsMethods [soapAction] = mb;
390 static string EncodeNs (string ns)
392 // Simple url encoding for namespaces
394 ns = ns.Replace (",","%2C");
395 ns = ns.Replace (" ","%20");
396 return ns.Replace ("=","%3D");
399 static string DecodeNs (string ns)
401 ns = ns.Replace ("%2C",",");
402 ns = ns.Replace ("%20"," ");
403 return ns.Replace ("%3D","=");