OutputChannel.cs 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. //------------------------------------------------------------
  2. // Copyright (c) Microsoft Corporation. All rights reserved.
  3. //------------------------------------------------------------
  4. namespace System.ServiceModel.Channels
  5. {
  6. using System.Diagnostics;
  7. using System.Runtime.Diagnostics;
  8. using System.ServiceModel;
  9. using System.ServiceModel.Diagnostics;
  10. abstract class OutputChannel : ChannelBase, IOutputChannel
  11. {
  12. protected OutputChannel(ChannelManagerBase manager)
  13. : base(manager)
  14. {
  15. }
  16. public abstract EndpointAddress RemoteAddress { get; }
  17. public abstract Uri Via { get; }
  18. public IAsyncResult BeginSend(Message message, AsyncCallback callback, object state)
  19. {
  20. return this.BeginSend(message, this.DefaultSendTimeout, callback, state);
  21. }
  22. public IAsyncResult BeginSend(Message message, TimeSpan timeout, AsyncCallback callback, object state)
  23. {
  24. if (message == null)
  25. throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull("message");
  26. if (timeout < TimeSpan.Zero)
  27. throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(
  28. new ArgumentOutOfRangeException("timeout", timeout, SR.GetString(SR.SFxTimeoutOutOfRange0)));
  29. ThrowIfDisposedOrNotOpen();
  30. AddHeadersTo(message);
  31. this.EmitTrace(message);
  32. return OnBeginSend(message, timeout, callback, state);
  33. }
  34. public void EndSend(IAsyncResult result)
  35. {
  36. OnEndSend(result);
  37. }
  38. public override T GetProperty<T>()
  39. {
  40. if (typeof(T) == typeof(IOutputChannel))
  41. {
  42. return (T)(object)this;
  43. }
  44. T baseProperty = base.GetProperty<T>();
  45. if (baseProperty != null)
  46. {
  47. return baseProperty;
  48. }
  49. return default(T);
  50. }
  51. protected abstract void OnSend(Message message, TimeSpan timeout);
  52. protected abstract IAsyncResult OnBeginSend(Message message, TimeSpan timeout, AsyncCallback callback, object state);
  53. protected abstract void OnEndSend(IAsyncResult result);
  54. public void Send(Message message)
  55. {
  56. this.Send(message, this.DefaultSendTimeout);
  57. }
  58. public void Send(Message message, TimeSpan timeout)
  59. {
  60. if (message == null)
  61. throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull("message");
  62. if (timeout < TimeSpan.Zero)
  63. throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(
  64. new ArgumentOutOfRangeException("timeout", timeout, SR.GetString(SR.SFxTimeoutOutOfRange0)));
  65. ThrowIfDisposedOrNotOpen();
  66. AddHeadersTo(message);
  67. this.EmitTrace(message);
  68. OnSend(message, timeout);
  69. }
  70. protected virtual TraceRecord CreateSendTrace(Message message)
  71. {
  72. return MessageTransmitTraceRecord.CreateSendTraceRecord(message, this.RemoteAddress);
  73. }
  74. void EmitTrace(Message message)
  75. {
  76. if (DiagnosticUtility.ShouldTraceInformation)
  77. {
  78. TraceUtility.TraceEvent(TraceEventType.Information, TraceCode.MessageSent,
  79. SR.GetString(SR.TraceCodeMessageSent),
  80. this.CreateSendTrace(message), this, null);
  81. }
  82. }
  83. protected virtual void AddHeadersTo(Message message)
  84. {
  85. }
  86. }
  87. }