Methods.cs 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456
  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. if (RequestNamespace == "") RequestNamespace = parent.LogicalType.GetWebServiceNamespace (Use);
  110. if (ResponseNamespace == "") ResponseNamespace = parent.LogicalType.GetWebServiceNamespace (Use);
  111. if (RequestName == "") RequestName = Name;
  112. if (ResponseName == "") ResponseName = Name + "Response";
  113. if (Binding == null || Binding == "") Binding = parent.DefaultBinding;
  114. else if (parent.GetBinding (Binding) == null) throw new InvalidOperationException ("Type '" + parent.Type + "' is missing WebServiceBinding attribute that defines a binding named '" + Binding + "'");
  115. if (Action == null || Action == "")
  116. Action = RequestNamespace.EndsWith("/") ? (RequestNamespace + Name) : (RequestNamespace + "/" + Name);
  117. bool hasWrappingElem = (ParameterStyle == SoapParameterStyle.Wrapped);
  118. bool writeAccessors = (SoapBindingStyle == SoapBindingStyle.Rpc);
  119. XmlReflectionMember [] in_members = BuildRequestReflectionMembers (optional_ns);
  120. XmlReflectionMember [] out_members = BuildResponseReflectionMembers (optional_ns);
  121. if (Use == SoapBindingUse.Literal) {
  122. InputMembersMapping = xmlImporter.ImportMembersMapping (RequestName, RequestNamespace, in_members, hasWrappingElem);
  123. OutputMembersMapping = xmlImporter.ImportMembersMapping (ResponseName, ResponseNamespace, out_members, hasWrappingElem);
  124. }
  125. else {
  126. InputMembersMapping = soapImporter.ImportMembersMapping (RequestName, RequestNamespace, in_members, hasWrappingElem, writeAccessors);
  127. OutputMembersMapping = soapImporter.ImportMembersMapping (ResponseName, ResponseNamespace, out_members, hasWrappingElem, writeAccessors);
  128. }
  129. requestSerializerId = parent.RegisterSerializer (InputMembersMapping);
  130. responseSerializerId = parent.RegisterSerializer (OutputMembersMapping);
  131. object[] o = source.GetCustomAttributes (typeof (SoapHeaderAttribute));
  132. Headers = new HeaderInfo[o.Length];
  133. for (int i = 0; i < o.Length; i++) {
  134. SoapHeaderAttribute att = (SoapHeaderAttribute) o[i];
  135. MemberInfo[] mems = source.DeclaringType.GetMember (att.MemberName);
  136. if (mems.Length == 0) throw new InvalidOperationException ("Member " + att.MemberName + " not found in class " + source.DeclaringType.FullName);
  137. Type headerType = (mems[0] is FieldInfo) ? ((FieldInfo)mems[0]).FieldType : ((PropertyInfo)mems[0]).PropertyType;
  138. Headers [i] = new HeaderInfo (mems[0], att);
  139. parent.RegisterHeaderType (headerType, Use);
  140. }
  141. SoapExtensions = SoapExtension.GetMethodExtensions (source);
  142. }
  143. XmlReflectionMember [] BuildRequestReflectionMembers (XmlElementAttribute optional_ns)
  144. {
  145. ParameterInfo [] input = MethodInfo.InParameters;
  146. XmlReflectionMember [] in_members = new XmlReflectionMember [input.Length];
  147. for (int i = 0; i < input.Length; i++)
  148. {
  149. XmlReflectionMember m = new XmlReflectionMember ();
  150. m.IsReturnValue = false;
  151. m.MemberName = input [i].Name;
  152. m.MemberType = input [i].ParameterType;
  153. m.XmlAttributes = new XmlAttributes (input[i]);
  154. m.SoapAttributes = new SoapAttributes (input[i]);
  155. if (m.MemberType.IsByRef)
  156. m.MemberType = m.MemberType.GetElementType ();
  157. if (optional_ns != null)
  158. m.XmlAttributes.XmlElements.Add (optional_ns);
  159. in_members [i] = m;
  160. }
  161. return in_members;
  162. }
  163. XmlReflectionMember [] BuildResponseReflectionMembers (XmlElementAttribute optional_ns)
  164. {
  165. ParameterInfo [] output = MethodInfo.OutParameters;
  166. bool has_return_value = !(OneWay || MethodInfo.ReturnType == typeof (void));
  167. XmlReflectionMember [] out_members = new XmlReflectionMember [(has_return_value ? 1 : 0) + output.Length];
  168. XmlReflectionMember m;
  169. int idx = 0;
  170. if (has_return_value)
  171. {
  172. m = new XmlReflectionMember ();
  173. m.IsReturnValue = true;
  174. m.MemberName = RequestName + "Result";
  175. m.MemberType = MethodInfo.ReturnType;
  176. m.XmlAttributes = new XmlAttributes (MethodInfo.ReturnTypeCustomAttributeProvider);
  177. m.SoapAttributes = new SoapAttributes (MethodInfo.ReturnTypeCustomAttributeProvider);
  178. if (optional_ns != null)
  179. m.XmlAttributes.XmlElements.Add (optional_ns);
  180. idx++;
  181. out_members [0] = m;
  182. }
  183. for (int i = 0; i < output.Length; i++)
  184. {
  185. m = new XmlReflectionMember ();
  186. m.IsReturnValue = false;
  187. m.MemberName = output [i].Name;
  188. m.MemberType = output [i].ParameterType;
  189. m.XmlAttributes = new XmlAttributes (output[i]);
  190. m.SoapAttributes = new SoapAttributes (output[i]);
  191. if (m.MemberType.IsByRef)
  192. m.MemberType = m.MemberType.GetElementType ();
  193. if (optional_ns != null)
  194. m.XmlAttributes.XmlElements.Add (optional_ns);
  195. out_members [i + idx] = m;
  196. }
  197. return out_members;
  198. }
  199. public HeaderInfo GetHeaderInfo (Type headerType)
  200. {
  201. foreach (HeaderInfo headerInfo in Headers)
  202. if (headerInfo.HeaderType == headerType) return headerInfo;
  203. return null;
  204. }
  205. }
  206. internal class HeaderInfo
  207. {
  208. internal MemberInfo Member;
  209. internal SoapHeaderAttribute AttributeInfo;
  210. internal Type HeaderType;
  211. public HeaderInfo (MemberInfo member, SoapHeaderAttribute attributeInfo)
  212. {
  213. Member = member;
  214. AttributeInfo = attributeInfo;
  215. if (Member is PropertyInfo) HeaderType = ((PropertyInfo)Member).PropertyType;
  216. else HeaderType = ((FieldInfo)Member).FieldType;
  217. }
  218. public object GetHeaderValue (object ob)
  219. {
  220. if (Member is PropertyInfo) return ((PropertyInfo)Member).GetValue (ob, null);
  221. else return ((FieldInfo)Member).GetValue (ob);
  222. }
  223. public void SetHeaderValue (object ob, object value)
  224. {
  225. if (Member is PropertyInfo) ((PropertyInfo)Member).SetValue (ob, value, null);
  226. else ((FieldInfo)Member).SetValue (ob, value);
  227. }
  228. public SoapHeaderDirection Direction
  229. {
  230. get { return AttributeInfo.Direction; }
  231. }
  232. }
  233. // FIXME: this class should be internal, but it needs to be public in
  234. // order to be serialized using XmlSerializer.
  235. [SoapType (Namespace="http://schemas.xmlsoap.org/soap/envelope/")]
  236. [XmlType (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. public XmlQualifiedName faultcode;
  248. public string faultstring;
  249. public string faultactor;
  250. [SoapIgnore]
  251. public XmlNode detail;
  252. }
  253. //
  254. // Holds the metadata loaded from the type stub, as well as
  255. // the metadata for all the methods in the type
  256. //
  257. internal class SoapTypeStubInfo : TypeStubInfo
  258. {
  259. Hashtable[] header_serializers = new Hashtable [3];
  260. Hashtable[] header_serializers_byname = new Hashtable [3];
  261. Hashtable methods_byaction = new Hashtable ();
  262. // Precomputed
  263. internal SoapParameterStyle ParameterStyle;
  264. internal SoapServiceRoutingStyle RoutingStyle;
  265. internal SoapBindingUse Use;
  266. internal int faultSerializerId = -1;
  267. internal SoapExtensionRuntimeConfig[][] SoapExtensions;
  268. internal SoapBindingStyle SoapBindingStyle;
  269. internal XmlReflectionImporter xmlImporter;
  270. internal SoapReflectionImporter soapImporter;
  271. public SoapTypeStubInfo (LogicalTypeInfo logicalTypeInfo)
  272. : base (logicalTypeInfo)
  273. {
  274. xmlImporter = new XmlReflectionImporter ();
  275. soapImporter = new SoapReflectionImporter ();
  276. object [] o;
  277. o = Type.GetCustomAttributes (typeof (WebServiceBindingAttribute), false);
  278. foreach (WebServiceBindingAttribute at in o)
  279. Bindings.Add (new BindingInfo (at, LogicalType.WebServiceNamespace));
  280. o = Type.GetCustomAttributes (typeof (SoapDocumentServiceAttribute), false);
  281. if (o.Length == 1){
  282. SoapDocumentServiceAttribute a = (SoapDocumentServiceAttribute) o [0];
  283. ParameterStyle = a.ParameterStyle;
  284. RoutingStyle = a.RoutingStyle;
  285. Use = a.Use;
  286. SoapBindingStyle = SoapBindingStyle.Document;
  287. } else {
  288. o = Type.GetCustomAttributes (typeof (SoapRpcServiceAttribute), false);
  289. if (o.Length == 1){
  290. SoapRpcServiceAttribute srs = (SoapRpcServiceAttribute) o [0];
  291. ParameterStyle = SoapParameterStyle.Wrapped;
  292. RoutingStyle = srs.RoutingStyle;
  293. Use = SoapBindingUse.Encoded;
  294. SoapBindingStyle = SoapBindingStyle.Rpc;
  295. } else {
  296. ParameterStyle = SoapParameterStyle.Wrapped;
  297. RoutingStyle = SoapServiceRoutingStyle.SoapAction;
  298. Use = SoapBindingUse.Literal;
  299. SoapBindingStyle = SoapBindingStyle.Document;
  300. }
  301. }
  302. if (ParameterStyle == SoapParameterStyle.Default) ParameterStyle = SoapParameterStyle.Wrapped;
  303. if (Use == SoapBindingUse.Default) Use = SoapBindingUse.Literal;
  304. SoapExtensions = SoapExtension.GetTypeExtensions (Type);
  305. }
  306. public override XmlReflectionImporter XmlImporter
  307. {
  308. get { return xmlImporter; }
  309. }
  310. public override SoapReflectionImporter SoapImporter
  311. {
  312. get { return soapImporter; }
  313. }
  314. public override string ProtocolName
  315. {
  316. get { return "Soap"; }
  317. }
  318. protected override MethodStubInfo CreateMethodStubInfo (TypeStubInfo parent, LogicalMethodInfo lmi, bool isClientProxy)
  319. {
  320. SoapMethodStubInfo res = null;
  321. object [] ats = lmi.GetCustomAttributes (typeof (SoapDocumentMethodAttribute));
  322. if (ats.Length == 0) ats = lmi.GetCustomAttributes (typeof (SoapRpcMethodAttribute));
  323. if (ats.Length == 0 && isClientProxy)
  324. return null;
  325. else if (ats.Length == 0)
  326. res = new SoapMethodStubInfo (parent, lmi, null, xmlImporter, soapImporter);
  327. else
  328. res = new SoapMethodStubInfo (parent, lmi, ats[0], xmlImporter, soapImporter);
  329. if (faultSerializerId == -1)
  330. {
  331. XmlReflectionImporter ri = new XmlReflectionImporter ();
  332. XmlTypeMapping tm = ri.ImportTypeMapping (typeof(Fault));
  333. faultSerializerId = RegisterSerializer (tm);
  334. }
  335. methods_byaction [res.Action] = res;
  336. return res;
  337. }
  338. public XmlSerializer GetFaultSerializer ()
  339. {
  340. return GetSerializer (faultSerializerId);
  341. }
  342. internal void RegisterHeaderType (Type type, SoapBindingUse use)
  343. {
  344. Hashtable serializers = header_serializers [(int)use];
  345. if (serializers == null) {
  346. serializers = new Hashtable ();
  347. header_serializers [(int)use] = serializers;
  348. header_serializers_byname [(int)use] = new Hashtable ();
  349. }
  350. if (serializers.ContainsKey (type))
  351. return;
  352. XmlTypeMapping tm;
  353. if (use == SoapBindingUse.Literal) {
  354. XmlReflectionImporter ri = new XmlReflectionImporter ();
  355. tm = ri.ImportTypeMapping (type, WebServiceAttribute.DefaultNamespace);
  356. }
  357. else {
  358. SoapReflectionImporter ri = new SoapReflectionImporter ();
  359. tm = ri.ImportTypeMapping (type, WebServiceAttribute.DefaultNamespace);
  360. }
  361. int sid = RegisterSerializer (tm);
  362. serializers [type] = sid;
  363. header_serializers_byname [(int)use] [new XmlQualifiedName (tm.ElementName, tm.Namespace)] = sid;
  364. }
  365. internal XmlSerializer GetHeaderSerializer (Type type, SoapBindingUse use)
  366. {
  367. Hashtable table = header_serializers [(int)use];
  368. if (table == null) return null;
  369. return GetSerializer ((int) table [type]);
  370. }
  371. internal XmlSerializer GetHeaderSerializer (XmlQualifiedName qname, SoapBindingUse use)
  372. {
  373. Hashtable table = header_serializers_byname [(int)use];
  374. if (table == null) return null;
  375. return GetSerializer ((int) table [qname]);
  376. }
  377. public SoapMethodStubInfo GetMethodForSoapAction (string name)
  378. {
  379. return (SoapMethodStubInfo) methods_byaction [name.Trim ('"',' ')];
  380. }
  381. }
  382. }