SoapCore.cs 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. //
  2. // System.Runtime.Remoting.Channels.SoapCore.cs
  3. //
  4. // Author: Lluis Sanchez Gual ([email protected])
  5. //
  6. // 2003 (C) Copyright, Novell, Inc.
  7. //
  8. //
  9. // Permission is hereby granted, free of charge, to any person obtaining
  10. // a copy of this software and associated documentation files (the
  11. // "Software"), to deal in the Software without restriction, including
  12. // without limitation the rights to use, copy, modify, merge, publish,
  13. // distribute, sublicense, and/or sell copies of the Software, and to
  14. // permit persons to whom the Software is furnished to do so, subject to
  15. // the following conditions:
  16. //
  17. // The above copyright notice and this permission notice shall be
  18. // included in all copies or substantial portions of the Software.
  19. //
  20. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  21. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  22. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  23. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  24. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  25. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  26. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  27. //
  28. using System.Collections;
  29. using System.Runtime.Remoting.Messaging;
  30. using System.Runtime.Serialization;
  31. using System.Runtime.Serialization.Formatters;
  32. using System.Runtime.Serialization.Formatters.Soap;
  33. namespace System.Runtime.Remoting.Channels
  34. {
  35. internal class SoapCore
  36. {
  37. SoapFormatter _serializationFormatter;
  38. SoapFormatter _deserializationFormatter;
  39. bool _includeVersions = true;
  40. bool _strictBinding = false;
  41. IDictionary _properties;
  42. #if NET_1_1
  43. TypeFilterLevel _filterLevel = TypeFilterLevel.Low;
  44. #endif
  45. public static SoapCore DefaultInstance = new SoapCore ();
  46. public SoapCore (object owner, IDictionary properties, string[] allowedProperties)
  47. {
  48. _properties = properties;
  49. if (_properties == null)
  50. {
  51. _properties = new Hashtable(10);
  52. }
  53. foreach(DictionaryEntry property in properties)
  54. {
  55. string key = (string) property.Key;
  56. if (Array.IndexOf (allowedProperties, key) == -1)
  57. throw new RemotingException (owner.GetType().Name + " does not recognize '" + key + "' configuration property");
  58. switch (key)
  59. {
  60. case "includeVersions":
  61. _includeVersions = Convert.ToBoolean (property.Value);
  62. break;
  63. case "strictBinding":
  64. _strictBinding = Convert.ToBoolean (property.Value);
  65. break;
  66. #if NET_1_1
  67. case "typeFilterLevel":
  68. if (property.Value is TypeFilterLevel)
  69. _filterLevel = (TypeFilterLevel) property.Value;
  70. else {
  71. string s = (string) property.Value;
  72. _filterLevel = (TypeFilterLevel) Enum.Parse (typeof(TypeFilterLevel), s);
  73. }
  74. break;
  75. #endif
  76. }
  77. }
  78. Init ();
  79. }
  80. public SoapCore ()
  81. {
  82. _properties = new Hashtable(10);
  83. Init ();
  84. }
  85. public void Init ()
  86. {
  87. RemotingSurrogateSelector surrogateSelector = new RemotingSurrogateSelector ();
  88. StreamingContext context = new StreamingContext (StreamingContextStates.Remoting, null);
  89. _serializationFormatter = CreateFormatter (surrogateSelector, context);
  90. _deserializationFormatter = CreateFormatter (null, context);
  91. #if NET_1_1
  92. _serializationFormatter.FilterLevel = _filterLevel;
  93. _deserializationFormatter.FilterLevel = _filterLevel;
  94. #endif
  95. }
  96. SoapFormatter CreateFormatter (ISurrogateSelector selector, StreamingContext context)
  97. {
  98. SoapFormatter fm = new SoapFormatter (selector, context);
  99. if (!_includeVersions)
  100. fm.AssemblyFormat = FormatterAssemblyStyle.Simple;
  101. if (!_strictBinding)
  102. fm.Binder = ChannelCore.SimpleBinder;
  103. return fm;
  104. }
  105. public SoapFormatter GetSafeDeserializer ()
  106. {
  107. StreamingContext context = new StreamingContext (StreamingContextStates.Remoting, null);
  108. return CreateFormatter (null, context);
  109. }
  110. public SoapFormatter Serializer
  111. {
  112. get { return _serializationFormatter; }
  113. }
  114. public SoapFormatter Deserializer
  115. {
  116. get { return _deserializationFormatter; }
  117. }
  118. public IDictionary Properties
  119. {
  120. get { return _properties; }
  121. }
  122. #if NET_1_1
  123. public TypeFilterLevel TypeFilterLevel
  124. {
  125. get { return _filterLevel; }
  126. }
  127. #endif
  128. }
  129. }