2
0

ServiceEndpoint.cs 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. //
  2. // ServiceEndpoint.cs
  3. //
  4. // Author:
  5. // Atsushi Enomoto <[email protected]>
  6. //
  7. // Copyright (C) 2005 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.ServiceModel;
  31. using System.ServiceModel.Channels;
  32. using System.ServiceModel.Dispatcher;
  33. namespace System.ServiceModel.Description
  34. {
  35. public class ServiceEndpoint
  36. {
  37. ContractDescription contract;
  38. Binding binding;
  39. EndpointAddress address;
  40. KeyedByTypeCollection<IEndpointBehavior> behaviors;
  41. Uri listen_uri;
  42. ListenUriMode listen_mode;
  43. string name;
  44. public ServiceEndpoint (ContractDescription contract)
  45. : this (contract, null, null)
  46. {
  47. }
  48. public ServiceEndpoint(ContractDescription contract,
  49. Binding binding, EndpointAddress address)
  50. {
  51. if (contract == null)
  52. throw new ArgumentNullException ("contract");
  53. this.contract = contract;
  54. this.binding = binding;
  55. this.address = address;
  56. behaviors = new KeyedByTypeCollection<IEndpointBehavior> ();
  57. }
  58. public KeyedByTypeCollection<IEndpointBehavior> Behaviors {
  59. get { return behaviors; }
  60. }
  61. public ContractDescription Contract {
  62. get { return contract; }
  63. }
  64. public EndpointAddress Address {
  65. get { return address; }
  66. set { address = value; }
  67. }
  68. public Binding Binding {
  69. get { return binding; }
  70. set { binding = value; }
  71. }
  72. public Uri ListenUri {
  73. get { return listen_uri; }
  74. set { listen_uri = value; }
  75. }
  76. public ListenUriMode ListenUriMode {
  77. get { return listen_mode; }
  78. set { listen_mode = value; }
  79. }
  80. public string Name {
  81. get {
  82. if (name == null)
  83. name = Binding.Name + "_" + Contract.Name;
  84. return name;
  85. }
  86. set { name = value; }
  87. }
  88. internal void Validate () {
  89. #if !NET_2_1
  90. foreach (IContractBehavior b in Contract.Behaviors)
  91. b.Validate (Contract, this);
  92. foreach (IEndpointBehavior b in Behaviors)
  93. b.Validate (this);
  94. foreach (OperationDescription operation in Contract.Operations) {
  95. foreach (IOperationBehavior b in operation.Behaviors)
  96. b.Validate (operation);
  97. }
  98. #endif
  99. }
  100. internal ClientRuntime CreateRuntime ()
  101. {
  102. ServiceEndpoint se = this;
  103. ClientRuntime proxy = new ClientRuntime (se);
  104. //proxy.ContractClientType = typeof (TChannel);
  105. foreach (OperationDescription od in se.Contract.Operations)
  106. if (!proxy.Operations.Contains (od.Name))
  107. PopulateClientOperation (proxy, od);
  108. #if !NET_2_1
  109. foreach (IEndpointBehavior b in se.Behaviors)
  110. b.ApplyClientBehavior (se, proxy);
  111. foreach (IContractBehavior b in se.Contract.Behaviors)
  112. b.ApplyClientBehavior (se.Contract, se, proxy);
  113. foreach (OperationDescription od in se.Contract.Operations)
  114. foreach (IOperationBehavior ob in od.Behaviors)
  115. ob.ApplyClientBehavior (od, proxy.Operations [od.Name]);
  116. #endif
  117. return proxy;
  118. }
  119. void PopulateClientOperation (ClientRuntime proxy, OperationDescription od)
  120. {
  121. string reqA = null, resA = null;
  122. foreach (MessageDescription m in od.Messages) {
  123. if (m.Direction == MessageDirection.Input)
  124. reqA = m.Action;
  125. else
  126. resA = m.Action;
  127. }
  128. ClientOperation o =
  129. od.IsOneWay ?
  130. new ClientOperation (proxy, od.Name, reqA) :
  131. new ClientOperation (proxy, od.Name, reqA, resA);
  132. foreach (MessageDescription md in od.Messages) {
  133. if (md.Direction == MessageDirection.Input &&
  134. md.Body.Parts.Count == 1 &&
  135. md.Body.Parts [0].Type == typeof (Message))
  136. o.SerializeRequest = false;
  137. if (md.Direction == MessageDirection.Output &&
  138. md.Body.ReturnValue != null &&
  139. md.Body.ReturnValue.Type == typeof (Message))
  140. o.DeserializeReply = false;
  141. }
  142. proxy.Operations.Add (o);
  143. }
  144. }
  145. }