SoapMessage.cs 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. //
  2. // System.Web.Services.Protocols.SoapMessage.cs
  3. //
  4. // Author:
  5. // Tim Coleman ([email protected])
  6. // Lluis Sanchez Gual ([email protected])
  7. //
  8. // Copyright (C) Tim Coleman, 2002
  9. //
  10. using System.IO;
  11. using System.Web.Services;
  12. namespace System.Web.Services.Protocols {
  13. public abstract class SoapMessage {
  14. #region Fields
  15. string content_type = "text/xml";
  16. SoapException exception = null;
  17. SoapHeaderCollection headers;
  18. SoapMessageStage stage;
  19. Stream stream;
  20. object[] inParameters;
  21. object[] outParameters;
  22. #endregion // Fields
  23. #region Constructors
  24. internal SoapMessage ()
  25. {
  26. headers = new SoapHeaderCollection ();
  27. }
  28. internal SoapMessage (Stream stream)
  29. {
  30. this.stream = stream;
  31. }
  32. internal SoapMessage (Stream stream, SoapException exception)
  33. {
  34. this.exception = exception;
  35. this.stream = stream;
  36. headers = new SoapHeaderCollection ();
  37. }
  38. #endregion
  39. #region Properties
  40. internal object[] InParameters
  41. {
  42. get { return inParameters; }
  43. set { inParameters = value; }
  44. }
  45. internal object[] OutParameters
  46. {
  47. get { return outParameters; }
  48. set { outParameters = value; }
  49. }
  50. public abstract string Action
  51. {
  52. get;
  53. }
  54. public string ContentType {
  55. get { return content_type; }
  56. set { content_type = value; }
  57. }
  58. public SoapException Exception {
  59. get { return exception; }
  60. }
  61. public SoapHeaderCollection Headers {
  62. get { return headers; }
  63. }
  64. public abstract LogicalMethodInfo MethodInfo {
  65. get;
  66. }
  67. public abstract bool OneWay {
  68. get;
  69. }
  70. public SoapMessageStage Stage {
  71. get { return stage; }
  72. }
  73. internal void SetStage (SoapMessageStage stage)
  74. {
  75. this.stage = stage;
  76. }
  77. public Stream Stream {
  78. get {
  79. return stream;
  80. }
  81. }
  82. public abstract string Url {
  83. get;
  84. }
  85. #endregion Properties
  86. #region Methods
  87. protected abstract void EnsureInStage ();
  88. protected abstract void EnsureOutStage ();
  89. protected void EnsureStage (SoapMessageStage stage)
  90. {
  91. if ((((int) stage) & ((int) Stage)) == 0)
  92. throw new InvalidOperationException ("The current SoapMessageStage is not the asserted stage or stages.");
  93. }
  94. public object GetInParameterValue (int index)
  95. {
  96. return inParameters [index];
  97. }
  98. public object GetOutParameterValue (int index)
  99. {
  100. if (MethodInfo.IsVoid) return outParameters [index];
  101. else return outParameters [index + 1];
  102. }
  103. public object GetReturnValue ()
  104. {
  105. if (!MethodInfo.IsVoid && exception == null) return outParameters [0];
  106. else return null;
  107. }
  108. internal void SetHeaders (SoapHeaderCollection headers)
  109. {
  110. this.headers = headers;
  111. }
  112. internal void SetException (SoapException ex)
  113. {
  114. exception = ex;
  115. }
  116. internal void CollectHeaders (object target, HeaderInfo[] headers, SoapHeaderDirection direction)
  117. {
  118. Headers.Clear ();
  119. foreach (HeaderInfo hi in headers)
  120. {
  121. if ((hi.Direction & direction) != 0)
  122. {
  123. SoapHeader headerVal = hi.GetHeaderValue (target) as SoapHeader;
  124. if (headerVal != null)
  125. Headers.Add (headerVal);
  126. }
  127. }
  128. }
  129. internal void UpdateHeaderValues (object target, HeaderInfo[] headersInfo)
  130. {
  131. foreach (SoapHeader header in Headers)
  132. {
  133. HeaderInfo hinfo = FindHeader (headersInfo, header.GetType ());
  134. if (hinfo != null)
  135. hinfo.SetHeaderValue (target, header);
  136. else
  137. if (header.MustUnderstand)
  138. throw new SoapHeaderException ("Unknown header", SoapException.MustUnderstandFaultCode);
  139. header.DidUnderstand = false;
  140. }
  141. }
  142. HeaderInfo FindHeader (HeaderInfo[] headersInfo, Type headerType)
  143. {
  144. foreach (HeaderInfo headerInfo in headersInfo)
  145. if (headerInfo.HeaderType == headerType) return headerInfo;
  146. return null;
  147. }
  148. #endregion // Methods
  149. }
  150. }