public void TestTypeFilterLevel()
        {
            // using an event handler requires type filter level Full for a remoting channel

            // by default, it's low, which will fail
            using (var domain = ProcessDomain.CreateDomain("Domain"))
            {
                Assert.Throws <SerializationException>(
                    () => { var obj = (RemoteTestObject)domain.CreateInstanceAndUnwrap(TestObjectAssemblyName, TestObjectTypeName); }
                    );
            }

            // now enable the remoting channel with type filter level Full
            var setup = new ProcessDomainSetup {
                TypeFilterLevel = TypeFilterLevel.Full
            };

            using (var domain = ProcessDomain.CreateDomain("Domain", setup))
            {
                var obj = (RemoteTestObject)domain.CreateInstanceAndUnwrap(TestObjectAssemblyName, TestObjectTypeName);

                obj.CalledBack = false;
                Assert.That(!obj.CalledBack);
                Assert.DoesNotThrow(() => obj.CallbackEvent += obj.SetCalledBack);
                obj.OnCallback();
                Assert.That(obj.CalledBack);
            }
        }
        public void TestProcessPriority()
        {
            // Default case
            using (var domain = ProcessDomain.CreateDomain("TestPriorityDomain", DefaultSetupInfo))
            {
                var obj = (RemoteTestObject)domain.CreateInstanceAndUnwrap(typeof(RemoteTestObject).Assembly.FullName, typeof(RemoteTestObject).FullName);
                Assert.That(obj.GetPriority(), Is.EqualTo(ProcessPriorityClass.Normal));
            }

            // Try each priority
            foreach (ProcessPriorityClass priority in Enum.GetValues(typeof(ProcessPriorityClass)))
            {
                var setup = new ProcessDomainSetup {
                    PriorityClass = priority, TypeFilterLevel = TypeFilterLevel.Full
                };
                using (var domain = ProcessDomain.CreateDomain("TestPriorityDomain", setup))
                {
                    var obj = (RemoteTestObject)domain.CreateInstanceAndUnwrap(typeof(RemoteTestObject).Assembly.FullName, typeof(RemoteTestObject).FullName);

                    // If not running as administrator, we can't run as RealTime, it will become High
                    var expectedPriority = priority == ProcessPriorityClass.RealTime && !obj.RunningAsAdministrator() ? ProcessPriorityClass.High : priority;

                    Assert.That(obj.GetPriority(), Is.EqualTo(expectedPriority));
                }
            }
        }
        public void TestConfigurationLocation()
        {
            // by default uses our app config
            using (var domain1 = ProcessDomain.CreateDomain("Domain1", DefaultSetupInfo))
            {
                var obj = (RemoteTestObject)domain1.CreateInstanceAndUnwrap(TestObjectAssemblyName, TestObjectTypeName);
                Assert.That(obj.GetAppConfigValue("MyValue"), Is.EqualTo("MyValue"));
            }

            // now point it at a different app config
            var setupInfo = new ProcessDomainSetup
            {
                AppDomainSetupInformation =
                {
                    ConfigurationFile = Path.Combine(Path.GetDirectoryName(AppDomain.CurrentDomain.SetupInformation.ConfigurationFile), "OtherApp.config"),
                },
                TypeFilterLevel = TypeFilterLevel.Full
            };

            using (var domain2 = ProcessDomain.CreateDomain("Domain2", setupInfo))
            {
                var obj = (RemoteTestObject)domain2.CreateInstanceAndUnwrap(TestObjectAssemblyName, TestObjectTypeName);
                Assert.That(obj.GetAppConfigValue("MyValue"), Is.EqualTo("OtherValue"));
            }
        }
 public void TestDefaultWorkingDirectory()
 {
     using (var domain = ProcessDomain.CreateDomain("ProcessDomain", DefaultSetupInfo))
     {
         var obj = (RemoteTestObject)domain.CreateInstanceAndUnwrap(TestObjectAssemblyName, TestObjectTypeName);
         Assert.That(obj.CurrentDirectory, Is.EqualTo(Environment.CurrentDirectory));
     }
 }
 public HttpResponseMessage getBacklogByUnit(decimal unit_id)
 {
     try {
         return(response(HttpStatusCode.OK, true, "ready", ProcessDomain.getBackLogByUnit(unit_id)));
     } catch (Exception e) {
         return(response(HttpStatusCode.OK, false, e));
     }
 }
 public void TestFriendlyName()
 {
     using (var domain = ProcessDomain.CreateDomain("ProcessDomain", DefaultSetupInfo))
     {
         var obj = (RemoteTestObject)domain.CreateInstanceAndUnwrap(TestObjectAssemblyName, TestObjectTypeName);
         Assert.That(obj.GetProcessId(), Is.Not.EqualTo(Process.GetCurrentProcess().Id));
         Assert.That(obj.GetProcessFileName(), Is.Not.EqualTo(Process.GetCurrentProcess().MainModule.FileName));
         Assert.That(obj.GetProcessFileName().EndsWith("ProcessDomain.exe"));
     }
 }
Exemple #7
0
 public void AddProcess(ProcessDomain process)
 {
     workDone = false;
     if (workDone == false)
     {
         lock (SJFLock)
         {
             processes.Add(process);
         }
     }
 }
        public void TestDomainAttachDetach()
        {
            var attachedEvent = new ManualResetEvent(false);
            var detachedEvent = new ManualResetEvent(false);

            using (var domain = ProcessDomain.CreateDomain("ProcessDomain", DefaultSetupInfo))
            {
                domain.Attached += () => attachedEvent.Set();
                domain.Detached += () => detachedEvent.Set();

                var obj = (RemoteTestObject)domain.CreateInstanceAndUnwrap(TestObjectAssemblyName, TestObjectTypeName);
                Assert.That(!attachedEvent.WaitOne(0));
                Assert.That(!detachedEvent.WaitOne(0));

                // restart should occur, but our current object will be invalid
                Process.GetProcessById(obj.GetProcessId()).Kill();
                Assert.That(detachedEvent.WaitOne(10000), "Timed-out waiting for process to die");
                Assert.That(attachedEvent.WaitOne(10000), "Timed-out waiting for process to respawn");
                Assert.Throws <RemotingException>(() => obj.GetProcessId());

                // create object in restarted domain
                obj = (RemoteTestObject)domain.CreateInstanceAndUnwrap(TestObjectAssemblyName, TestObjectTypeName);
                Assert.That(obj.GetProcessId(), Is.Not.EqualTo(Process.GetCurrentProcess().Id));
            }

            var setupInfo = new ProcessDomainSetup
            {
                RestartOnProcessExit = false,
                TypeFilterLevel      = TypeFilterLevel.Full
            };

            // now restart should not occur
            using (var domain = ProcessDomain.CreateDomain("RemoteProcess2", setupInfo))
            {
                domain.Attached += () => attachedEvent.Set();
                domain.Detached += () => detachedEvent.Set();

                var obj = (RemoteTestObject)domain.CreateInstanceAndUnwrap(TestObjectAssemblyName, TestObjectTypeName);
                Assert.That(obj.GetProcessId(), Is.Not.EqualTo(Process.GetCurrentProcess().Id));

                attachedEvent.Reset();
                detachedEvent.Reset();

                Process.GetProcessById(obj.GetProcessId()).Kill();

                Assert.That(detachedEvent.WaitOne(10000), "Timed-out waiting for process to die");
                Assert.That(!attachedEvent.WaitOne(5000), "Unexpected re-attach");
                Assert.Throws <RemotingException>(() => obj.GetProcessId());
                Assert.Throws <RemotingException>(() => obj = (RemoteTestObject)domain.CreateInstanceAndUnwrap(TestObjectAssemblyName, TestObjectTypeName));
            }
        }
        public void TestExecutableLocation()
        {
            string desiredExecutableFileName = Path.Combine(Environment.CurrentDirectory, "MyDomain.exe");

            var setupInfo = new ProcessDomainSetup
            {
                ExecutableDirectory = Environment.CurrentDirectory,
                TypeFilterLevel     = TypeFilterLevel.Full
            };

            // default uses temp directory
            using (var domain1 = ProcessDomain.CreateDomain("MyDomain", DefaultSetupInfo))
            {
                var obj = (RemoteTestObject)domain1.CreateInstanceAndUnwrap(TestObjectAssemblyName, TestObjectTypeName);
                Assert.That(obj.GetProcessFileName(), Is.Not.EqualTo(desiredExecutableFileName));
            }

            // now using our specified location
            using (var domain2 = ProcessDomain.CreateDomain("MyDomain", setupInfo))
            {
                var obj = (RemoteTestObject)domain2.CreateInstanceAndUnwrap(TestObjectAssemblyName, TestObjectTypeName);
                Assert.That(obj.GetProcessFileName(), Is.EqualTo(desiredExecutableFileName));
            }

            // test if file exists, it will be overwritten
            using (var writer = new StreamWriter(desiredExecutableFileName, false))
            {
                writer.WriteLine("Garbage");

                // will fail to compile because file is open
                Assert.Throws <AssemblyGeneratorCompilerException>(() => ProcessDomain.CreateDomain("MyDomain", setupInfo));

                writer.Flush();
            }

            // file is now closed, but contains garbage that can't execute,
            // but the file will get overwritten
            using (var domain3 = ProcessDomain.CreateDomain("MyDomain", setupInfo))
            {
                var obj = (RemoteTestObject)domain3.CreateInstanceAndUnwrap(TestObjectAssemblyName, TestObjectTypeName);
                Assert.That(obj.GetProcessFileName(), Is.EqualTo(desiredExecutableFileName));
            }

            // and once we're done, file is gone
            Assert.That(!File.Exists(desiredExecutableFileName));
        }
        public void TestProcessDomainAssemblyResolver()
        {
            string prevDirectory = Environment.CurrentDirectory;

            try
            {
                Directory.SetCurrentDirectory(Path.Combine(prevDirectory, ".."));

                using (var domain = ProcessDomain.CreateDomain("ProcessDomain", DefaultSetupInfo))
                {
                    // this used to fail because it would try to load the ProcessDomain assembly from 'current directory'
                }
            }
            finally
            {
                Directory.SetCurrentDirectory(prevDirectory);
            }
        }
        public void TestDeleteFileOnExit()
        {
            string desiredExecutableFileName = Path.Combine(Environment.CurrentDirectory, "ProcessDomain.exe");

            var setupInfo = new ProcessDomainSetup
            {
                ExecutableDirectory = Environment.CurrentDirectory,
                DeleteOnUnload      = false
            };

            ProcessDomain.Unload(ProcessDomain.CreateDomain("ProcessDomain", setupInfo));
            Assert.That(File.Exists(desiredExecutableFileName));

            setupInfo.DeleteOnUnload = true;

            ProcessDomain.Unload(ProcessDomain.CreateDomain("ProcessDomain", setupInfo));
            Assert.That(!File.Exists(desiredExecutableFileName));
        }
        public void TestMultipleProcessDomains()
        {
            // Along with enabling support for TypeFilterLevel = Full, we also have to allow multiple
            // channels to be created.  This will simply do just that and ensure there's no
            // duplicate channel registration exceptions
            using (var domain1 = ProcessDomain.CreateDomain("Domain1", DefaultSetupInfo))
                using (var domain2 = ProcessDomain.CreateDomain("Domain2", DefaultSetupInfo))
                {
                    var obj1 = (RemoteTestObject)domain1.CreateInstanceAndUnwrap(TestObjectAssemblyName, TestObjectTypeName);
                    var obj2 = (RemoteTestObject)domain2.CreateInstanceAndUnwrap(TestObjectAssemblyName, TestObjectTypeName);

                    Assert.That(!obj1.CalledBack);
                    Assert.That(!obj2.CalledBack);
                    obj1.SetCalledBack();
                    obj2.SetCalledBack();
                    Assert.That(obj1.CalledBack);
                    Assert.That(obj2.CalledBack);
                }
        }
 public HttpResponseMessage createProcess(CreateProcessRequest request)
 {
     try {
         if (checkToken(Request))
         {
             try {
                 ProcessDomain.createProcess(request.name, request.description, request.user_id);
                 return(response(HttpStatusCode.OK, true, "ready"));
             } catch (Exception e) {
                 return(response(HttpStatusCode.InternalServerError, false, e));
             }
         }
         else
         {
             return(response(HttpStatusCode.Unauthorized, false, "invalid token"));
         }
     } catch (Exception e) {
         return(response(HttpStatusCode.OK, false, e));
     }
 }
        public void TestProcessPriority()
        {
            // Default case
            using (var domain = ProcessDomain.CreateDomain("TestPriorityDomain"))
            {
                var obj = (RemoteTestObject)domain.CreateInstanceAndUnwrap(typeof(RemoteTestObject).Assembly.FullName, typeof(RemoteTestObject).FullName);
                Assert.That(obj.GetPriority(), Is.EqualTo(ProcessPriorityClass.Normal));
            }

            // Try each priority
            foreach (ProcessPriorityClass priority in Enum.GetValues(typeof(ProcessPriorityClass)))
            {
                var setup = new ProcessDomainSetup {
                    PriorityClass = priority
                };
                using (var domain = ProcessDomain.CreateDomain("TestPriorityDomain", setup))
                {
                    var obj = (RemoteTestObject)domain.CreateInstanceAndUnwrap(typeof(RemoteTestObject).Assembly.FullName, typeof(RemoteTestObject).FullName);
                    Assert.That(obj.GetPriority(), Is.EqualTo(priority));
                }
            }
        }
 public static LoggingConfigurator CreateConfigurator(ProcessDomain domain)
 {
     return (LoggingConfigurator)domain.CreateInstanceAndUnwrap(typeof(LoggingConfigurator).Assembly.FullName, typeof(LoggingConfigurator).FullName);
 }
Exemple #16
0
 public static LoggingConfigurator CreateConfigurator(ProcessDomain domain)
 {
     return((LoggingConfigurator)domain.CreateInstanceAndUnwrap(typeof(LoggingConfigurator).Assembly.FullName, typeof(LoggingConfigurator).FullName));
 }
        static void Main(string[] args)
        {
            try
            {
                var localObject = new RemoteObject();
                Console.WriteLine("Local object - Main Module Location: {0}", localObject.GetProcessMainModuleFileName());
                Console.WriteLine("Local object - App.config location: {0}", localObject.GetAppConfigLocation());
                Console.WriteLine("Local object - App.config value: {0}", localObject.GetAppConfigValue("MyString"));

                var setup = new ProcessDomainSetup
                {
                    ProcessStartTimeout = new TimeSpan(0, 0, 5),
                };

                using (ProcessDomain processDomain = ProcessDomain.CreateDomain("RemoteProcess", setup))
                {
                    LoggingConfigurator.CreateConfigurator(processDomain).ConfigureAppConfig();

                    var remoteObject = (RemoteObject)processDomain.CreateInstanceAndUnwrap(typeof(RemoteObject).Assembly.FullName, typeof(RemoteObject).FullName);
                    Console.WriteLine("Remote object - Main Module Location: {0}", remoteObject.GetProcessMainModuleFileName());
                    Console.WriteLine("Remote object - App.config location: {0}", remoteObject.GetAppConfigLocation());
                    Console.WriteLine("Remote object - App.config value: {0}", remoteObject.GetAppConfigValue("MyString"));

                    var detachedEvent = new ManualResetEvent(false);
                    var attachedEvent = new ManualResetEvent(false);
                    processDomain.Detached += () => detachedEvent.Set();
                    processDomain.Attached += () =>
                    {
                        LoggingConfigurator.CreateConfigurator(processDomain).ConfigureAppConfig();
                        attachedEvent.Set();
                    };

                    Console.WriteLine("Finding RemoteProcess and killing it...");
                    Process.GetProcessesByName("RemoteProcess").FirstOrDefault().Kill();

                    if (!detachedEvent.WaitOne(10000))
                    {
                        throw new Exception("Timed-out while waiting for process to die");
                    }

                    Console.WriteLine("Waiting for new process to spawn");
                    if (!attachedEvent.WaitOne(10000))
                    {
                        throw new Exception("Timed-out while waiting for process to restart");
                    }

                    Console.WriteLine("Re-creating remote object in newly spawned process");
                    remoteObject = (RemoteObject)processDomain.CreateInstanceAndUnwrap(typeof(RemoteObject).Assembly.FullName, typeof(RemoteObject).FullName);
                    Console.WriteLine("Remote object - Main Module Location: {0}", remoteObject.GetProcessMainModuleFileName());
                    Console.WriteLine("Remote object - App.config location: {0}", remoteObject.GetAppConfigLocation());
                    Console.WriteLine("Remote object - App.config value: {0}", remoteObject.GetAppConfigValue("MyString"));

                    Console.WriteLine("Throwing an exception...");
                    try
                    {
                        remoteObject.ThrowException();
                        Console.WriteLine("Did not catch an exception...");
                    }
                    catch (RemoteException rex)
                    {
                        Console.WriteLine("Caught exception: {0}", rex.Message);
                    }
                }

                Console.WriteLine("Two process domains at the same time");

                using (var processDomain1 = ProcessDomain.CreateDomain("RemoteProcess1", setup))
                    using (var processDomain2 = ProcessDomain.CreateDomain("RemoteProcess2", setup))
                    {
                        var remoteObject1 = (RemoteObject)processDomain1.CreateInstanceAndUnwrap(typeof(RemoteObject).Assembly.FullName, typeof(RemoteObject).FullName);
                        var remoteObject2 = (RemoteObject)processDomain2.CreateInstanceAndUnwrap(typeof(RemoteObject).Assembly.FullName, typeof(RemoteObject).FullName);

                        Console.WriteLine("Remote object #1 - App.config value: {0}", remoteObject1.GetAppConfigValue("MyString"));
                        Console.WriteLine("Remote object #2 - App.config value: {0}", remoteObject2.GetAppConfigValue("MyString"));
                    }

                Console.WriteLine("Process domain in alternate location");
                setup.AppDomainSetupInformation.ApplicationBase = @"c:\";
                setup.ExternalAssemblies[typeof(Program).Assembly.GetName()] = typeof(Program).Assembly.Location;
                using (var processDomain = ProcessDomain.CreateDomain("RemoteProcess", setup))
                {
                    var remoteObject = (RemoteObject)processDomain.CreateInstanceAndUnwrap(typeof(RemoteObject).Assembly.FullName, typeof(RemoteObject).FullName);

                    Console.WriteLine("Remote object - App.config value: {0}", remoteObject.GetAppConfigValue("MyString"));
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine("Exception: {0}", ex);
            }

            Console.WriteLine("Press any key to exit");
            Console.ReadKey();
        }