BaseRequestProcessor.cs 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  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. namespace System.ServiceModel.Dispatcher
  9. {
  10. internal class BaseRequestProcessor
  11. {
  12. HandlersChain initialize_handlers_chain = new HandlersChain();
  13. HandlersChain process_handlers_chain = new HandlersChain ();
  14. HandlersChain error_handlers_chain = new HandlersChain ();
  15. HandlersChain finalize_handlers_chain = new HandlersChain ();
  16. protected BaseRequestProcessor () { }
  17. protected virtual void ProcessRequest (MessageProcessingContext mrc)
  18. {
  19. initialize_handlers_chain.ProcessRequestChain (mrc);
  20. using (new OperationContextScope (mrc.OperationContext)) {
  21. try {
  22. process_handlers_chain.ProcessRequestChain (mrc);
  23. }
  24. catch (Exception e) {
  25. // FIXME: this is not really expected use of ChannelDispatcher.ErrorHandlers.
  26. // They are now correctly used in process_handler_chain (namely OperationInvokerHandler).
  27. // For this kind of "outsider" exceptions are actually left thrown
  28. // (and could even cause server loop crash in .NET).
  29. Console.WriteLine ("Exception " + e.Message + " " + e.StackTrace);
  30. mrc.ProcessingException = e;
  31. error_handlers_chain.ProcessRequestChain (mrc);
  32. }
  33. finally {
  34. finalize_handlers_chain.ProcessRequestChain (mrc);
  35. }
  36. }
  37. }
  38. public HandlersChain InitializeChain
  39. {
  40. get { return initialize_handlers_chain; }
  41. }
  42. public HandlersChain ProcessingChain
  43. {
  44. get { return process_handlers_chain; }
  45. }
  46. public HandlersChain ErrorChain
  47. {
  48. get { return error_handlers_chain; }
  49. }
  50. public HandlersChain FinalizationChain
  51. {
  52. get { return finalize_handlers_chain; }
  53. }
  54. }
  55. internal class HandlersChain
  56. {
  57. BaseRequestProcessorHandler chain;
  58. public void ProcessRequestChain (MessageProcessingContext mrc)
  59. {
  60. if (chain != null)
  61. chain.ProcessRequestChain (mrc);
  62. }
  63. public HandlersChain AddHandler (BaseRequestProcessorHandler handler)
  64. {
  65. if (chain == null) {
  66. chain = handler;
  67. }
  68. else {
  69. BaseRequestProcessorHandler current = chain;
  70. while (current.Next != null)
  71. current = current.Next;
  72. current.Next = handler;
  73. }
  74. return this;
  75. }
  76. }
  77. }