Advertisement
Guest User

Untitled

a guest
Aug 22nd, 2014
231
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.27 KB | None | 0 0
  1. //Client Assembly:
  2. void WCFClientChannel()
  3. {
  4. var wcfClient = new ObjectManagerClient();
  5. var simpleObject = wcfClient.GetSimpleObject(0) as ISimpleObject;// this cath exeption
  6. simpleObject.TestCall();
  7. }
  8.  
  9. //Common Assembly:
  10. public interface ISimpleObject
  11. {
  12. string TestCall();
  13. }
  14.  
  15. [ServiceContract]
  16. public interface IObjectManager
  17. {
  18. [OperationContract]
  19. [ServiceKnownType(typeof(ISimpleObject))]
  20. ISimpleObject GetSimpleObject(int id);
  21. }
  22.  
  23.  
  24. //SimpleObject Assembly:
  25. [DataContract(Name = "SimpleObject")]
  26. [KnownType(typeof(ISimpleObject))]
  27. [ServiceBehavior(InstanceContextMode = InstanceContextMode.Single, IncludeExceptionDetailInFaults = true)]
  28. public class SimpleObject : MarshalByRefObject, ISimpleObject, IDisposable
  29. {
  30. public SimpleObject(int Id)
  31. {
  32. this.Id = Id;
  33. }
  34.  
  35. public string TestCall()
  36. {
  37. var watch = new Stopwatch();
  38. watch.Start();
  39. Console.Write("TestCall ");
  40. watch.Stop();
  41. var time = watch.Elapsed.ToString("ss\.ffffff") + " sec";
  42. Console.WriteLine(time);
  43. return string.Format("Time: {0}", time);
  44. }
  45.  
  46. [DataMember(Name = "Id")]
  47. public int Id { get; private set; }
  48.  
  49. public void Dispose()
  50. {
  51. //throw new NotImplementedException();
  52. }
  53.  
  54.  
  55. //ObjectManager Assembly:
  56. [KnownType(typeof(ObjectManager))]
  57. [DataContract(Name = "ObjectManager")]
  58. public class ObjectManager : SimpleObject, IObjectManager
  59. {
  60. public ObjectManager()
  61. {
  62. if (components == null || !components.Any())
  63. {
  64. components = new List<SimpleObject>
  65. {
  66. new SimpleObject(0),
  67. new SimpleObject(1),
  68. };
  69. }
  70. }
  71.  
  72. public ISimpleObject GetSimpleObject(int id)
  73. {
  74. if (components.Any(o => o.Id == id))
  75. {
  76. Console.WriteLine("Return SimpleObject with id {0}", id);
  77. ISimpleObject o = components[id];
  78. return o;
  79. }
  80.  
  81. throw new KeyNotFoundException(string.Format("can not find object with id = {0}", Id));
  82. }
  83.  
  84. private readonly List<SimpleObject> components;
  85. }
  86.  
  87. //Service Assembly:
  88. private void InitializeWCF()
  89. {
  90. Console.WriteLine("Opening WCF host...");
  91.  
  92. var baseAddress = new Uri("http://localhost:8000/ServiceManager");
  93. wcfHost = new ServiceHost(typeof(ObjectManager), baseAddress);
  94.  
  95. try
  96. {
  97. wcfHost.AddServiceEndpoint(
  98. typeof(IObjectManager),
  99. new WSHttpBinding()
  100. {
  101. ReceiveTimeout = TimeSpan.FromMinutes(15),
  102. OpenTimeout = TimeSpan.FromMinutes(1),
  103. SendTimeout = TimeSpan.FromMinutes(30),
  104. MaxReceivedMessageSize = Int32.MaxValue,
  105. Security = new WSHttpSecurity(){Transport = new HttpTransportSecurity(){ClientCredentialType = HttpClientCredentialType.Windows}}
  106. },
  107. "ObjectManager");
  108.  
  109. var smb = new ServiceMetadataBehavior { HttpGetEnabled = true };
  110. wcfHost.Description.Behaviors.Add(smb);
  111.  
  112. //System.Net.ServicePointManager.Expect100Continue = false;
  113.  
  114. wcfHost.Open();
  115.  
  116. Console.WriteLine("WCF host is ready.");
  117. }
  118. catch (CommunicationException ce)
  119. {
  120. Console.WriteLine("WCF host is failed. An exception occurred: {0}", ce.Message);
  121.  
  122. wcfHost.Abort();
  123. }
  124. finally
  125. {
  126. Console.WriteLine(Environment.NewLine);
  127. }
  128. }
  129.  
  130. <?xml version="1.0" encoding="utf-8"?>
  131. <configuration>
  132. <system.serviceModel>
  133. <bindings>
  134. <wsHttpBinding>
  135. <binding name="WSHttpBinding_IObjectManager" />
  136. </wsHttpBinding>
  137. </bindings>
  138. <client>
  139. <endpoint address="http://localhost:8000/ServiceManager/ObjectManager"
  140. binding="wsHttpBinding" bindingConfiguration="WSHttpBinding_IObjectManager"
  141. contract="IObjectManager" name="WSHttpBinding_IObjectManager">
  142. </endpoint>
  143. </client>
  144. </system.serviceModel>
  145. </configuration>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement