/// <summary> /// Starts an application in a separate AppDomain. /// Loads all enabled plugins and starts the application via an ApplicationManager instance activated in the new AppDomain. /// </summary> /// <param name="appInfo">application to run</param> public void Run(ApplicationDescription appInfo, ICommandLineArgument[] args) { if (!initialized) { throw new InvalidOperationException("PluginManager is not initialized. DiscoverAndCheckPlugins() must be called before Run()"); } // create a separate AppDomain for the application // initialize the static ApplicationManager in the AppDomain // and remotely tell it to start the application OnApplicationStarting(new PluginInfrastructureEventArgs(appInfo)); AppDomain applicationDomain = null; try { AppDomainSetup setup = AppDomain.CurrentDomain.SetupInformation; setup.PrivateBinPath = pluginDir; applicationDomain = AppDomain.CreateDomain(AppDomain.CurrentDomain.FriendlyName, null, setup); Type applicationManagerType = typeof(DefaultApplicationManager); DefaultApplicationManager applicationManager = (DefaultApplicationManager)applicationDomain.CreateInstanceAndUnwrap(applicationManagerType.Assembly.FullName, applicationManagerType.FullName, true, BindingFlags.NonPublic | BindingFlags.Instance, null, null, null, null); applicationManager.PluginLoaded += applicationManager_PluginLoaded; applicationManager.PluginUnloaded += applicationManager_PluginUnloaded; applicationManager.PrepareApplicationDomain(applications, plugins); OnApplicationStarted(new PluginInfrastructureEventArgs(appInfo)); applicationManager.Run(appInfo, args); } finally { // make sure domain is unloaded in all cases AppDomain.Unload(applicationDomain); } }
/// <summary> /// Returns a new AppDomain with loaded assemblies/plugins from applicationBase /// </summary> public static AppDomain CreateAndInitSandbox(string appDomainName, string applicationBase, string configFilePath) { PermissionSet pSet; pSet = new PermissionSet(PermissionState.Unrestricted); AppDomainSetup setup = new AppDomainSetup(); setup.PrivateBinPath = applicationBase; setup.ApplicationBase = applicationBase; setup.ConfigurationFile = configFilePath; Type applicationManagerType = typeof(DefaultApplicationManager); AppDomain applicationDomain = AppDomain.CreateDomain(appDomainName, null, setup, pSet, null); DefaultApplicationManager applicationManager = (DefaultApplicationManager)applicationDomain.CreateInstanceAndUnwrap(applicationManagerType.Assembly.FullName, applicationManagerType.FullName, true, BindingFlags.NonPublic | BindingFlags.Instance, null, null, null, null); PluginManager pm = new PluginManager(applicationBase); pm.DiscoverAndCheckPlugins(); applicationManager.PrepareApplicationDomain(pm.Applications, pm.Plugins); return(applicationDomain); }