Beispiel #1
0
        public void Execute()
        {
            var filePaths      = new List <string>();
            var directoryPaths = new List <string>
            {
                Assembly.GetExecutingAssembly().LocalDirectoryPath()
            };

            var resolutionPaths = AppDomainResolutionPaths.WithFilesAndDirectories(
                Path.GetDirectoryName(new Uri(Assembly.GetExecutingAssembly().CodeBase).LocalPath),
                filePaths,
                directoryPaths);
            Func <string, AppDomainPaths, AppDomain> builder = (name, paths) => AppDomainBuilder.Assemble(name, resolutionPaths);

            var projects = new Mock <ILinkToProjects>();

            using (var host = new ScriptHost(projects.Object, builder))
            {
                var output = string.Empty;
                using (var writer = new ScriptOutputPipe())
                {
                    writer.OnScriptOutput += (s, e) => output += e.Text;
                    var tuple = host.Execute(ScriptLanguage.IronPython, "print \"hello\"", writer);

                    Assert.IsTrue(host.IsExecutingScript);

                    tuple.Item1.Wait();
                    Assert.IsFalse(host.IsExecutingScript);
                    Assert.AreEqual("hello" + Environment.NewLine, output);
                }
            }
        }
        public void RunGuarded()
        {
            // Define the base path for the AppDomain. By default the assembly resolution will look for DLLs here
            var baseDirectory = @"c:\myapplication\plugins";

            // Define any additional files that should be resolved
            var additionalFiles = new[]
            {
                @"c:\myapplication\mylibrary.dll"
            };

            // Define any additional directories from where assemblies should be resolved
            var additionalDirectories = new[]
            {
                @"c:\myapplication\"
            };

            var assemblyResolutionPaths = AppDomainResolutionPaths.WithFilesAndDirectories(
                baseDirectory,
                additionalFiles,
                additionalDirectories);
            var appDomain = AppDomainBuilder.Assemble("MyAppDomain", assemblyResolutionPaths);

            Assert.IsNotNull(appDomain);
        }
        /// <summary>
        /// Starts the runtime
        /// </summary>
        public RuntimeErrorCode Run()
        {
            // Set working directory to location of this exe
            // ReSharper disable once AssignNullToNotNullAttribute
            Directory.SetCurrentDirectory(Path.GetDirectoryName(Assembly.GetEntryAssembly().Location));

            // Load all assemblies from directory before accessing any other code
            AppDomainBuilder.LoadAssemblies();

            // This is necessary to enable assembly lookup from AppDomain
            AppDomain.CurrentDomain.AssemblyResolve += AppDomainBuilder.ResolveAssembly;

            // This will give a message to the user before crashing
            AppDomain.CurrentDomain.UnhandledException += CrashHandler.HandleCrash;

            // Prepare container
            _container = CreateContainer();

            // Load and run environment
            var loadResult = LoadEnvironment(out var env);
            var returnCode = RuntimeErrorCode.NoError;

            switch (loadResult)
            {
            case EnvironmentLoadResult.BadVerb:
            case EnvironmentLoadResult.NoVerb:
            case EnvironmentLoadResult.Error:
                returnCode = RuntimeErrorCode.Error;
                break;

            case EnvironmentLoadResult.HelpRequested:
                returnCode = RuntimeErrorCode.NoError;
                break;

            case EnvironmentLoadResult.Success:
                returnCode = RunEnvironment(env);
                break;
            }

            // Clean up and exit
            try
            {
                _container.Destroy();
            }
            catch (Exception)
            {
                // Ignore it.
            }

            AppDomain.CurrentDomain.AssemblyResolve    -= AppDomainBuilder.ResolveAssembly;
            AppDomain.CurrentDomain.UnhandledException -= CrashHandler.HandleCrash;

            return(returnCode);
        }
Beispiel #4
0
        private static void RegisterAppDomainBuilder(ContainerBuilder builder)
        {
            builder.Register(
                c =>
            {
                // Autofac 2.4.5 forces the 'c' variable to disappear. See here:
                // http://stackoverflow.com/questions/5383888/autofac-registration-issue-in-release-v2-4-5-724
                Func <string, AppDomainPaths, AppDomain> result =
                    (name, paths) => AppDomainBuilder.Assemble(
                        name,
                        AppDomainResolutionPathsFor(paths));

                return(result);
            })
            .As <Func <string, AppDomainPaths, AppDomain> >()
            .SingleInstance();
        }
        /// <summary>
        /// Creates an instance of the <see cref="HeartOfGold"/>
        /// </summary>
        public HeartOfGold(string[] args)
        {
            PrepareArguments(args);

            // Set working directory to location of this exe
            // ReSharper disable once AssignNullToNotNullAttribute
            Directory.SetCurrentDirectory(Path.GetDirectoryName(Assembly.GetEntryAssembly().Location));

            // Load all assemblies from directory before accessing any other code
            AppDomainBuilder.LoadAssemblies();

            // This is necessary to enable assembly lookup from AppDomain
            AppDomain.CurrentDomain.AssemblyResolve += AppDomainBuilder.ResolveAssembly;

            // This will give a message to the user before crashing
            AppDomain.CurrentDomain.UnhandledException += CrashHandler.HandleCrash;

            // Prepare container
            _container = CreateContainer();
        }
Beispiel #6
0
 public void TestFixtureSetUp()
 {
     AppDomainBuilder.LoadAssemblies();
 }