ConfigurationEndpointTrait.cs 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  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 ConfigurationEndpointTrait<TChannel> : EndpointTrait<TChannel>
  10. where TChannel : class
  11. {
  12. string endpointConfigurationName;
  13. EndpointAddress remoteAddress;
  14. InstanceContext callbackInstance;
  15. public ConfigurationEndpointTrait(string endpointConfigurationName,
  16. EndpointAddress remoteAddress,
  17. InstanceContext callbackInstance)
  18. {
  19. this.endpointConfigurationName = endpointConfigurationName;
  20. this.remoteAddress = remoteAddress;
  21. this.callbackInstance = callbackInstance;
  22. }
  23. public override bool Equals(object obj)
  24. {
  25. ConfigurationEndpointTrait<TChannel> trait1 = obj as ConfigurationEndpointTrait<TChannel>;
  26. if (trait1 == null) return false;
  27. if (!object.ReferenceEquals(this.callbackInstance, trait1.callbackInstance))
  28. return false;
  29. if (string.CompareOrdinal(this.endpointConfigurationName, trait1.endpointConfigurationName) != 0)
  30. {
  31. return false;
  32. }
  33. // EndpointAddress.Equals is used.
  34. if (this.remoteAddress != trait1.remoteAddress)
  35. return false;
  36. return true;
  37. }
  38. public override int GetHashCode()
  39. {
  40. int hashCode = 0;
  41. if (this.callbackInstance != null)
  42. {
  43. hashCode ^= this.callbackInstance.GetHashCode();
  44. }
  45. Fx.Assert(this.endpointConfigurationName != null, "endpointConfigurationName should not be null.");
  46. hashCode ^= this.endpointConfigurationName.GetHashCode();
  47. if (this.remoteAddress != null)
  48. {
  49. hashCode ^= this.remoteAddress.GetHashCode();
  50. }
  51. return hashCode;
  52. }
  53. public override ChannelFactory<TChannel> CreateChannelFactory()
  54. {
  55. if (this.callbackInstance != null)
  56. return CreateDuplexFactory();
  57. return CreateSimplexFactory();
  58. }
  59. DuplexChannelFactory<TChannel> CreateDuplexFactory()
  60. {
  61. if (this.remoteAddress != null)
  62. {
  63. return new DuplexChannelFactory<TChannel>(this.callbackInstance, this.endpointConfigurationName, this.remoteAddress);
  64. }
  65. return new DuplexChannelFactory<TChannel>(this.callbackInstance, this.endpointConfigurationName);
  66. }
  67. ChannelFactory<TChannel> CreateSimplexFactory()
  68. {
  69. if (this.remoteAddress != null)
  70. {
  71. return new ChannelFactory<TChannel>(this.endpointConfigurationName, this.remoteAddress);
  72. }
  73. return new ChannelFactory<TChannel>(this.endpointConfigurationName);
  74. }
  75. }
  76. }