2 // System.AppDomainSetup.cs
5 // Dietmar Maurer (dietmar@ximian.com)
6 // Sebastien Pouliot <sebastien@ximian.com>
8 // (C) 2001 Ximian, Inc. http://www.ximian.com
9 // Copyright (C) 2004-2005 Novell, Inc (http://www.novell.com)
12 // Fix serialization compatibility with MS.NET.
14 // Permission is hereby granted, free of charge, to any person obtaining
15 // a copy of this software and associated documentation files (the
16 // "Software"), to deal in the Software without restriction, including
17 // without limitation the rights to use, copy, modify, merge, publish,
18 // distribute, sublicense, and/or sell copies of the Software, and to
19 // permit persons to whom the Software is furnished to do so, subject to
20 // the following conditions:
22 // The above copyright notice and this permission notice shall be
23 // included in all copies or substantial portions of the Software.
25 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
26 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
27 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
28 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
29 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
30 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
31 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
34 using System
.Collections
.Generic
;
36 using System
.Runtime
.CompilerServices
;
37 using System
.Runtime
.InteropServices
;
38 using System
.Security
;
39 using System
.Runtime
.Serialization
.Formatters
.Binary
;
42 using System
.Runtime
.Hosting
;
43 using System
.Security
.Policy
;
49 [ClassInterface (ClassInterfaceType
.None
)]
52 public sealed class AppDomainSetup
54 public sealed class AppDomainSetup
: IAppDomainSetup
57 string application_base
;
58 string application_name
;
60 string configuration_file
;
63 string private_bin_path
;
64 string private_bin_path_probe
;
65 string shadow_copy_directories
;
66 string shadow_copy_files
;
67 bool publisher_policy
;
68 private bool path_changed
;
69 private LoaderOptimization loader_optimization
;
70 bool disallow_binding_redirects
;
71 bool disallow_code_downloads
;
74 private ActivationArguments _activationArguments
;
75 AppDomainInitializer domain_initializer
;
77 ApplicationTrust application_trust
;
78 string [] domain_initializer_args
;
80 object _activationArguments
;
81 object domain_initializer
; // always null
83 object application_trust
; // dummy, always null
84 object domain_initializer_args
;
86 bool disallow_appbase_probe
;
87 byte [] configuration_bytes
;
89 byte [] serialized_non_primitives
;
91 public AppDomainSetup ()
95 internal AppDomainSetup (AppDomainSetup setup
)
97 application_base
= setup
.application_base
;
98 application_name
= setup
.application_name
;
99 cache_path
= setup
.cache_path
;
100 configuration_file
= setup
.configuration_file
;
101 dynamic_base
= setup
.dynamic_base
;
102 license_file
= setup
.license_file
;
103 private_bin_path
= setup
.private_bin_path
;
104 private_bin_path_probe
= setup
.private_bin_path_probe
;
105 shadow_copy_directories
= setup
.shadow_copy_directories
;
106 shadow_copy_files
= setup
.shadow_copy_files
;
107 publisher_policy
= setup
.publisher_policy
;
108 path_changed
= setup
.path_changed
;
109 loader_optimization
= setup
.loader_optimization
;
110 disallow_binding_redirects
= setup
.disallow_binding_redirects
;
111 disallow_code_downloads
= setup
.disallow_code_downloads
;
112 _activationArguments
= setup
._activationArguments
;
113 domain_initializer
= setup
.domain_initializer
;
114 application_trust
= setup
.application_trust
;
115 domain_initializer_args
= setup
.domain_initializer_args
;
116 disallow_appbase_probe
= setup
.disallow_appbase_probe
;
117 configuration_bytes
= setup
.configuration_bytes
;
121 public AppDomainSetup (ActivationArguments activationArguments
)
123 _activationArguments
= activationArguments
;
126 public AppDomainSetup (ActivationContext activationContext
)
128 _activationArguments
= new ActivationArguments (activationContext
);
132 static string GetAppBase (string appBase
)
137 int len
= appBase
.Length
;
138 if (len
>= 8 && appBase
.ToLower ().StartsWith ("file://")) {
139 appBase
= appBase
.Substring (7);
140 if (Path
.DirectorySeparatorChar
!= '/')
141 appBase
= appBase
.Replace ('/', Path
.DirectorySeparatorChar
);
142 if (Environment
.IsRunningOnWindows
) {
143 // Under Windows prepend "//" to indicate it's a local file
144 appBase
= "//" + appBase
;
147 appBase
= Path
.GetFullPath (appBase
);
153 public string ApplicationBase
{
154 get { return GetAppBase (application_base); }
155 set { application_base = value; }
158 public string ApplicationName
{
160 return application_name
;
163 application_name
= value;
167 public string CachePath
{
176 public string ConfigurationFile
{
178 if (configuration_file
== null)
180 if (Path
.IsPathRooted(configuration_file
))
181 return configuration_file
;
182 if (ApplicationBase
== null)
183 throw new MemberAccessException("The ApplicationBase must be set before retrieving this property.");
184 return Path
.Combine(ApplicationBase
, configuration_file
);
187 configuration_file
= value;
191 public bool DisallowPublisherPolicy
{
193 return publisher_policy
;
196 publisher_policy
= value;
200 public string DynamicBase
{
202 if (dynamic_base
== null)
205 if (Path
.IsPathRooted (dynamic_base
))
208 if (ApplicationBase
== null)
209 throw new MemberAccessException ("The ApplicationBase must be set before retrieving this property.");
211 return Path
.Combine (ApplicationBase
, dynamic_base
);
214 if (application_name
== null)
215 throw new MemberAccessException ("ApplicationName must be set before the DynamicBase can be set.");
216 uint id
= (uint) application_name
.GetHashCode ();
217 dynamic_base
= Path
.Combine (value, id
.ToString("x"));
221 public string LicenseFile
{
226 license_file
= value;
230 [MonoLimitation ("In Mono this is controlled by the --share-code flag")]
231 public LoaderOptimization LoaderOptimization
{
233 return loader_optimization
;
236 loader_optimization
= value;
240 public string PrivateBinPath
{
242 return private_bin_path
;
245 private_bin_path
= value;
250 public string PrivateBinPathProbe
{
252 return private_bin_path_probe
;
255 private_bin_path_probe
= value;
260 public string ShadowCopyDirectories
{
262 return shadow_copy_directories
;
265 shadow_copy_directories
= value;
269 public string ShadowCopyFiles
{
271 return shadow_copy_files
;
274 shadow_copy_files
= value;
278 public bool DisallowBindingRedirects
{
280 return disallow_binding_redirects
;
283 disallow_binding_redirects
= value;
287 public bool DisallowCodeDownload
{
289 return disallow_code_downloads
;
292 disallow_code_downloads
= value;
296 public ActivationArguments ActivationArguments
{
298 if (_activationArguments
!= null)
299 return _activationArguments
;
300 DeserializeNonPrimitives ();
301 return _activationArguments
;
303 set { _activationArguments = value; }
306 [MonoLimitation ("it needs to be invoked within the created domain")]
307 public AppDomainInitializer AppDomainInitializer
{
309 if (domain_initializer
!= null)
310 return domain_initializer
;
311 DeserializeNonPrimitives ();
312 return domain_initializer
;
314 set { domain_initializer = value; }
317 [MonoLimitation ("it needs to be used to invoke the initializer within the created domain")]
318 public string [] AppDomainInitializerArguments
{
319 get { return domain_initializer_args; }
320 set { domain_initializer_args = value; }
323 [MonoNotSupported ("This property exists but not considered.")]
324 public ApplicationTrust ApplicationTrust
{
326 if (application_trust
!= null)
327 return application_trust
;
328 DeserializeNonPrimitives ();
329 if (application_trust
== null)
330 application_trust
= new ApplicationTrust ();
331 return application_trust
;
333 set { application_trust = value; }
336 [MonoNotSupported ("This property exists but not considered.")]
337 public bool DisallowApplicationBaseProbing
{
338 get { return disallow_appbase_probe; }
339 set { disallow_appbase_probe = value; }
342 [MonoNotSupported ("This method exists but not considered.")]
343 public byte [] GetConfigurationBytes ()
345 return configuration_bytes
!= null ? configuration_bytes
.Clone () as byte [] : null;
348 [MonoNotSupported ("This method exists but not considered.")]
349 public void SetConfigurationBytes (byte [] value)
351 configuration_bytes
= value;
354 private void DeserializeNonPrimitives ()
357 if (serialized_non_primitives
== null)
360 BinaryFormatter bf
= new BinaryFormatter ();
361 MemoryStream ms
= new MemoryStream (serialized_non_primitives
);
363 object [] arr
= (object []) bf
.Deserialize (ms
);
365 _activationArguments
= (ActivationArguments
) arr
[0];
366 domain_initializer
= (AppDomainInitializer
) arr
[1];
367 application_trust
= (ApplicationTrust
) arr
[2];
369 serialized_non_primitives
= null;
373 internal void SerializeNonPrimitives ()
375 object [] arr
= new object [3];
377 arr
[0] = _activationArguments
;
378 arr
[1] = domain_initializer
;
379 arr
[2] = application_trust
;
381 BinaryFormatter bf
= new BinaryFormatter ();
382 MemoryStream ms
= new MemoryStream ();
384 bf
.Serialize (ms
, arr
);
386 serialized_non_primitives
= ms
.ToArray ();
389 #if NET_4_0 || MOONLIGHT
390 [MonoTODO ("not implemented, does not throw because it's used in testing moonlight")]
391 public void SetCompatibilitySwitches (IEnumerable
<string> switches
)