ServiceEndpointTrait.cs 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  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 ServiceEndpointTrait<TChannel> : EndpointTrait<TChannel>
  10. where TChannel : class
  11. {
  12. InstanceContext callbackInstance;
  13. ServiceEndpoint endpoint;
  14. public ServiceEndpointTrait(ServiceEndpoint endpoint,
  15. InstanceContext callbackInstance)
  16. {
  17. this.endpoint = endpoint;
  18. this.callbackInstance = callbackInstance;
  19. }
  20. public override bool Equals(object obj)
  21. {
  22. ServiceEndpointTrait<TChannel> trait1 = obj as ServiceEndpointTrait<TChannel>;
  23. if (trait1 == null) return false;
  24. if (!object.ReferenceEquals(this.callbackInstance, trait1.callbackInstance))
  25. return false;
  26. if (!object.ReferenceEquals(this.endpoint, trait1.endpoint))
  27. return false;
  28. return true;
  29. }
  30. public override int GetHashCode()
  31. {
  32. int hashCode = 0;
  33. if (this.callbackInstance != null)
  34. {
  35. hashCode ^= this.callbackInstance.GetHashCode();
  36. }
  37. Fx.Assert(this.endpoint != null, "endpoint should not be null.");
  38. hashCode ^= this.endpoint.GetHashCode();
  39. return hashCode;
  40. }
  41. public override ChannelFactory<TChannel> CreateChannelFactory()
  42. {
  43. if (this.callbackInstance != null)
  44. return CreateDuplexFactory();
  45. return CreateSimplexFactory();
  46. }
  47. DuplexChannelFactory<TChannel> CreateDuplexFactory()
  48. {
  49. Fx.Assert(this.endpoint != null, "endpoint should not be null.");
  50. return new DuplexChannelFactory<TChannel>(this.callbackInstance, this.endpoint);
  51. }
  52. ChannelFactory<TChannel> CreateSimplexFactory()
  53. {
  54. Fx.Assert(this.endpoint != null, "endpoint should not be null.");
  55. return new ChannelFactory<TChannel>(this.endpoint);
  56. }
  57. }
  58. }