2
0

Methods.cs 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475
  1. //
  2. // Methods.cs: Information about a method and its mapping to a SOAP web service.
  3. //
  4. // Author:
  5. // Miguel de Icaza
  6. // Lluis Sanchez Gual ([email protected])
  7. //
  8. // (C) 2003 Ximian, Inc.
  9. //
  10. using System.Reflection;
  11. using System.Collections;
  12. using System.Xml;
  13. using System.Xml.Serialization;
  14. using System.Web.Services;
  15. using System.Web.Services.Description;
  16. namespace System.Web.Services.Protocols {
  17. //
  18. // This class represents all the information we extract from a MethodInfo
  19. // in the SoapHttpClientProtocol derivative stub class
  20. //
  21. internal class SoapMethodStubInfo : MethodStubInfo
  22. {
  23. internal string Action;
  24. internal string Binding;
  25. // The name/namespace of the request
  26. internal string RequestName;
  27. internal string RequestNamespace;
  28. // The name/namespace of the response.
  29. internal string ResponseName;
  30. internal string ResponseNamespace;
  31. internal bool OneWay;
  32. internal SoapParameterStyle ParameterStyle;
  33. internal SoapBindingStyle SoapBindingStyle;
  34. internal SoapBindingUse Use;
  35. internal HeaderInfo[] Headers;
  36. internal SoapExtensionRuntimeConfig[] SoapExtensions;
  37. internal XmlMembersMapping InputMembersMapping;
  38. internal XmlMembersMapping OutputMembersMapping;
  39. private int requestSerializerId;
  40. private int responseSerializerId;
  41. internal XmlSerializer RequestSerializer
  42. {
  43. get { return TypeStub.GetSerializer (requestSerializerId); }
  44. }
  45. internal XmlSerializer ResponseSerializer
  46. {
  47. get { return TypeStub.GetSerializer (responseSerializerId); }
  48. }
  49. //
  50. // Constructor
  51. //
  52. public SoapMethodStubInfo (TypeStubInfo typeStub, LogicalMethodInfo source, object kind, XmlReflectionImporter xmlImporter, SoapReflectionImporter soapImporter)
  53. : base (typeStub, source)
  54. {
  55. SoapTypeStubInfo parent = (SoapTypeStubInfo) typeStub;
  56. XmlElementAttribute optional_ns = null;
  57. if (kind == null) {
  58. Use = parent.Use;
  59. RequestName = "";
  60. RequestNamespace = "";
  61. ResponseName = "";
  62. ResponseNamespace = "";
  63. ParameterStyle = parent.ParameterStyle;
  64. SoapBindingStyle = parent.SoapBindingStyle;
  65. OneWay = false;
  66. }
  67. else if (kind is SoapDocumentMethodAttribute){
  68. SoapDocumentMethodAttribute dma = (SoapDocumentMethodAttribute) kind;
  69. Use = dma.Use;
  70. if (Use == SoapBindingUse.Default) {
  71. if (parent.SoapBindingStyle == SoapBindingStyle.Document)
  72. Use = parent.Use;
  73. else
  74. Use = SoapBindingUse.Literal;
  75. }
  76. Action = dma.Action;
  77. Binding = dma.Binding;
  78. RequestName = dma.RequestElementName;
  79. RequestNamespace = dma.RequestNamespace;
  80. ResponseName = dma.ResponseElementName;
  81. ResponseNamespace = dma.ResponseNamespace;
  82. ParameterStyle = dma.ParameterStyle;
  83. if (ParameterStyle == SoapParameterStyle.Default)
  84. ParameterStyle = parent.ParameterStyle;
  85. OneWay = dma.OneWay;
  86. SoapBindingStyle = SoapBindingStyle.Document;
  87. } else {
  88. SoapRpcMethodAttribute rma = (SoapRpcMethodAttribute) kind;
  89. Use = SoapBindingUse.Encoded; // RPC always use encoded
  90. Action = rma.Action;
  91. Binding = rma.Binding;
  92. RequestName = rma.RequestElementName;
  93. RequestNamespace = rma.RequestNamespace;
  94. ResponseNamespace = rma.ResponseNamespace;
  95. ResponseName = rma.ResponseElementName;
  96. ParameterStyle = SoapParameterStyle.Wrapped;
  97. OneWay = rma.OneWay;
  98. SoapBindingStyle = SoapBindingStyle.Rpc;
  99. // For RPC calls, make all arguments be part of the empty namespace
  100. optional_ns = new XmlElementAttribute ();
  101. optional_ns.Namespace = "";
  102. }
  103. if (OneWay){
  104. if (source.ReturnType != typeof (void))
  105. throw new Exception ("OneWay methods should not have a return value");
  106. if (source.OutParameters.Length != 0)
  107. throw new Exception ("OneWay methods should not have out/ref parameters");
  108. }
  109. BindingInfo binfo = parent.GetBinding (Binding);
  110. if (binfo == null) throw new InvalidOperationException ("Type '" + parent.Type + "' is missing WebServiceBinding attribute that defines a binding named '" + Binding + "'");
  111. string serviceNamespace = binfo.Namespace;
  112. if (RequestNamespace == "") RequestNamespace = parent.LogicalType.GetWebServiceNamespace (serviceNamespace, Use);
  113. if (ResponseNamespace == "") ResponseNamespace = parent.LogicalType.GetWebServiceNamespace (serviceNamespace, Use);
  114. if (RequestName == "") RequestName = Name;
  115. if (ResponseName == "") ResponseName = Name + "Response";
  116. if (Action == null || Action == "")
  117. Action = RequestNamespace.EndsWith("/") ? (RequestNamespace + Name) : (RequestNamespace + "/" + Name);
  118. bool hasWrappingElem = (ParameterStyle == SoapParameterStyle.Wrapped);
  119. bool writeAccessors = (SoapBindingStyle == SoapBindingStyle.Rpc);
  120. XmlReflectionMember [] in_members = BuildRequestReflectionMembers (optional_ns);
  121. XmlReflectionMember [] out_members = BuildResponseReflectionMembers (optional_ns);
  122. if (Use == SoapBindingUse.Literal) {
  123. InputMembersMapping = xmlImporter.ImportMembersMapping (RequestName, RequestNamespace, in_members, hasWrappingElem);
  124. OutputMembersMapping = xmlImporter.ImportMembersMapping (ResponseName, ResponseNamespace, out_members, hasWrappingElem);
  125. }
  126. else {
  127. InputMembersMapping = soapImporter.ImportMembersMapping (RequestName, RequestNamespace, in_members, hasWrappingElem, writeAccessors);
  128. OutputMembersMapping = soapImporter.ImportMembersMapping (ResponseName, ResponseNamespace, out_members, hasWrappingElem, writeAccessors);
  129. }
  130. requestSerializerId = parent.RegisterSerializer (InputMembersMapping);
  131. responseSerializerId = parent.RegisterSerializer (OutputMembersMapping);
  132. object[] o = source.GetCustomAttributes (typeof (SoapHeaderAttribute));
  133. Headers = new HeaderInfo[o.Length];
  134. for (int i = 0; i < o.Length; i++) {
  135. SoapHeaderAttribute att = (SoapHeaderAttribute) o[i];
  136. MemberInfo[] mems = source.DeclaringType.GetMember (att.MemberName);
  137. if (mems.Length == 0) throw new InvalidOperationException ("Member " + att.MemberName + " not found in class " + source.DeclaringType.FullName);
  138. Type headerType = (mems[0] is FieldInfo) ? ((FieldInfo)mems[0]).FieldType : ((PropertyInfo)mems[0]).PropertyType;
  139. Headers [i] = new HeaderInfo (mems[0], att);
  140. parent.RegisterHeaderType (headerType, serviceNamespace, Use);
  141. }
  142. SoapExtensions = SoapExtension.GetMethodExtensions (source);
  143. }
  144. XmlReflectionMember [] BuildRequestReflectionMembers (XmlElementAttribute optional_ns)
  145. {
  146. ParameterInfo [] input = MethodInfo.InParameters;
  147. XmlReflectionMember [] in_members = new XmlReflectionMember [input.Length];
  148. for (int i = 0; i < input.Length; i++)
  149. {
  150. XmlReflectionMember m = new XmlReflectionMember ();
  151. m.IsReturnValue = false;
  152. m.MemberName = input [i].Name;
  153. m.MemberType = input [i].ParameterType;
  154. m.XmlAttributes = new XmlAttributes (input[i]);
  155. m.SoapAttributes = new SoapAttributes (input[i]);
  156. if (m.MemberType.IsByRef)
  157. m.MemberType = m.MemberType.GetElementType ();
  158. if (optional_ns != null)
  159. m.XmlAttributes.XmlElements.Add (optional_ns);
  160. in_members [i] = m;
  161. }
  162. return in_members;
  163. }
  164. XmlReflectionMember [] BuildResponseReflectionMembers (XmlElementAttribute optional_ns)
  165. {
  166. ParameterInfo [] output = MethodInfo.OutParameters;
  167. bool has_return_value = !(OneWay || MethodInfo.ReturnType == typeof (void));
  168. XmlReflectionMember [] out_members = new XmlReflectionMember [(has_return_value ? 1 : 0) + output.Length];
  169. XmlReflectionMember m;
  170. int idx = 0;
  171. if (has_return_value)
  172. {
  173. m = new XmlReflectionMember ();
  174. m.IsReturnValue = true;
  175. m.MemberName = RequestName + "Result";
  176. m.MemberType = MethodInfo.ReturnType;
  177. m.XmlAttributes = new XmlAttributes (MethodInfo.ReturnTypeCustomAttributeProvider);
  178. m.SoapAttributes = new SoapAttributes (MethodInfo.ReturnTypeCustomAttributeProvider);
  179. if (optional_ns != null)
  180. m.XmlAttributes.XmlElements.Add (optional_ns);
  181. idx++;
  182. out_members [0] = m;
  183. }
  184. for (int i = 0; i < output.Length; i++)
  185. {
  186. m = new XmlReflectionMember ();
  187. m.IsReturnValue = false;
  188. m.MemberName = output [i].Name;
  189. m.MemberType = output [i].ParameterType;
  190. m.XmlAttributes = new XmlAttributes (output[i]);
  191. m.SoapAttributes = new SoapAttributes (output[i]);
  192. if (m.MemberType.IsByRef)
  193. m.MemberType = m.MemberType.GetElementType ();
  194. if (optional_ns != null)
  195. m.XmlAttributes.XmlElements.Add (optional_ns);
  196. out_members [i + idx] = m;
  197. }
  198. return out_members;
  199. }
  200. public HeaderInfo GetHeaderInfo (Type headerType)
  201. {
  202. foreach (HeaderInfo headerInfo in Headers)
  203. if (headerInfo.HeaderType == headerType) return headerInfo;
  204. return null;
  205. }
  206. }
  207. internal class HeaderInfo
  208. {
  209. internal MemberInfo Member;
  210. internal SoapHeaderAttribute AttributeInfo;
  211. internal Type HeaderType;
  212. public HeaderInfo (MemberInfo member, SoapHeaderAttribute attributeInfo)
  213. {
  214. Member = member;
  215. AttributeInfo = attributeInfo;
  216. if (Member is PropertyInfo) HeaderType = ((PropertyInfo)Member).PropertyType;
  217. else HeaderType = ((FieldInfo)Member).FieldType;
  218. }
  219. public object GetHeaderValue (object ob)
  220. {
  221. if (Member is PropertyInfo) return ((PropertyInfo)Member).GetValue (ob, null);
  222. else return ((FieldInfo)Member).GetValue (ob);
  223. }
  224. public void SetHeaderValue (object ob, object value)
  225. {
  226. if (Member is PropertyInfo) ((PropertyInfo)Member).SetValue (ob, value, null);
  227. else ((FieldInfo)Member).SetValue (ob, value);
  228. }
  229. public SoapHeaderDirection Direction
  230. {
  231. get { return AttributeInfo.Direction; }
  232. }
  233. }
  234. // FIXME: this class should be internal, but it needs to be public in
  235. // order to be serialized using XmlSerializer.
  236. [XmlRoot (Namespace="http://schemas.xmlsoap.org/soap/envelope/")]
  237. public class Fault
  238. {
  239. public Fault () {}
  240. public Fault (SoapException ex)
  241. {
  242. faultcode = ex.Code;
  243. faultstring = ex.Message;
  244. faultactor = ex.Actor;
  245. detail = ex.Detail;
  246. }
  247. [XmlElement (Namespace="")]
  248. public XmlQualifiedName faultcode;
  249. [XmlElement (Namespace="")]
  250. public string faultstring;
  251. [XmlElement (Namespace="")]
  252. public string faultactor;
  253. [SoapIgnore]
  254. public XmlNode detail;
  255. }
  256. //
  257. // Holds the metadata loaded from the type stub, as well as
  258. // the metadata for all the methods in the type
  259. //
  260. internal class SoapTypeStubInfo : TypeStubInfo
  261. {
  262. Hashtable[] header_serializers = new Hashtable [3];
  263. Hashtable[] header_serializers_byname = new Hashtable [3];
  264. Hashtable methods_byaction = new Hashtable ();
  265. // Precomputed
  266. internal SoapParameterStyle ParameterStyle;
  267. internal SoapServiceRoutingStyle RoutingStyle;
  268. internal SoapBindingUse Use;
  269. internal int faultSerializerId = -1;
  270. internal SoapExtensionRuntimeConfig[][] SoapExtensions;
  271. internal SoapBindingStyle SoapBindingStyle;
  272. internal XmlReflectionImporter xmlImporter;
  273. internal SoapReflectionImporter soapImporter;
  274. public SoapTypeStubInfo (LogicalTypeInfo logicalTypeInfo)
  275. : base (logicalTypeInfo)
  276. {
  277. xmlImporter = new XmlReflectionImporter ();
  278. soapImporter = new SoapReflectionImporter ();
  279. object [] o;
  280. o = Type.GetCustomAttributes (typeof (WebServiceBindingAttribute), false);
  281. foreach (WebServiceBindingAttribute at in o)
  282. AddBinding (new BindingInfo (at, LogicalType.WebServiceNamespace));
  283. o = Type.GetCustomAttributes (typeof (SoapDocumentServiceAttribute), false);
  284. if (o.Length == 1){
  285. SoapDocumentServiceAttribute a = (SoapDocumentServiceAttribute) o [0];
  286. ParameterStyle = a.ParameterStyle;
  287. RoutingStyle = a.RoutingStyle;
  288. Use = a.Use;
  289. SoapBindingStyle = SoapBindingStyle.Document;
  290. } else {
  291. o = Type.GetCustomAttributes (typeof (SoapRpcServiceAttribute), false);
  292. if (o.Length == 1){
  293. SoapRpcServiceAttribute srs = (SoapRpcServiceAttribute) o [0];
  294. ParameterStyle = SoapParameterStyle.Wrapped;
  295. RoutingStyle = srs.RoutingStyle;
  296. Use = SoapBindingUse.Encoded;
  297. SoapBindingStyle = SoapBindingStyle.Rpc;
  298. } else {
  299. ParameterStyle = SoapParameterStyle.Wrapped;
  300. RoutingStyle = SoapServiceRoutingStyle.SoapAction;
  301. Use = SoapBindingUse.Literal;
  302. SoapBindingStyle = SoapBindingStyle.Document;
  303. }
  304. }
  305. if (ParameterStyle == SoapParameterStyle.Default) ParameterStyle = SoapParameterStyle.Wrapped;
  306. if (Use == SoapBindingUse.Default) Use = SoapBindingUse.Literal;
  307. SoapExtensions = SoapExtension.GetTypeExtensions (Type);
  308. }
  309. public override XmlReflectionImporter XmlImporter
  310. {
  311. get { return xmlImporter; }
  312. }
  313. public override SoapReflectionImporter SoapImporter
  314. {
  315. get { return soapImporter; }
  316. }
  317. public override string ProtocolName
  318. {
  319. get { return "Soap"; }
  320. }
  321. protected override MethodStubInfo CreateMethodStubInfo (TypeStubInfo parent, LogicalMethodInfo lmi, bool isClientProxy)
  322. {
  323. SoapMethodStubInfo res = null;
  324. object [] ats = lmi.GetCustomAttributes (typeof (SoapDocumentMethodAttribute));
  325. if (ats.Length == 0) ats = lmi.GetCustomAttributes (typeof (SoapRpcMethodAttribute));
  326. if (ats.Length == 0 && isClientProxy)
  327. return null;
  328. else if (ats.Length == 0)
  329. res = new SoapMethodStubInfo (parent, lmi, null, xmlImporter, soapImporter);
  330. else
  331. res = new SoapMethodStubInfo (parent, lmi, ats[0], xmlImporter, soapImporter);
  332. if (faultSerializerId == -1)
  333. {
  334. XmlReflectionImporter ri = new XmlReflectionImporter ();
  335. XmlTypeMapping tm = ri.ImportTypeMapping (typeof(Fault));
  336. faultSerializerId = RegisterSerializer (tm);
  337. }
  338. methods_byaction [res.Action] = res;
  339. return res;
  340. }
  341. public XmlSerializer GetFaultSerializer ()
  342. {
  343. return GetSerializer (faultSerializerId);
  344. }
  345. internal void RegisterHeaderType (Type type, string serviceNamespace, SoapBindingUse use)
  346. {
  347. Hashtable serializers = header_serializers [(int)use];
  348. if (serializers == null) {
  349. serializers = new Hashtable ();
  350. header_serializers [(int)use] = serializers;
  351. header_serializers_byname [(int)use] = new Hashtable ();
  352. }
  353. if (serializers.ContainsKey (type))
  354. return;
  355. XmlTypeMapping tm;
  356. if (use == SoapBindingUse.Literal) {
  357. XmlReflectionImporter ri = new XmlReflectionImporter ();
  358. // MS.NET reflects header classes in a weird way. The root element
  359. // name is the CLR class name unless it is specified in an XmlRootAttribute.
  360. // The usual is to use the xml type name by default, but not in this case.
  361. XmlRootAttribute root;
  362. XmlAttributes ats = new XmlAttributes (type);
  363. if (ats.XmlRoot != null) root = ats.XmlRoot;
  364. else root = new XmlRootAttribute (type.Name);
  365. if (root.Namespace == null) root.Namespace = LogicalType.GetWebServiceLiteralNamespace (serviceNamespace);
  366. if (root.ElementName == null) root.ElementName = type.Name;
  367. tm = ri.ImportTypeMapping (type, root);
  368. }
  369. else {
  370. SoapReflectionImporter ri = new SoapReflectionImporter ();
  371. tm = ri.ImportTypeMapping (type, LogicalType.GetWebServiceEncodedNamespace (serviceNamespace));
  372. }
  373. int sid = RegisterSerializer (tm);
  374. serializers [type] = sid;
  375. header_serializers_byname [(int)use] [new XmlQualifiedName (tm.ElementName, tm.Namespace)] = sid;
  376. }
  377. internal XmlSerializer GetHeaderSerializer (Type type, SoapBindingUse use)
  378. {
  379. Hashtable table = header_serializers [(int)use];
  380. if (table == null) return null;
  381. return GetSerializer ((int) table [type]);
  382. }
  383. internal XmlSerializer GetHeaderSerializer (XmlQualifiedName qname, SoapBindingUse use)
  384. {
  385. Hashtable table = header_serializers_byname [(int)use];
  386. if (table == null) return null;
  387. return GetSerializer ((int) table [qname]);
  388. }
  389. public SoapMethodStubInfo GetMethodForSoapAction (string name)
  390. {
  391. return (SoapMethodStubInfo) methods_byaction [name.Trim ('"',' ')];
  392. }
  393. }
  394. }