Example #1
0
 public virtual void Start()
 {
     /*
      * Create a new AppDomain containing the Host, and begin handling incoming requests.
      */
     if (_sock != null)
     {
         return;
     }
     _openConnections = 0;
     SetStatus(ServerStatus.Starting);
     try {
         _domainHook = _appManager.CreateObject(_appId, typeof(DomainHook), _virtualDir, _physicalDir, false) as DomainHook;
         _domainHook.Configure(_virtualDir, _physicalDir, _serverVariables);
         _domainHook.RegisterILogger(Logger.Instance);
         _sock = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
         _sock.Bind(new IPEndPoint(_endPoint, _port));
         _sock.Listen(100);
         _sock.BeginAccept(new AsyncCallback(ProcessRequest), _sock);
     } catch {
         SetStatus(ServerStatus.Stopped);
         throw;
     }
     SetStatus(ServerStatus.Running);
 }
Example #2
0
        private void Execute(object objArgs)
        {
            string cwd = null;

            try
            {
                _exception = null;
                cwd        = Environment.CurrentDirectory;
                StartArguments args = (StartArguments)objArgs;

                ObjectHandle obj = _workerDomain.CreateInstanceFrom(GetType().Assembly.Location, typeof(DomainHook).FullName);
                if (obj == null)
                {
                    throw new ApplicationException("Unable to hook child application domain.");
                }
                using (DomainHook hook = (DomainHook)obj.Unwrap())
                {
                    hook.Capture(args.StdInput == null ? String.Empty : args.StdInput.ReadToEnd());
                    try
                    {
                        string[] arguments = args.Arguments;
                        Environment.CurrentDirectory = WorkingDirectory;
#if NET20 || NET35
                        _exitCode = _workerDomain.ExecuteAssembly(_executable, AppDomain.CurrentDomain.Evidence, arguments);
#else
                        _exitCode = _workerDomain.ExecuteAssembly(_executable, arguments);
#endif
                    }
                    finally
                    {
                        string line, stdout, stderr;
                        hook.GetOutput(out stdout, out stderr);
                        if (_outputReceived != null)
                        {
                            using (StringReader r = new StringReader(stdout))
                                while (null != (line = r.ReadLine()))
                                {
                                    _outputReceived(this, new ProcessOutputEventArgs(line, false));
                                }
                            using (StringReader r = new StringReader(stderr))
                                while (null != (line = r.ReadLine()))
                                {
                                    _outputReceived(this, new ProcessOutputEventArgs(line, true));
                                }
                        }
                    }
                }
            }
            catch (ThreadAbortException) { return; }
            catch (Exception e)
            {
                _exitCode = -1;
                try
                {
                    ThreadStart savestack = Delegate.CreateDelegate(typeof(ThreadStart), e, "InternalPreserveStackTrace", false, false) as ThreadStart;
                    if (savestack != null)
                    {
                        savestack();
                    }
                    _exception = e;
                }
                catch { _exception = new TargetInvocationException(e); }
            }
            finally
            {
                _isRunning = false;
                _running   = null;
                if (cwd != null)
                {
                    Environment.CurrentDirectory = cwd;
                }

                if (_processExited != null)
                {
                    _processExited(this, new ProcessExitedEventArgs(_exitCode));
                }
            }
        }