SoapServerFormatterSink.cs 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. //
  2. // System.Runtime.Remoting.Channels.SoapServerFormatterSink.cs
  3. //
  4. // Authors: Duncan Mak ([email protected])
  5. // Jean-Marc Andre ([email protected])
  6. //
  7. // 2002 (C) Copyright, Ximian, Inc.
  8. //
  9. using System.Collections;
  10. using System.IO;
  11. using System.Reflection;
  12. using System.Runtime.Remoting.Messaging;
  13. using System.Runtime.Serialization;
  14. using System.Runtime.Serialization.Formatters;
  15. using System.Runtime.Serialization.Formatters.Soap;
  16. namespace System.Runtime.Remoting.Channels {
  17. /// <summary>
  18. // The formatter sink that uses SoapFormatter
  19. /// </summary>
  20. // <remarks>
  21. // The formatter sink deserializes the message from the channel sink
  22. // and passes the result to the remoting infrastructure
  23. // </remark>
  24. //
  25. public class SoapServerFormatterSink : IServerChannelSink, IChannelSinkBase
  26. {
  27. IServerChannelSink next_sink;
  28. IChannelReceiver _receiver;
  29. private SoapFormatter _serializationFormatter;
  30. private SoapFormatter _deserializationFormatter;
  31. public SoapServerFormatterSink (SoapServerFormatterSink.Protocol protocol,
  32. IServerChannelSink nextSink,
  33. IChannelReceiver receiver)
  34. {
  35. this.next_sink = nextSink;
  36. _receiver = receiver;
  37. RemotingSurrogateSelector surrogateSelector = new RemotingSurrogateSelector();
  38. StreamingContext context = new StreamingContext(StreamingContextStates.Other);
  39. _serializationFormatter = new SoapFormatter(surrogateSelector, context);
  40. _deserializationFormatter = new SoapFormatter(null, context);
  41. }
  42. /// <summary>
  43. // Gets the next channel sink in the channel sink chain
  44. // </summary>
  45. /// <value>
  46. // The next channel sink in the sink chain
  47. // </value>
  48. public IServerChannelSink NextChannelSink {
  49. get {
  50. return next_sink;
  51. }
  52. }
  53. public IDictionary Properties {
  54. get {
  55. return null;
  56. }
  57. }
  58. public void AsyncProcessResponse (IServerResponseChannelSinkStack sinkStack, object state,
  59. IMessage msg, ITransportHeaders headers, Stream stream)
  60. {
  61. ITransportHeaders responseHeaders = new TransportHeaders();
  62. if(sinkStack != null) stream = sinkStack.GetResponseStream(msg, responseHeaders);
  63. if(stream == null) stream = new MemoryStream();
  64. SoapMessageFormatter soapMsgFormatter = (SoapMessageFormatter)state;
  65. SoapMessage soapMessage = (SoapMessage) soapMsgFormatter.BuildSoapMessageFromMethodResponse((IMethodReturnMessage)msg, out responseHeaders);
  66. _serializationFormatter.Serialize(stream, soapMessage, null);
  67. if(stream is MemoryStream) stream.Position = 0;
  68. }
  69. public Stream GetResponseStream (IServerResponseChannelSinkStack sinkStack, object state,
  70. IMessage msg, ITransportHeaders headers)
  71. {
  72. // this method shouldn't be called
  73. throw new NotSupportedException ();
  74. }
  75. public ServerProcessing ProcessMessage (IServerChannelSinkStack sinkStack,
  76. IMessage requestMsg, ITransportHeaders requestHeaders, Stream requestStream,
  77. out IMessage responseMsg, out ITransportHeaders responseHeaders, out Stream responseStream)
  78. {
  79. responseMsg = null;
  80. responseHeaders = null;
  81. responseStream = null;
  82. string url = (string)requestHeaders[CommonTransportKeys.RequestUri];
  83. string uri;
  84. _receiver.Parse(url, out uri);
  85. if(uri == null) uri = url;
  86. Type serverType = RemotingServices.GetServerTypeForUri(uri);
  87. SoapMessage soapMessage = new SoapMessage();
  88. _deserializationFormatter.TopObject = soapMessage;
  89. ServerProcessing sp;
  90. object rtnMessageObject;
  91. SoapMessageFormatter soapMsgFormatter = new SoapMessageFormatter();
  92. requestStream.Position = 0;
  93. _deserializationFormatter.Deserialize(requestStream);
  94. requestMsg = soapMsgFormatter.BuildMethodCallFromSoapMessage(soapMessage, uri);
  95. sinkStack.Push(this, soapMsgFormatter);
  96. try{
  97. sp = next_sink.ProcessMessage(sinkStack, requestMsg, requestHeaders, null, out responseMsg, out responseHeaders, out responseStream);
  98. }
  99. catch(Exception e) {
  100. responseMsg = (IMethodReturnMessage)new ReturnMessage(e, (IMethodCallMessage)requestMsg);
  101. sp = ServerProcessing.Complete;
  102. }
  103. if(sp == ServerProcessing.Complete) {
  104. if(responseMsg != null && responseStream == null) {
  105. rtnMessageObject = soapMsgFormatter.BuildSoapMessageFromMethodResponse((IMethodReturnMessage) responseMsg, out responseHeaders);
  106. responseStream = new MemoryStream();
  107. _serializationFormatter.Serialize(responseStream, rtnMessageObject);
  108. }
  109. sinkStack.Pop(this);
  110. }
  111. return sp;
  112. }
  113. private object HH(Header[] headers) {
  114. foreach(Header h in headers) {
  115. Console.WriteLine("Name: {0} Value:{0}", h.Name, h.Value);
  116. }
  117. return null;
  118. }
  119. [Serializable]
  120. public enum Protocol
  121. {
  122. Http = 0,
  123. Other = 1,
  124. }
  125. }
  126. }