OperationInvokerHandler.cs 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text;
  4. using System.ServiceModel.Channels;
  5. using System.ServiceModel;
  6. using System.Reflection;
  7. namespace System.ServiceModel.Dispatcher
  8. {
  9. internal class OperationInvokerHandler : BaseRequestProcessorHandler
  10. {
  11. protected override bool ProcessRequest (MessageProcessingContext mrc)
  12. {
  13. RequestContext rc = mrc.RequestContext;
  14. DispatchRuntime dispatchRuntime = mrc.OperationContext.EndpointDispatcher.DispatchRuntime;
  15. DispatchOperation operation = GetOperation (mrc.IncomingMessage, dispatchRuntime);
  16. mrc.Operation = operation;
  17. try {
  18. DoProcessRequest (mrc);
  19. if (!operation.Invoker.IsSynchronous)
  20. return true;
  21. } catch (TargetInvocationException ex) {
  22. mrc.ReplyMessage = BuildExceptionMessage (mrc.IncomingMessage, ex.InnerException,
  23. dispatchRuntime.ChannelDispatcher.IncludeExceptionDetailInFaults);
  24. }
  25. return false;
  26. }
  27. void DoProcessRequest (MessageProcessingContext mrc)
  28. {
  29. DispatchOperation operation = mrc.Operation;
  30. Message req = mrc.IncomingMessage;
  31. object instance = mrc.InstanceContext.GetServiceInstance(req);
  32. object [] parameters;
  33. BuildInvokeParams (mrc, out parameters);
  34. if (operation.Invoker.IsSynchronous) {
  35. object result = operation.Invoker.Invoke (instance, parameters);
  36. HandleInvokeResult (mrc, parameters, result);
  37. } else {// asynchronous
  38. InvokeAsynchronous (mrc, instance, parameters);
  39. }
  40. }
  41. void InvokeAsynchronous (MessageProcessingContext mrc, object instance, object [] parameters)
  42. {
  43. DispatchOperation operation = mrc.Operation;
  44. DispatchRuntime dispatchRuntime = mrc.OperationContext.EndpointDispatcher.DispatchRuntime;
  45. operation.Invoker.InvokeBegin (instance, parameters,
  46. delegate (IAsyncResult res) {
  47. try {
  48. object result;
  49. result = operation.Invoker.InvokeEnd (instance, out parameters, res);
  50. HandleInvokeResult (mrc, parameters, result);
  51. mrc.Reply (true);
  52. } catch (Exception ex) {
  53. mrc.ReplyMessage = BuildExceptionMessage (mrc.IncomingMessage, ex, dispatchRuntime.ChannelDispatcher.IncludeExceptionDetailInFaults);
  54. mrc.Reply (false);
  55. }
  56. },
  57. null);
  58. }
  59. DispatchOperation GetOperation (Message input, DispatchRuntime dispatchRuntime)
  60. {
  61. if (dispatchRuntime.OperationSelector != null) {
  62. string name = dispatchRuntime.OperationSelector.SelectOperation (ref input);
  63. foreach (DispatchOperation d in dispatchRuntime.Operations)
  64. if (d.Name == name)
  65. return d;
  66. } else {
  67. string action = input.Headers.Action;
  68. foreach (DispatchOperation d in dispatchRuntime.Operations)
  69. if (d.Action == action)
  70. return d;
  71. }
  72. return dispatchRuntime.UnhandledDispatchOperation;
  73. }
  74. void HandleInvokeResult (MessageProcessingContext mrc, object [] outputs, object result)
  75. {
  76. DispatchOperation operation = mrc.Operation;
  77. mrc.EventsHandler.AfterInvoke (operation);
  78. Message res = null;
  79. if (operation.SerializeReply)
  80. res = operation.Formatter.SerializeReply (
  81. mrc.OperationContext.EndpointDispatcher.ChannelDispatcher.MessageVersion, outputs, result);
  82. else
  83. res = (Message) result;
  84. res.Headers.CopyHeadersFrom (mrc.OperationContext.OutgoingMessageHeaders);
  85. res.Properties.CopyProperties (mrc.OperationContext.OutgoingMessageProperties);
  86. mrc.ReplyMessage = res;
  87. }
  88. Message CreateActionNotSupported (Message req)
  89. {
  90. FaultCode fc = new FaultCode (
  91. req.Version.Addressing.ActionNotSupported,
  92. req.Version.Addressing.Namespace);
  93. // FIXME: set correct namespace URI
  94. return Message.CreateMessage (req.Version, fc,
  95. String.Format ("action '{0}' is not supported in this service contract.", req.Headers.Action), String.Empty);
  96. }
  97. void BuildInvokeParams (MessageProcessingContext mrc, out object [] parameters)
  98. {
  99. DispatchOperation operation = mrc.Operation;
  100. EnsureValid (operation);
  101. if (operation.DeserializeRequest) {
  102. parameters = operation.Invoker.AllocateParameters ();
  103. operation.Formatter.DeserializeRequest (mrc.IncomingMessage, parameters);
  104. } else
  105. parameters = new object [] { mrc.IncomingMessage };
  106. mrc.EventsHandler.BeforeInvoke (operation);
  107. }
  108. Message BuildExceptionMessage (Message req, Exception ex, bool includeDetailsInFault)
  109. {
  110. // FIXME: set correct name
  111. FaultCode fc = new FaultCode (
  112. "InternalServiceFault",
  113. req.Version.Addressing.Namespace);
  114. if (includeDetailsInFault) {
  115. return Message.CreateMessage (req.Version, fc, ex.Message, new ExceptionDetail (ex), req.Headers.Action);
  116. }
  117. string faultString =
  118. @"The server was unable to process the request due to an internal error. For more information about the error, either turn on IncludeExceptionDetailInFaults (either from ServiceBehaviorAttribute or from the <serviceDebug> configuration behavior) on the server in order to send the exception information back to the client, or turn on tracing as per the Microsoft .NET Framework 3.0 SDK documentation and inspect the server trace logs.";
  119. return Message.CreateMessage (req.Version, fc, faultString, req.Headers.Action);
  120. }
  121. void EnsureValid (DispatchOperation operation)
  122. {
  123. if (operation.Invoker == null)
  124. throw new InvalidOperationException ("DispatchOperation requires Invoker.");
  125. if ((operation.DeserializeRequest || operation.SerializeReply) && operation.Formatter == null)
  126. throw new InvalidOperationException ("The DispatchOperation '" + operation.Name + "' requires Formatter, since DeserializeRequest and SerializeReply are not both false.");
  127. }
  128. }
  129. }