HttpSimpleWebServiceHandler.cs 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  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. string name = context.Request.PathInfo;
  29. if (name.StartsWith ("/")) name = name.Substring (1);
  30. Stream respStream = null;
  31. Exception error = null;
  32. try
  33. {
  34. HttpSimpleMethodStubInfo method = (HttpSimpleMethodStubInfo) _typeInfo.GetMethod (name);
  35. if (method == null) throw new InvalidOperationException ("Method " + name + " not defined in service " + ServiceType.Name);
  36. MimeParameterReader parameterReader = (MimeParameterReader) method.ParameterReaderType.Create ();
  37. object[] parameters = parameterReader.Read (context.Request);
  38. MimeReturnWriter returnWriter = (MimeReturnWriter) method.ReturnWriterType.Create ();
  39. object result = Invoke (method.MethodInfo, parameters);
  40. respStream = context.Response.OutputStream;
  41. returnWriter.Write (context.Response, respStream, result);
  42. }
  43. catch (Exception ex)
  44. {
  45. error = ex;
  46. }
  47. if (error != null)
  48. {
  49. try
  50. {
  51. context.Response.ContentType = "text/plain";
  52. context.Response.StatusCode = 500;
  53. context.Response.Write (error.Message);
  54. }
  55. catch {}
  56. }
  57. if (respStream != null)
  58. respStream.Close ();
  59. }
  60. object Invoke (LogicalMethodInfo method, object[] parameters)
  61. {
  62. try
  63. {
  64. object server = CreateServerInstance ();
  65. object[] res = method.Invoke (server, parameters);
  66. if (!method.IsVoid) return res[0];
  67. else return null;
  68. }
  69. catch (TargetInvocationException ex)
  70. {
  71. throw ex.InnerException;
  72. }
  73. }
  74. }
  75. }