HttpSimpleWebServiceHandler.cs 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. //
  2. // System.Web.Services.Protocols.HttpSimpleWebServiceHandler.cs
  3. //
  4. // Author:
  5. // Lluis Sanchez Gual ([email protected])
  6. //
  7. // Copyright (C) Ximian, Inc. 2003
  8. //
  9. //
  10. // Permission is hereby granted, free of charge, to any person obtaining
  11. // a copy of this software and associated documentation files (the
  12. // "Software"), to deal in the Software without restriction, including
  13. // without limitation the rights to use, copy, modify, merge, publish,
  14. // distribute, sublicense, and/or sell copies of the Software, and to
  15. // permit persons to whom the Software is furnished to do so, subject to
  16. // the following conditions:
  17. //
  18. // The above copyright notice and this permission notice shall be
  19. // included in all copies or substantial portions of the Software.
  20. //
  21. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  22. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  23. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  24. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  25. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  26. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  27. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  28. //
  29. using System;
  30. using System.Reflection;
  31. using System.IO;
  32. using System.Web.Services;
  33. namespace System.Web.Services.Protocols
  34. {
  35. internal class HttpSimpleWebServiceHandler: WebServiceHandler
  36. {
  37. HttpSimpleTypeStubInfo _typeInfo;
  38. HttpSimpleMethodStubInfo method;
  39. public HttpSimpleWebServiceHandler (Type type, string protocolName)
  40. : base (type)
  41. {
  42. _typeInfo = (HttpSimpleTypeStubInfo) TypeStubManager.GetTypeStub (type, protocolName);
  43. }
  44. protected HttpSimpleTypeStubInfo TypeStub
  45. {
  46. get { return _typeInfo; }
  47. }
  48. internal override MethodStubInfo GetRequestMethod (HttpContext context)
  49. {
  50. string name = context.Request.PathInfo;
  51. if (name.StartsWith ("/"))
  52. name = name.Substring (1);
  53. method = (HttpSimpleMethodStubInfo) _typeInfo.GetMethod (name);
  54. if (method == null)
  55. WriteError (context, "Method " + name + " not defined in service " + ServiceType.Name);
  56. return method;
  57. }
  58. public override void ProcessRequest (HttpContext context)
  59. {
  60. Context = context;
  61. Stream respStream = null;
  62. Exception error = null;
  63. try
  64. {
  65. if (method == null)
  66. method = (HttpSimpleMethodStubInfo) GetRequestMethod (context);
  67. if (method.MethodInfo.EnableSession)
  68. Session = context.Session;
  69. MimeParameterReader parameterReader = (MimeParameterReader) method.ParameterReaderType.Create ();
  70. object[] parameters = parameterReader.Read (context.Request);
  71. MimeReturnWriter returnWriter = (MimeReturnWriter) method.ReturnWriterType.Create ();
  72. object result = Invoke (method.MethodInfo, parameters);
  73. respStream = context.Response.OutputStream;
  74. returnWriter.Write (context.Response, respStream, result);
  75. }
  76. catch (Exception ex)
  77. {
  78. error = ex;
  79. }
  80. if (error != null)
  81. WriteError (context, error.Message);
  82. if (respStream != null)
  83. respStream.Close ();
  84. }
  85. void WriteError (HttpContext context, string msg)
  86. {
  87. try
  88. {
  89. context.Response.ContentType = "text/plain";
  90. context.Response.StatusCode = 500;
  91. context.Response.Write (msg);
  92. context.Response.End ();
  93. }
  94. catch {}
  95. }
  96. object Invoke (LogicalMethodInfo method, object[] parameters)
  97. {
  98. try
  99. {
  100. object server = CreateServerInstance ();
  101. object[] res = method.Invoke (server, parameters);
  102. if (!method.IsVoid) return res[0];
  103. else return null;
  104. }
  105. catch (TargetInvocationException ex)
  106. {
  107. throw ex.InnerException;
  108. }
  109. }
  110. }
  111. }