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