HttpSimpleWebServiceHandler.cs 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  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. using System;
  10. using System.Reflection;
  11. using System.IO;
  12. using System.Web.Services;
  13. namespace System.Web.Services.Protocols
  14. {
  15. internal class HttpSimpleWebServiceHandler: WebServiceHandler
  16. {
  17. HttpSimpleTypeStubInfo _typeInfo;
  18. public HttpSimpleWebServiceHandler (Type type, string protocolName): base (type)
  19. {
  20. _typeInfo = (HttpSimpleTypeStubInfo) TypeStubManager.GetTypeStub (type, protocolName);
  21. }
  22. protected HttpSimpleTypeStubInfo TypeStub
  23. {
  24. get { return _typeInfo; }
  25. }
  26. public override void ProcessRequest (HttpContext context)
  27. {
  28. Context = context;
  29. string name = context.Request.PathInfo;
  30. if (name.StartsWith ("/")) name = name.Substring (1);
  31. Stream respStream = null;
  32. Exception error = null;
  33. try
  34. {
  35. HttpSimpleMethodStubInfo method = (HttpSimpleMethodStubInfo) _typeInfo.GetMethod (name);
  36. if (method == null) throw new InvalidOperationException ("Method " + name + " not defined in service " + ServiceType.Name);
  37. MimeParameterReader parameterReader = (MimeParameterReader) method.ParameterReaderType.Create ();
  38. object[] parameters = parameterReader.Read (context.Request);
  39. MimeReturnWriter returnWriter = (MimeReturnWriter) method.ReturnWriterType.Create ();
  40. object result = Invoke (method.MethodInfo, parameters);
  41. respStream = context.Response.OutputStream;
  42. returnWriter.Write (context.Response, respStream, result);
  43. }
  44. catch (Exception ex)
  45. {
  46. error = ex;
  47. }
  48. if (error != null)
  49. {
  50. try
  51. {
  52. context.Response.ContentType = "text/plain";
  53. context.Response.StatusCode = 500;
  54. context.Response.Write (error.Message);
  55. }
  56. catch {}
  57. }
  58. if (respStream != null)
  59. respStream.Close ();
  60. }
  61. object Invoke (LogicalMethodInfo method, object[] parameters)
  62. {
  63. try
  64. {
  65. object server = CreateServerInstance ();
  66. object[] res = method.Invoke (server, parameters);
  67. if (!method.IsVoid) return res[0];
  68. else return null;
  69. }
  70. catch (TargetInvocationException ex)
  71. {
  72. throw ex.InnerException;
  73. }
  74. }
  75. }
  76. }