BaseRequestProcessor.cs 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ServiceModel;
  4. using System.ServiceModel.Channels;
  5. using System.ServiceModel.Security;
  6. using System.ServiceModel.Security.Tokens;
  7. using System.Text;
  8. using System.IO;
  9. using System.Xml;
  10. using System.Net.Sockets;
  11. namespace System.ServiceModel.Dispatcher
  12. {
  13. internal class BaseRequestProcessor
  14. {
  15. HandlersChain initialize_handlers_chain = new HandlersChain();
  16. HandlersChain process_handlers_chain = new HandlersChain ();
  17. HandlersChain error_handlers_chain = new HandlersChain ();
  18. HandlersChain finalize_handlers_chain = new HandlersChain ();
  19. protected BaseRequestProcessor () { }
  20. protected virtual void ProcessRequest (MessageProcessingContext mrc)
  21. {
  22. initialize_handlers_chain.ProcessRequestChain (mrc);
  23. using (new OperationContextScope (mrc.OperationContext)) {
  24. try {
  25. process_handlers_chain.ProcessRequestChain (mrc);
  26. } catch (IOException e) {
  27. // FIXME?: On dropped connection do not
  28. // dump a stacktrace, but should be safe
  29. // to dump a console message as in
  30. // default exception handler and
  31. // call error_handlers_chain
  32. Console.WriteLine ("I/O Error (Dropped Connection?): " + e.Message);
  33. mrc.ProcessingException = e;
  34. error_handlers_chain.ProcessRequestChain (mrc);
  35. } catch (SocketException e) {
  36. // FIXME?: On dropped connection do not
  37. // dump a stacktrace, but should be safe
  38. // to dump a console message as in
  39. // default exception handler and
  40. // call error_handlers_chain
  41. Console.WriteLine ("SocketExcpetion (Dropped Connection?): " + e.Message);
  42. mrc.ProcessingException = e;
  43. error_handlers_chain.ProcessRequestChain (mrc);
  44. } catch (XmlException e) {
  45. // FIXME?: On dropped connection do not
  46. // dump a stacktrace, but should be safe
  47. // to dump a console message as in
  48. // default exception handler and
  49. // call error_handlers_chain
  50. Console.WriteLine ("XmlException (Dropped Connection?): " + e.Message);
  51. mrc.ProcessingException = e;
  52. error_handlers_chain.ProcessRequestChain (mrc);
  53. } catch (Exception e) {
  54. // FIXME: this is not really expected use of ChannelDispatcher.ErrorHandlers.
  55. // They are now correctly used in process_handler_chain (namely OperationInvokerHandler).
  56. // For this kind of "outsider" exceptions are actually left thrown
  57. // (and could even cause server loop crash in .NET).
  58. Console.WriteLine ("Exception " + e.Message + " " + e.StackTrace);
  59. mrc.ProcessingException = e;
  60. error_handlers_chain.ProcessRequestChain (mrc);
  61. }
  62. finally {
  63. finalize_handlers_chain.ProcessRequestChain (mrc);
  64. }
  65. }
  66. }
  67. public HandlersChain InitializeChain
  68. {
  69. get { return initialize_handlers_chain; }
  70. }
  71. public HandlersChain ProcessingChain
  72. {
  73. get { return process_handlers_chain; }
  74. }
  75. public HandlersChain ErrorChain
  76. {
  77. get { return error_handlers_chain; }
  78. }
  79. public HandlersChain FinalizationChain
  80. {
  81. get { return finalize_handlers_chain; }
  82. }
  83. }
  84. internal class HandlersChain
  85. {
  86. BaseRequestProcessorHandler chain;
  87. public void ProcessRequestChain (MessageProcessingContext mrc)
  88. {
  89. if (chain != null)
  90. chain.ProcessRequestChain (mrc);
  91. }
  92. public HandlersChain AddHandler (BaseRequestProcessorHandler handler)
  93. {
  94. if (chain == null) {
  95. chain = handler;
  96. }
  97. else {
  98. BaseRequestProcessorHandler current = chain;
  99. while (current.Next != null)
  100. current = current.Next;
  101. current.Next = handler;
  102. }
  103. return this;
  104. }
  105. }
  106. }