示例#1
0
        void ProcessExited(IAsyncOperation oper)
        {
            lock (this) {
                Counters.ExternalHostProcesses--;

                // Remove all callbacks from existing objects
                foreach (object ob in remoteObjects)
                {
                    RemotingService.UnregisterMethodCallback(ob, "Dispose");
                }

                remoteObjects.Clear();

                exitedEvent.Set();

                // If the remote process crashes, a thread may be left hung in WaitForExit. This will awaken it.
                exitRequestEvent.Set();

                if (oper != process)
                {
                    return;
                }

                // The process suddently died
                runningEvent.Reset();
                processHost = null;
                process     = null;
                references  = 0;
            }
        }
示例#2
0
        public object CreateInstance(string assemblyPath, string typeName, string[] addins, IList <string> userAssemblyPaths = null)
        {
            lock (this) {
                references++;
                if (processHost == null)
                {
                    Start(userAssemblyPaths);
                }
            }

            if (!runningEvent.WaitOne(15000, false))
            {
                references--;
                throw new ApplicationException("Couldn't create a remote process.");
            }

            try {
                // Before creating the instance, load the add-ins on which it depends
                if (addins != null && addins.Length > 0)
                {
                    processHost.LoadAddins(addins);
                }
                RemotingService.RegisterAssemblyForSimpleResolve(Path.GetFileNameWithoutExtension(assemblyPath));
                object obj = processHost.CreateInstance(assemblyPath, typeName);
                RemotingService.RegisterMethodCallback(obj, "Dispose", RemoteProcessObjectDisposing, null);
                RemotingService.RegisterMethodCallback(obj, "Shutdown", RemoteProcessObjectShuttingDown, null);
                remoteObjects.Add(obj);
                Counters.ExternalObjects++;
                return(obj);
            } catch {
                ReleaseInstance(null);
                throw;
            }
        }
示例#3
0
        public object CreateInstance(Type type, string[] addins)
        {
            lock (this) {
                references++;
                if (processHost == null)
                {
                    Start();
                }
            }

            if (!runningEvent.WaitOne(15000, false))
            {
                references--;
                throw new ApplicationException("Couldn't create a remote process.");
            }

            try {
                // Before creating the instance, load the add-ins on which it depends
                if (addins != null && addins.Length > 0)
                {
                    processHost.LoadAddins(addins);
                }
                object obj = processHost.CreateInstance(type);
                RemotingService.RegisterMethodCallback(obj, "Dispose", RemoteProcessObjectDisposing, null);
                remoteObjects.Add(obj);
                Counters.ExternalObjects++;
                return(obj);
            } catch {
                ReleaseInstance(null);
                throw;
            }
        }
示例#4
0
        void ProcessExited(IAsyncOperation oper)
        {
            lock (this) {
                Counters.ExternalHostProcesses--;

                // Remove all callbacks from existing objects
                foreach (object ob in remoteObjects)
                {
                    RemotingService.UnregisterMethodCallback(ob, "Dispose");
                }

                remoteObjects.Clear();

                exitedEvent.Set();
                if (oper != process)
                {
                    return;
                }

                // The process suddently died
                runningEvent.Reset();
                processHost = null;
                process     = null;
                references  = 0;
            }
        }
示例#5
0
        public void Start()
        {
            lock (this)
            {
                if (starting)
                {
                    return;
                }
                starting = true;
                exitRequestEvent.Reset();

                RemotingService.RegisterRemotingChannel();

                BinaryFormatter bf   = new BinaryFormatter();
                ObjRef          oref = RemotingServices.Marshal(this);
                MemoryStream    ms   = new MemoryStream();
                bf.Serialize(ms, oref);
                string sref    = Convert.ToBase64String(ms.ToArray());
                string tmpFile = null;

                if (executionHandlerFactory == null)
                {
                    executionHandlerFactory = Runtime.SystemAssemblyService.CurrentRuntime.GetExecutionHandler();
                }

                try {
                    string location = Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location);
                    location = Path.Combine(location, "mdhost.exe");

                    ProcessHostConsole cons = new ProcessHostConsole();
                    tmpFile = Path.GetTempFileName();
                    File.WriteAllText(tmpFile, sref + "\n" + Process.GetCurrentProcess().Id + "\n");
                    string arguments           = string.Format("{0} \"{1}\"", id, tmpFile);
                    DotNetExecutionCommand cmd = new DotNetExecutionCommand(location, arguments, AppDomain.CurrentDomain.BaseDirectory);
                    cmd.DebugMode = isDebugMode;
                    process       = executionHandlerFactory.Execute(cmd, cons);
                    Counters.ExternalHostProcesses++;

                    process.Completed += ProcessExited;
                } catch (Exception ex) {
                    if (tmpFile != null)
                    {
                        try {
                            File.Delete(tmpFile);
                        } catch {
                        }
                    }
                    LoggingService.LogError(ex.ToString());
                    throw;
                }
            }
        }
        public IMessage SyncProcessMessage(IMessage msg)
        {
            IMethodCallMessage mcm = (IMethodCallMessage)msg;
            int  timeout           = -1;
            bool timedOut          = false;

            RemotingService.CallbackData data = RemotingService.GetCallbackData(mcm.Uri, mcm.MethodName);
            if (data != null)
            {
                timeout = data.Timeout;
                if (data.Calling != null)
                {
                    IMessage r = data.Calling(data.Target, mcm);
                    if (r != null)
                    {
                        return(r);
                    }
                }
            }

            IMessage res = null;

            if (timeout != -1)
            {
                ManualResetEvent ev = new ManualResetEvent(false);
                ThreadPool.QueueUserWorkItem(delegate {
                    res = ((IMessageSink)nextSink).SyncProcessMessage(msg);
                    ev.Set();
                });
                if (!ev.WaitOne(timeout, false))
                {
                    timedOut = true;
                    res      = new ReturnMessage(null, null, 0, mcm.LogicalCallContext, mcm);
                }
            }
            else
            {
                res = ((IMessageSink)nextSink).SyncProcessMessage(msg);
            }

            if (data != null && data.Called != null)
            {
                IMessage cr = data.Called(data.Target, mcm, res as IMethodReturnMessage, timedOut);
                if (cr != null)
                {
                    res = cr;
                }
            }

            return(res);
        }
示例#7
0
        public void Start(IList <string> userAssemblyPaths = null)
        {
            lock (this) {
                if (starting)
                {
                    return;
                }
                starting = true;
                exitRequestEvent.Reset();

                RemotingService.RegisterRemotingChannel();

                BinaryFormatter bf   = new BinaryFormatter();
                ObjRef          oref = RemotingServices.Marshal(this);
                MemoryStream    ms   = new MemoryStream();
                bf.Serialize(ms, oref);
                string sref    = Convert.ToBase64String(ms.ToArray());
                string tmpFile = null;

                if (executionHandlerFactory == null)
                {
                    executionHandlerFactory = Runtime.SystemAssemblyService.CurrentRuntime.GetExecutionHandler();
                }

                try {
                    string location = Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location);
                    location = Path.Combine(location, "mdhost.exe");

                    tmpFile = Path.GetTempFileName();
                    StreamWriter sw = new StreamWriter(tmpFile);
                    sw.WriteLine(sref);
                    sw.WriteLine(Process.GetCurrentProcess().Id);
                    sw.WriteLine(Runtime.SystemAssemblyService.CurrentRuntime.RuntimeId);

                    // Explicitly load Mono.Addins since the target runtime may not have it installed
                    sw.WriteLine(2);
                    sw.WriteLine(typeof(AddinManager).Assembly.Location);
                    sw.WriteLine(typeof(Mono.Addins.Setup.SetupService).Assembly.Location);
                    sw.Close();

                    string arguments           = string.Format("{0} \"{1}\"", id, tmpFile);
                    DotNetExecutionCommand cmd = new DotNetExecutionCommand(location, arguments, AppDomain.CurrentDomain.BaseDirectory);
                    if (userAssemblyPaths != null)
                    {
                        cmd.UserAssemblyPaths = userAssemblyPaths;
                    }
                    cmd.DebugMode = isDebugMode;
                    ProcessHostConsole cons = new ProcessHostConsole();
                    process = executionHandlerFactory.Execute(cmd, cons);
                    Counters.ExternalHostProcesses++;

                    process.Completed += ProcessExited;
                } catch (Exception ex) {
                    if (tmpFile != null)
                    {
                        try {
                            File.Delete(tmpFile);
                        } catch {
                        }
                    }
                    LoggingService.LogError(ex.ToString());
                    throw;
                }
            }
        }
示例#8
0
 internal void Dispose()
 {
     RemotingService.Dispose();
 }