SoapFormatter.cs 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. // created on 07/04/2003 at 17:16
  2. //
  3. // System.Runtime.Serialization.Formatters.Soap.SoapFormatter
  4. //
  5. // Authors:
  6. // Jean-Marc Andre ([email protected])
  7. //
  8. using System;
  9. using System.IO;
  10. using System.Reflection;
  11. using System.Runtime.Remoting;
  12. using System.Runtime.Serialization;
  13. using System.Runtime.Remoting.Messaging;
  14. using System.Xml.Serialization;
  15. namespace System.Runtime.Serialization.Formatters.Soap {
  16. enum RemMessageType {
  17. MethodCall, MethodResponse, ServerFault, NotRecognize
  18. }
  19. public class SoapFormatter: IRemotingFormatter, IFormatter {
  20. private ObjectWriter _objWriter;
  21. private SoapWriter _soapWriter;
  22. private SerializationBinder _binder;
  23. private StreamingContext _context;
  24. private ISurrogateSelector _selector;
  25. private ISoapMessage _topObject;
  26. public SoapFormatter() {
  27. }
  28. public SoapFormatter(ISurrogateSelector selector, StreamingContext context):this() {
  29. _selector = selector;
  30. _context = context;
  31. }
  32. ~SoapFormatter() {
  33. }
  34. public object Deserialize(Stream serializationStream) {
  35. return Deserialize(serializationStream, null);
  36. }
  37. public object Deserialize(Stream serializationStream, HeaderHandler handler) {
  38. SoapParser parser = new SoapParser(serializationStream);
  39. SoapReader soapReader = new SoapReader(parser);
  40. soapReader.Binder = _binder;
  41. if(_topObject != null) soapReader.TopObject = _topObject;
  42. ObjectReader reader = new ObjectReader(_selector, _context, soapReader);
  43. parser.Run();
  44. object objReturn = reader.TopObject;
  45. return objReturn;
  46. }
  47. public void Serialize(Stream serializationStream, object graph) {
  48. Serialize(serializationStream, graph, null);
  49. }
  50. public void Serialize(Stream serializationStream, object graph, Header[] headers) {
  51. if(serializationStream == null)
  52. throw new ArgumentNullException("serializationStream");
  53. if(!serializationStream.CanWrite)
  54. throw new SerializationException("Can't write in the serialization stream");
  55. _soapWriter = new SoapWriter(serializationStream);
  56. _objWriter = new ObjectWriter((ISoapWriter) _soapWriter, _selector, new StreamingContext(StreamingContextStates.File));
  57. _soapWriter.Writer = _objWriter;
  58. _objWriter.Serialize(graph);
  59. }
  60. public ISurrogateSelector SurrogateSelector {
  61. get {
  62. return _selector;
  63. }
  64. set {
  65. _selector = value;
  66. }
  67. }
  68. public SerializationBinder Binder {
  69. get {
  70. return _binder;
  71. }
  72. set {
  73. _binder = value;
  74. }
  75. }
  76. public StreamingContext Context {
  77. get {
  78. return _context;
  79. }
  80. set {
  81. _context = value;
  82. }
  83. }
  84. public ISoapMessage TopObject {
  85. get {
  86. return _topObject;
  87. }
  88. set {
  89. _topObject = value;
  90. }
  91. }
  92. }
  93. }