ContractDescription.cs 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. //
  2. // ContractDescription.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;
  30. using System.Collections.Generic;
  31. using System.Collections.ObjectModel;
  32. using System.Net.Security;
  33. using System.Reflection;
  34. using System.Runtime.Serialization;
  35. using System.ServiceModel;
  36. using System.ServiceModel.Channels;
  37. using System.ServiceModel.Dispatcher;
  38. namespace System.ServiceModel.Description
  39. {
  40. public class ContractDescription
  41. {
  42. [MonoTODO]
  43. public static ContractDescription GetContract (
  44. Type contractType)
  45. {
  46. return ContractDescriptionGenerator.GetContract (contractType);
  47. }
  48. [MonoTODO]
  49. public static ContractDescription GetContract (
  50. Type contractType, object serviceImplementation)
  51. {
  52. return ContractDescriptionGenerator.GetContract (contractType, serviceImplementation);
  53. }
  54. [MonoTODO]
  55. public static ContractDescription GetContract (
  56. Type contractType, Type serviceType)
  57. {
  58. return ContractDescriptionGenerator.GetContract (contractType, serviceType);
  59. }
  60. OperationDescriptionCollection operations;
  61. KeyedByTypeCollection<IContractBehavior> behaviors;
  62. Type callback_contract_type, contract_type;
  63. string name, ns, config_name;
  64. ProtectionLevel protection_level;
  65. bool has_protection_level;
  66. SessionMode session;
  67. public ContractDescription (string name)
  68. : this (name, null)
  69. {
  70. }
  71. public ContractDescription (string name, string ns)
  72. {
  73. if (name == null)
  74. throw new ArgumentNullException ("name");
  75. if (name.Length == 0)
  76. throw new ArgumentOutOfRangeException ("ContractDescription's Name must be a non-empty string.");
  77. if (ns == null)
  78. ns = "http://tempuri.org/";
  79. this.name = name;
  80. this.ns = ns;
  81. behaviors = new KeyedByTypeCollection<IContractBehavior> ();
  82. operations = new OperationDescriptionCollection ();
  83. }
  84. public KeyedByTypeCollection<IContractBehavior> Behaviors {
  85. get { return behaviors; }
  86. }
  87. public Type CallbackContractType {
  88. get { return callback_contract_type; }
  89. set { callback_contract_type = value; }
  90. }
  91. public string ConfigurationName {
  92. get { return config_name; }
  93. set { config_name = value; }
  94. }
  95. public Type ContractType {
  96. get { return contract_type; }
  97. set { contract_type = value; }
  98. }
  99. public bool HasProtectionLevel {
  100. get { return has_protection_level; }
  101. }
  102. public ProtectionLevel ProtectionLevel {
  103. get { return protection_level; }
  104. set {
  105. protection_level = value;
  106. has_protection_level = true;
  107. }
  108. }
  109. public string Name {
  110. get { return name; }
  111. set { name = value; }
  112. }
  113. public string Namespace {
  114. get { return ns; }
  115. set { ns = value; }
  116. }
  117. public OperationDescriptionCollection Operations {
  118. get { return operations; }
  119. }
  120. public SessionMode SessionMode {
  121. get { return session; }
  122. set { session = value; }
  123. }
  124. [MonoTODO]
  125. public Collection<ContractDescription> GetInheritedContracts ()
  126. {
  127. throw new NotImplementedException ();
  128. }
  129. internal ClientRuntime CreateClientRuntime ()
  130. {
  131. ClientRuntime proxy = new ClientRuntime (Name, Namespace) { ContractClientType = ContractType, CallbackClientType = CallbackContractType };
  132. //proxy.ContractClientType = typeof (TChannel);
  133. foreach (OperationDescription od in Operations) {
  134. if (!proxy.Operations.Contains (od.Name))
  135. PopulateClientOperation (proxy, od);
  136. #if !NET_2_1
  137. foreach (IOperationBehavior ob in od.Behaviors)
  138. ob.ApplyClientBehavior (od, proxy.Operations [od.Name]);
  139. #endif
  140. }
  141. return proxy;
  142. }
  143. void PopulateClientOperation (ClientRuntime proxy, OperationDescription od)
  144. {
  145. string reqA = null, resA = null;
  146. foreach (MessageDescription m in od.Messages) {
  147. if (m.Direction == MessageDirection.Input)
  148. reqA = m.Action;
  149. else
  150. resA = m.Action;
  151. }
  152. ClientOperation o =
  153. od.IsOneWay ?
  154. new ClientOperation (proxy, od.Name, reqA) :
  155. new ClientOperation (proxy, od.Name, reqA, resA);
  156. foreach (MessageDescription md in od.Messages) {
  157. if (md.Direction == MessageDirection.Input &&
  158. md.Body.Parts.Count == 1 &&
  159. md.Body.Parts [0].Type == typeof (Message))
  160. o.SerializeRequest = false;
  161. if (md.Direction == MessageDirection.Output &&
  162. md.Body.ReturnValue != null &&
  163. md.Body.ReturnValue.Type == typeof (Message))
  164. o.DeserializeReply = false;
  165. }
  166. proxy.Operations.Add (o);
  167. }
  168. }
  169. }