EndpointDispatcher.cs 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. //
  2. // EndpointDispatcher.cs
  3. //
  4. // Author:
  5. // Atsushi Enomoto <[email protected]>
  6. //
  7. // Copyright (C) 2005-2010 Novell, Inc. http://www.novell.com
  8. //
  9. // Permission is hereby granted, free of charge, to any person obtaining
  10. // a copy of this software and associated documentation files (the
  11. // "Software"), to deal in the Software without restriction, including
  12. // without limitation the rights to use, copy, modify, merge, publish,
  13. // distribute, sublicense, and/or sell copies of the Software, and to
  14. // permit persons to whom the Software is furnished to do so, subject to
  15. // the following conditions:
  16. //
  17. // The above copyright notice and this permission notice shall be
  18. // included in all copies or substantial portions of the Software.
  19. //
  20. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  21. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  22. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  23. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  24. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  25. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  26. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  27. //
  28. using System;
  29. using System.Collections.Generic;
  30. using System.Collections.ObjectModel;
  31. using System.Reflection;
  32. using System.ServiceModel.Description;
  33. using System.ServiceModel.Channels;
  34. using System.ServiceModel.Dispatcher;
  35. using System.ServiceModel.Security.Tokens;
  36. using System.Text;
  37. namespace System.ServiceModel.Dispatcher
  38. {
  39. public class EndpointDispatcher
  40. {
  41. EndpointAddress address;
  42. string contract_name, contract_ns;
  43. ChannelDispatcher channel_dispatcher;
  44. MessageFilter address_filter;
  45. MessageFilter contract_filter;
  46. int filter_priority;
  47. DispatchRuntime dispatch_runtime;
  48. // Umm, this API is ugly, since it or its members will
  49. // anyways require ServiceEndpoint, those arguments are
  50. // likely to be replaced by ServiceEndpoint (especially
  51. // considering about possible EndpointAddress inconsistency).
  52. public EndpointDispatcher (EndpointAddress address,
  53. string contractName, string contractNamespace)
  54. {
  55. if (contractName == null)
  56. throw new ArgumentNullException ("contractName");
  57. if (contractNamespace == null)
  58. throw new ArgumentNullException ("contractNamespace");
  59. if (address == null)
  60. throw new ArgumentNullException ("address");
  61. this.address = address;
  62. contract_name = contractName;
  63. contract_ns = contractNamespace;
  64. dispatch_runtime = new DispatchRuntime (this, null);
  65. this.address_filter = new EndpointAddressMessageFilter (address);
  66. }
  67. public DispatchRuntime DispatchRuntime {
  68. get { return dispatch_runtime; }
  69. }
  70. public string ContractName {
  71. get { return contract_name; }
  72. }
  73. public string ContractNamespace {
  74. get { return contract_ns; }
  75. }
  76. public ChannelDispatcher ChannelDispatcher {
  77. get { return channel_dispatcher; }
  78. internal set { channel_dispatcher = value; }
  79. }
  80. public MessageFilter AddressFilter {
  81. get { return address_filter; }
  82. set {
  83. if (value == null)
  84. throw new ArgumentNullException ("value");
  85. address_filter = value;
  86. }
  87. }
  88. public MessageFilter ContractFilter {
  89. get { return contract_filter ?? (contract_filter = new MatchAllMessageFilter ()); }
  90. set {
  91. if (value == null)
  92. throw new ArgumentNullException ("value");
  93. contract_filter = value;
  94. }
  95. }
  96. public EndpointAddress EndpointAddress {
  97. get { return address; }
  98. }
  99. public int FilterPriority {
  100. get { return filter_priority; }
  101. set { filter_priority = value; }
  102. }
  103. internal void InitializeServiceEndpoint (bool isCallback, Type serviceType, ServiceEndpoint se)
  104. {
  105. this.ContractFilter = GetContractFilter (se.Contract);
  106. this.DispatchRuntime.Type = serviceType;
  107. //Build the dispatch operations
  108. DispatchRuntime db = this.DispatchRuntime;
  109. if (!isCallback && se.Contract.CallbackContractType != null) {
  110. var ccd = se.Contract;
  111. db.CallbackClientRuntime.CallbackClientType = se.Contract.CallbackContractType;
  112. db.CallbackClientRuntime.ContractClientType = se.Contract.ContractType;
  113. ccd.FillClientOperations (db.CallbackClientRuntime, true);
  114. }
  115. foreach (OperationDescription od in se.Contract.Operations)
  116. if (!db.Operations.Contains (od.Name))
  117. PopulateDispatchOperation (db, od);
  118. }
  119. void PopulateDispatchOperation (DispatchRuntime db, OperationDescription od) {
  120. string reqA = null, resA = null;
  121. foreach (MessageDescription m in od.Messages) {
  122. if (m.IsRequest)
  123. reqA = m.Action;
  124. else
  125. resA = m.Action;
  126. }
  127. DispatchOperation o =
  128. od.IsOneWay ?
  129. new DispatchOperation (db, od.Name, reqA) :
  130. new DispatchOperation (db, od.Name, reqA, resA);
  131. bool no_serialized_reply = od.IsOneWay;
  132. foreach (MessageDescription md in od.Messages) {
  133. if (md.IsRequest &&
  134. md.Body.Parts.Count == 1 &&
  135. md.Body.Parts [0].Type == typeof (Message))
  136. o.DeserializeRequest = false;
  137. if (!md.IsRequest &&
  138. md.Body.ReturnValue != null) {
  139. if (md.Body.ReturnValue.Type == typeof (Message))
  140. o.SerializeReply = false;
  141. else if (md.Body.ReturnValue.Type == typeof (void))
  142. no_serialized_reply = true;
  143. }
  144. }
  145. foreach (var fd in od.Faults)
  146. o.FaultContractInfos.Add (new FaultContractInfo (fd.Action, fd.DetailType));
  147. // Setup Invoker
  148. o.Invoker = new DefaultOperationInvoker (od);
  149. // Setup Formater
  150. // FIXME: this seems to be null at initializing, and should be set after applying all behaviors.
  151. // I leave this as is to not regress and it's cosmetic compatibility to fix.
  152. // FIXME: pass correct isRpc, isEncoded
  153. o.Formatter = new OperationFormatter (od, false, false);
  154. if (o.Action == "*" && (o.IsOneWay || o.ReplyAction == "*")) {
  155. //Signature : Message (Message)
  156. // : void (Message)
  157. //FIXME: void (IChannel)
  158. if (!o.DeserializeRequest && (!o.SerializeReply || no_serialized_reply)) // what is this double-ish check for?
  159. db.UnhandledDispatchOperation = o;
  160. }
  161. db.Operations.Add (o);
  162. }
  163. MessageFilter GetContractFilter (ContractDescription cd)
  164. {
  165. List<string> actions = new List<string> ();
  166. foreach (var od in cd.Operations)
  167. foreach (var md in od.Messages)
  168. if (md.IsRequest)
  169. if (md.Action == "*")
  170. return new MatchAllMessageFilter ();
  171. else
  172. actions.Add (md.Action);
  173. return new ActionMessageFilter (actions.ToArray ());
  174. }
  175. }
  176. }