Methods.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413
  1. //
  2. // Methods.cs: Information about a method and its mapping to a SOAP web service.
  3. //
  4. // Author:
  5. // Miguel de Icaza
  6. //
  7. // (C) 2003 Ximian, Inc.
  8. //
  9. // TODO:
  10. //
  11. //
  12. using System.Reflection;
  13. using System.Collections;
  14. using System.Xml;
  15. using System.Xml.Serialization;
  16. using System.Web.Services;
  17. using System.Web.Services.Description;
  18. namespace System.Web.Services.Protocols {
  19. //
  20. // This class represents all the information we extract from a MethodInfo
  21. // in the SoapHttpClientProtocol derivative stub class
  22. //
  23. internal class MethodStubInfo {
  24. internal LogicalMethodInfo MethodInfo;
  25. // The name used bythe stub class to reference this method.
  26. internal string Name;
  27. internal string Action;
  28. internal string Binding;
  29. // The name/namespace of the request
  30. internal string RequestName;
  31. internal string RequestNamespace;
  32. // The name/namespace of the response.
  33. internal string ResponseName;
  34. internal string ResponseNamespace;
  35. internal bool OneWay;
  36. internal SoapParameterStyle ParameterStyle;
  37. internal XmlSerializer RequestSerializer;
  38. internal XmlSerializer ResponseSerializer;
  39. internal HeaderInfo[] Headers;
  40. //
  41. // Constructor
  42. //
  43. MethodStubInfo (TypeStubInfo parent, LogicalMethodInfo source, object kind, XmlReflectionImporter xmlImporter, SoapReflectionImporter soapImporter)
  44. {
  45. MethodInfo = source;
  46. XmlElementAttribute optional_ns = null;
  47. SoapBindingUse use;
  48. if (kind is SoapDocumentMethodAttribute){
  49. SoapDocumentMethodAttribute dma = (SoapDocumentMethodAttribute) kind;
  50. use = dma.Use;
  51. if (use == SoapBindingUse.Default)
  52. use = parent.Use;
  53. Action = dma.Action;
  54. Binding = dma.Binding;
  55. RequestName = dma.RequestElementName;
  56. RequestNamespace = dma.RequestNamespace;
  57. ResponseName = dma.ResponseElementName;
  58. ResponseNamespace = dma.ResponseNamespace;
  59. ParameterStyle = dma.ParameterStyle;
  60. if (ParameterStyle == SoapParameterStyle.Default)
  61. ParameterStyle = parent.ParameterStyle;
  62. OneWay = dma.OneWay;
  63. } else {
  64. SoapRpcMethodAttribute rma = (SoapRpcMethodAttribute) kind;
  65. use = SoapBindingUse.Encoded; // RPC always use encoded
  66. Action = rma.Action;
  67. Binding = rma.Binding;
  68. RequestName = rma.RequestElementName;
  69. RequestNamespace = rma.RequestNamespace;
  70. ResponseNamespace = rma.ResponseNamespace;
  71. ResponseName = rma.ResponseElementName;
  72. OneWay = rma.OneWay;
  73. // For RPC calls, make all arguments be part of the empty namespace
  74. optional_ns = new XmlElementAttribute ();
  75. optional_ns.Namespace = "";
  76. }
  77. if (Binding == "")
  78. Binding = parent.BindingName;
  79. if (RequestName == "")
  80. RequestName = source.Name;
  81. if (OneWay){
  82. if (source.ReturnType != typeof (void))
  83. throw new Exception ("OneWay methods should not have a return value");
  84. if (source.OutParameters.Length != 0)
  85. throw new Exception ("OneWay methods should not have out/ref parameters");
  86. }
  87. object [] o = source.GetCustomAttributes (typeof (WebMethodAttribute));
  88. if (o.Length == 1){
  89. WebMethodAttribute wma = (WebMethodAttribute) o [0];
  90. Name = wma.MessageName;
  91. if (Name == "")
  92. Name = source.Name;
  93. } else
  94. Name = source.Name;
  95. if (ResponseName == "")
  96. ResponseName = Name + "Response";
  97. XmlReflectionMember [] in_members = BuildRequestReflectionMembers (optional_ns);
  98. XmlReflectionMember [] out_members = BuildResponseReflectionMembers (optional_ns);
  99. XmlMembersMapping [] members = new XmlMembersMapping [2];
  100. try {
  101. if (use == SoapBindingUse.Literal) {
  102. members [0] = xmlImporter.ImportMembersMapping (RequestName, RequestNamespace, in_members, true);
  103. members [1] = xmlImporter.ImportMembersMapping (ResponseName, ResponseNamespace, out_members, true);
  104. }
  105. else {
  106. members [0] = soapImporter.ImportMembersMapping (RequestName, RequestNamespace, in_members, true, true);
  107. members [1] = soapImporter.ImportMembersMapping (ResponseName, ResponseNamespace, out_members, true, true);
  108. }
  109. XmlSerializer [] s = null;
  110. s = XmlSerializer.FromMappings (members);
  111. RequestSerializer = s [0];
  112. ResponseSerializer = s [1];
  113. } catch {
  114. Console.WriteLine ("Got exception while creating serializer");
  115. Console.WriteLine ("Method name: " + RequestName + " parameters are:");
  116. for (int i = 0; i < in_members.Length; i++) {
  117. Console.WriteLine (" {0}: {1} {2}", i, in_members [i].MemberName, in_members [i].MemberType);
  118. }
  119. Console.WriteLine ("Output parameters are:");
  120. for (int i = 0; i < out_members.Length; i++) {
  121. Console.WriteLine (" {0}: {1} {2}", i, out_members [i].MemberName, out_members [i].MemberType);
  122. }
  123. throw;
  124. }
  125. o = source.GetCustomAttributes (typeof (SoapHeaderAttribute));
  126. Headers = new HeaderInfo[o.Length];
  127. for (int i = 0; i < o.Length; i++) {
  128. SoapHeaderAttribute att = (SoapHeaderAttribute) o[i];
  129. MemberInfo[] mems = source.DeclaringType.GetMember (att.MemberName);
  130. if (mems.Length == 0) throw new InvalidOperationException ("Member " + att.MemberName + " not found in class " + source.DeclaringType.FullName);
  131. Type headerType = (mems[0] is FieldInfo) ? ((FieldInfo)mems[0]).FieldType : ((PropertyInfo)mems[0]).PropertyType;
  132. Headers [i] = new HeaderInfo (parent.GetCommonSerializer (headerType), mems[0], att);
  133. }
  134. }
  135. static internal MethodStubInfo Create (TypeStubInfo parent, LogicalMethodInfo lmi, XmlReflectionImporter xmlImporter, SoapReflectionImporter soapImporter)
  136. {
  137. object [] o = lmi.GetCustomAttributes (typeof (SoapDocumentMethodAttribute));
  138. if (o.Length == 0){
  139. o = lmi.GetCustomAttributes (typeof (SoapRpcMethodAttribute));
  140. if (o.Length == 0)
  141. return null;
  142. return new MethodStubInfo (parent, lmi, o [0], xmlImporter, soapImporter);
  143. } else
  144. return new MethodStubInfo (parent, lmi, o [0], xmlImporter, soapImporter);
  145. }
  146. XmlReflectionMember [] BuildRequestReflectionMembers (XmlElementAttribute optional_ns)
  147. {
  148. ParameterInfo [] input = MethodInfo.InParameters;
  149. XmlReflectionMember [] in_members = new XmlReflectionMember [input.Length];
  150. for (int i = 0; i < input.Length; i++)
  151. {
  152. XmlReflectionMember m = new XmlReflectionMember ();
  153. m.IsReturnValue = false;
  154. m.MemberName = input [i].Name;
  155. m.MemberType = input [i].ParameterType;
  156. m.XmlAttributes = new XmlAttributes (input[i]);
  157. m.SoapAttributes = new SoapAttributes (input[i]);
  158. if (m.MemberType.IsByRef)
  159. m.MemberType = m.MemberType.GetElementType ();
  160. if (optional_ns != null)
  161. m.XmlAttributes.XmlElements.Add (optional_ns);
  162. in_members [i] = m;
  163. }
  164. return in_members;
  165. }
  166. XmlReflectionMember [] BuildResponseReflectionMembers (XmlElementAttribute optional_ns)
  167. {
  168. ParameterInfo [] output = MethodInfo.OutParameters;
  169. bool has_return_value = !(OneWay || MethodInfo.ReturnType == typeof (void));
  170. XmlReflectionMember [] out_members = new XmlReflectionMember [(has_return_value ? 1 : 0) + output.Length];
  171. XmlReflectionMember m;
  172. int idx = 0;
  173. if (has_return_value)
  174. {
  175. m = new XmlReflectionMember ();
  176. m.IsReturnValue = true;
  177. m.MemberName = RequestName + "Result";
  178. m.MemberType = MethodInfo.ReturnType;
  179. m.XmlAttributes = new XmlAttributes (MethodInfo.ReturnTypeCustomAttributeProvider);
  180. m.SoapAttributes = new SoapAttributes (MethodInfo.ReturnTypeCustomAttributeProvider);
  181. if (optional_ns != null)
  182. m.XmlAttributes.XmlElements.Add (optional_ns);
  183. idx++;
  184. out_members [0] = m;
  185. }
  186. for (int i = 0; i < output.Length; i++)
  187. {
  188. m = new XmlReflectionMember ();
  189. m.IsReturnValue = false;
  190. m.MemberName = output [i].Name;
  191. m.MemberType = output [i].ParameterType;
  192. m.XmlAttributes = new XmlAttributes (output[i]);
  193. m.SoapAttributes = new SoapAttributes (output[i]);
  194. if (m.MemberType.IsByRef)
  195. m.MemberType = m.MemberType.GetElementType ();
  196. if (optional_ns != null)
  197. m.XmlAttributes.XmlElements.Add (optional_ns);
  198. out_members [i + idx] = m;
  199. }
  200. return out_members;
  201. }
  202. }
  203. internal class HeaderInfo
  204. {
  205. internal XmlSerializer Serializer;
  206. internal MemberInfo Member;
  207. internal SoapHeaderAttribute AttributeInfo;
  208. public HeaderInfo (XmlSerializer serializer, MemberInfo member, SoapHeaderAttribute attributeInfo)
  209. {
  210. Serializer = serializer;
  211. Member = member;
  212. AttributeInfo = attributeInfo;
  213. }
  214. public object GetHeaderValue (object ob)
  215. {
  216. if (Member is PropertyInfo) return ((PropertyInfo)Member).GetValue (ob, null);
  217. else return ((FieldInfo)Member).GetValue (ob);
  218. }
  219. }
  220. internal class Fault
  221. {
  222. public XmlQualifiedName faultcode;
  223. public string faultstring;
  224. public string faultactor;
  225. public XmlNode detail;
  226. }
  227. //
  228. // Holds the metadata loaded from the type stub, as well as
  229. // the metadata for all the methods in the type
  230. //
  231. internal class TypeStubInfo {
  232. Hashtable name_to_method = new Hashtable ();
  233. Hashtable common_serializers = new Hashtable ();
  234. // Precomputed
  235. internal SoapParameterStyle ParameterStyle;
  236. internal SoapServiceRoutingStyle RoutingStyle;
  237. internal SoapBindingUse Use;
  238. internal string WebServiceName;
  239. internal string WebServiceNamespace;
  240. internal string BindingLocation;
  241. internal string BindingName;
  242. internal string BindingNamespace;
  243. internal XmlSerializer FaultSerializer;
  244. void GetTypeAttributes (Type t)
  245. {
  246. object [] o;
  247. o = t.GetCustomAttributes (typeof (WebServiceBindingAttribute), false);
  248. if (o.Length != 1)
  249. throw new Exception ("Expected WebServiceBindingAttribute on "+ t.Name);
  250. WebServiceBindingAttribute b = (WebServiceBindingAttribute) o [0];
  251. BindingLocation = b.Location;
  252. BindingName = b.Name;
  253. BindingNamespace = b.Namespace;
  254. o = t.GetCustomAttributes (typeof (WebService), false);
  255. if (o.Length == 1){
  256. WebServiceAttribute a = (WebServiceAttribute) o [0];
  257. WebServiceName = a.Name;
  258. WebServiceNamespace = a.Namespace;
  259. } else {
  260. WebServiceName = t.Name;
  261. WebServiceNamespace = WebServiceAttribute.DefaultNamespace;
  262. }
  263. o = t.GetCustomAttributes (typeof (SoapDocumentServiceAttribute), false);
  264. if (o.Length == 1){
  265. SoapDocumentServiceAttribute a = (SoapDocumentServiceAttribute) o [0];
  266. ParameterStyle = a.ParameterStyle;
  267. RoutingStyle = a.RoutingStyle;
  268. Use = a.Use;
  269. } else {
  270. o = t.GetCustomAttributes (typeof (SoapRpcServiceAttribute), false);
  271. if (o.Length == 1){
  272. SoapRpcServiceAttribute srs = (SoapRpcServiceAttribute) o [0];
  273. ParameterStyle = SoapParameterStyle.Wrapped;
  274. RoutingStyle = srs.RoutingStyle;
  275. Use = SoapBindingUse.Literal;
  276. } else {
  277. ParameterStyle = SoapParameterStyle.Wrapped;
  278. RoutingStyle = SoapServiceRoutingStyle.SoapAction;
  279. Use = SoapBindingUse.Literal;
  280. }
  281. }
  282. FaultSerializer = GetCommonSerializer (typeof(Fault));
  283. }
  284. //
  285. // Extract all method information
  286. //
  287. void GetTypeMethods (Type t, XmlReflectionImporter xmlImporter, SoapReflectionImporter soapImporter)
  288. {
  289. MethodInfo [] type_methods = t.GetMethods (BindingFlags.Instance | BindingFlags.Public);
  290. LogicalMethodInfo [] methods = LogicalMethodInfo.Create (type_methods, LogicalMethodTypes.Sync);
  291. foreach (LogicalMethodInfo mi in methods){
  292. MethodStubInfo msi = MethodStubInfo.Create (this, mi, xmlImporter, soapImporter);
  293. if (msi == null)
  294. continue;
  295. name_to_method [msi.Name] = msi;
  296. }
  297. }
  298. internal TypeStubInfo (Type t)
  299. {
  300. GetTypeAttributes (t);
  301. XmlReflectionImporter xmlImporter = new XmlReflectionImporter ();
  302. SoapReflectionImporter soapImporter = new SoapReflectionImporter ();
  303. GetTypeMethods (t, xmlImporter, soapImporter);
  304. }
  305. internal MethodStubInfo GetMethod (string name)
  306. {
  307. return (MethodStubInfo) name_to_method [name];
  308. }
  309. internal XmlSerializer GetCommonSerializer (Type type)
  310. {
  311. XmlSerializer s = (XmlSerializer) common_serializers [type];
  312. if (s != null) return s;
  313. s = new XmlSerializer (type);
  314. common_serializers [type] = s;
  315. return s;
  316. }
  317. }
  318. //
  319. // Manages
  320. //
  321. internal class TypeStubManager {
  322. static Hashtable type_to_manager;
  323. static TypeStubManager ()
  324. {
  325. type_to_manager = new Hashtable ();
  326. }
  327. //
  328. // This needs to be thread safe
  329. //
  330. static internal TypeStubInfo GetTypeStub (Type t)
  331. {
  332. TypeStubInfo tm = (TypeStubInfo) type_to_manager [t];
  333. if (tm != null)
  334. return tm;
  335. lock (typeof (TypeStubInfo)){
  336. tm = (TypeStubInfo) type_to_manager [t];
  337. if (tm != null)
  338. return tm;
  339. tm = new TypeStubInfo (t);
  340. type_to_manager [t] = tm;
  341. return tm;
  342. }
  343. }
  344. }
  345. }