/// <summary> /// Initializes a new instance of the <see cref="BaseSandbox" /> class. /// </summary> /// <param name="setting">The setting.</param> public Sandbox(SandboxSetting setting) : base(setting) { }
/// <summary> /// Initializes a new instance of the <see cref="BaseSandbox" /> class. /// </summary> /// <param name="setting">The setting.</param> /// <param name="key">The identifier.</param> private Sandbox(SandboxSetting setting, Guid key) : base(setting, key) { }
/// <summary> /// Initializes the specified name. /// </summary> /// <param name="setting">The setting.</param> protected virtual void Initialize(SandboxSetting setting) { try { setting.CheckNullObject(nameof(setting)); var applicationDirectory = ApplicationDirectory = setting.ApplicationBaseDirectory.SafeToString(EnvironmentCore.ApplicationBaseDirectory); bool shareApplicationBaseDirectory = applicationDirectory.Equals(EnvironmentCore.ApplicationBaseDirectory, StringComparison.OrdinalIgnoreCase); if (!shareApplicationBaseDirectory && !Directory.Exists(applicationDirectory)) { Directory.CreateDirectory(applicationDirectory); } AppDomain = CreateAppDomain(applicationDirectory); // Start to process assemblies and libraries HashSet <string> assemblyNameToLoad = setting.AssemblyNameListToLoad.ToHashSet() ?? new HashSet <string>(); Dictionary <string, byte[]> assemblyBytesToLoad = new Dictionary <string, byte[]>(); // Get all known and loaded Assemblies. foreach (var one in (GetDefaultAssembliesToLoad() ?? new HashSet <Assembly>())) { assemblyNameToLoad.Add(Path.GetFileNameWithoutExtension(one.Location)); } // Load assemblies foreach (var one in assemblyNameToLoad) { var destinationPath = Path.Combine(applicationDirectory, GetAssemblyFileNameWithExtension(one)); // Copy dlls if not share base directory if (!shareApplicationBaseDirectory) { var sourcePath = Path.Combine(EnvironmentCore.ApplicationBaseDirectory, GetAssemblyFileNameWithExtension(one)); // NOTE: Need to check if file exists. Because not all loaded assembly stay in bin folder, like system dlls. So if not existed in source folder, no need to copy. if (!sourcePath.Equals(destinationPath, StringComparison.OrdinalIgnoreCase) && File.Exists(sourcePath)) { File.Copy(sourcePath, destinationPath, true); } } if (File.Exists(destinationPath)) { AppDomain.Load(Path.GetFileNameWithoutExtension(destinationPath)); // this.AppDomain.Load(File.ReadAllBytes(destinationPath)); } } _invoker = CreateInstanceAndUnwrap <SandboxInvoker>(AppDomain); // Load external byte base assemblies if (setting.ExternalAssemblyToLoad.HasItem()) { foreach (var one in setting.ExternalAssemblyToLoad) { if (!assemblyNameToLoad.Contains(one.Key)) { var destinationPath = Path.Combine(applicationDirectory, GetAssemblyFileNameWithExtension(one.Key)); File.WriteAllBytes(destinationPath, one.Value); var exceptionJsonObject = _invoker.LoadAssemblyByName(Path.GetFileNameWithoutExtension(destinationPath)); if (!string.IsNullOrWhiteSpace(exceptionJsonObject)) { var exceptionObject = ExceptionExtension.ToException(JsonConvert.DeserializeObject <ExceptionInfo>(exceptionJsonObject)); if (exceptionObject != null) { throw exceptionObject.Handle(new { ExternalAssemblyToLoad = one }); } } //this.AppDomain.Load(new AssemblyName(Path.GetFileNameWithoutExtension(destinationPath))); //this.AppDomain.ExecuteAssembly(Path.GetFileNameWithoutExtension(destinationPath)); // this.AppDomain.Load(one.Value); } } } } catch (Exception ex) { throw ex.Handle(new { setting }); } }
/// <summary> /// Initializes a new instance of the <see cref="BaseSandbox"/> class. /// </summary> /// <param name="setting">The setting.</param> /// <param name="key">The identifier.</param> protected internal BaseSandbox(SandboxSetting setting, Guid key) { Key = key; Initialize(setting ?? new SandboxSetting { }); }
/// <summary> /// Initializes a new instance of the <see cref="BaseSandbox" /> class. /// </summary> /// <param name="setting">The setting.</param> protected BaseSandbox(SandboxSetting setting) : this(setting, Guid.NewGuid()) { }
/// <summary> /// Initializes the specified name. /// </summary> /// <param name="setting">The setting.</param> protected override void Initialize(SandboxSetting setting) { base.Initialize(setting); _assemblyProvider = CreateInstanceAndUnwrap <RemoteAssemblyProvider>(AppDomain); }
/// <summary> /// Initializes a new instance of the <see cref="RuntimeAssemblySandbox" /> class. /// </summary> /// <param name="setting">The setting.</param> public RuntimeAssemblySandbox(SandboxSetting setting) : base(setting) { }