ProgrammaticEndpointTrait.cs 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. //------------------------------------------------------------
  2. // Copyright (c) Microsoft Corporation. All rights reserved.
  3. //------------------------------------------------------------
  4. namespace System.ServiceModel
  5. {
  6. using System.Runtime;
  7. using System.ServiceModel.Channels;
  8. using System.ServiceModel.Description;
  9. sealed class ProgrammaticEndpointTrait<TChannel> : EndpointTrait<TChannel>
  10. where TChannel : class
  11. {
  12. EndpointAddress remoteAddress;
  13. Binding binding;
  14. InstanceContext callbackInstance;
  15. public ProgrammaticEndpointTrait(Binding binding,
  16. EndpointAddress remoteAddress,
  17. InstanceContext callbackInstance)
  18. : base()
  19. {
  20. this.binding = binding;
  21. this.remoteAddress = remoteAddress;
  22. this.callbackInstance = callbackInstance;
  23. }
  24. public override bool Equals(object obj)
  25. {
  26. ProgrammaticEndpointTrait<TChannel> trait1 = obj as ProgrammaticEndpointTrait<TChannel>;
  27. if (trait1 == null) return false;
  28. if (!object.ReferenceEquals(this.callbackInstance, trait1.callbackInstance))
  29. return false;
  30. // EndpointAddress.Equals is used.
  31. if (this.remoteAddress != trait1.remoteAddress)
  32. return false;
  33. if (!object.ReferenceEquals(this.binding, trait1.binding))
  34. return false;
  35. return true;
  36. }
  37. public override int GetHashCode()
  38. {
  39. int hashCode = 0;
  40. if (this.callbackInstance != null)
  41. {
  42. hashCode ^= this.callbackInstance.GetHashCode();
  43. }
  44. Fx.Assert(this.remoteAddress != null, "remoteAddress should not be null.");
  45. hashCode ^= this.remoteAddress.GetHashCode();
  46. Fx.Assert(this.binding != null, "binding should not be null.");
  47. hashCode ^= this.binding.GetHashCode();
  48. return hashCode;
  49. }
  50. public override ChannelFactory<TChannel> CreateChannelFactory()
  51. {
  52. if (this.callbackInstance != null)
  53. return CreateDuplexFactory();
  54. return CreateSimplexFactory();
  55. }
  56. DuplexChannelFactory<TChannel> CreateDuplexFactory()
  57. {
  58. Fx.Assert(this.remoteAddress != null, "remoteAddress should not be null.");
  59. Fx.Assert(this.binding != null, "binding should not be null.");
  60. return new DuplexChannelFactory<TChannel>(this.callbackInstance, this.binding, this.remoteAddress);
  61. }
  62. ChannelFactory<TChannel> CreateSimplexFactory()
  63. {
  64. Fx.Assert(this.remoteAddress != null, "remoteAddress should not be null.");
  65. Fx.Assert(this.binding != null, "binding should not be null.");
  66. return new ChannelFactory<TChannel>(this.binding, this.remoteAddress);
  67. }
  68. }
  69. }