Exemplo n.º 1
0
        private static int Main(string[] args)
        {
            AppDomain.CurrentDomain.UnhandledException += CurrentDomain_UnhandledException;
            TaskScheduler.UnobservedTaskException      += TaskScheduler_UnobservedTaskException;

            var loggerFuncs = new DelegateLogger();

            loggerFuncs.Add(WriteLogMessageToConsole);

            var logger = new AggregateLogger();

            logger.Add(loggerFuncs);

            try
            {
                using (var server = new ApplicationServer())
                {
                    if (!server.IsInitialized)
                    {
                        GlobalConsole.Current.WriteLine("Initializing server... ");

                        var srvCtx = new SimpleAppServerContext(server);

                        var initCtx = new SimpleAppServerInitContext();
                        initCtx.Arguments     = args;
                        initCtx.Logger        = logger;
                        initCtx.ServerContext = srvCtx;

                        foreach (var a in args)
                        {
                            if (a.ToLower().Trim().StartsWith("/rootdir:"))
                            {
                                // custom root/working directory
                                initCtx.WorkingDirectory = new DirectoryInfo(a.Substring(a.IndexOf(':') + 1)
                                                                             .TrimStart()).FullName;
                            }
                        }

                        server.Initialize(initCtx);
                        GlobalConsole.Current.WriteLine("Server has been initialized.");

                        srvCtx.InnerServiceLocator = server.GlobalServiceLocator;
                    }

                    GlobalConsole.Current.WriteLine("Starting server... ");
                    server.Start();
                    GlobalConsole.Current.WriteLine("Server has been started.");

                    IMenuHandler currentMenu = new RootMenu();

                    string line;
                    while (true)
                    {
                        if (currentMenu == null)
                        {
                            break;
                        }

                        GlobalConsole.Current.Clear();
                        currentMenu.DrawMenu();

                        var inputIsValid = false;
                        while (!inputIsValid)
                        {
                            GlobalConsole.Current.WriteLine();
                            GlobalConsole.Current.Write("> ");

                            if (currentMenu.WaitsForInput)
                            {
                                line = GlobalConsole.Current.ReadLine();
                            }
                            else
                            {
                                line = null;
                            }

                            IMenuHandler temp;
                            if (!(inputIsValid = currentMenu.HandleInput(line, out temp)))
                            {
                                GlobalConsole.Current.WriteLine();
                                GlobalConsole.Current.WriteLine("Invalid input!");
                            }
                            else
                            {
                                currentMenu = temp;
                            }
                        }
                    }

                    GlobalConsole.Current.Write("Shutting down server... ");
                }
                GlobalConsole.Current.WriteLine("[OK]");

                return(0);
            }
            catch (Exception ex)
            {
                GlobalConsole.Current
                .WriteLine(ex.GetBaseException() ?? ex);

                return(1);
            }
        }
Exemplo n.º 2
0
        // Protected Methods (2) 

        /// <summary>
        ///
        /// </summary>
        /// <see cref="ServiceBase.OnStart(string[])" />
        protected override void OnStart(string[] args)
        {
            var      asmDir     = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
            TimeSpan?startDelay = null;

            var workDir = asmDir;

            foreach (var a in args)
            {
                if (a.ToLower().Trim().StartsWith("/rootdir:"))
                {
                    // custom root/working directory
                    var dir = new DirectoryInfo(a.Substring(a.IndexOf(':') + 1)
                                                .TrimStart()).FullName;

                    if (Path.IsPathRooted(dir))
                    {
                        workDir = new DirectoryInfo(dir).CreateDirectoryDeep().FullName;
                    }
                    else
                    {
                        workDir = new DirectoryInfo(Path.Combine(asmDir,
                                                                 dir)).CreateDirectoryDeep().FullName;
                    }
                }
                else if (a.ToLower().Trim().StartsWith("/startdelay:"))
                {
                    var seconds = a.Substring(a.IndexOf(':') + 1)
                                  .Trim();
                    if (seconds == string.Empty)
                    {
                        seconds = "15";
                    }

                    startDelay = TimeSpan.FromSeconds(double.Parse(seconds,
                                                                   CultureInfo.InvariantCulture));
                }
            }

            if (startDelay.HasValue)
            {
                Thread.Sleep(startDelay.Value);
            }

            this.EventLog
            .WriteEntry(string.Format("Root directory is: {0}",
                                      workDir),
                        EventLogEntryType.Information);

            this.LogDirectory = new DirectoryInfo(Path.Combine(workDir,
                                                               "logs")).CreateDirectoryDeep()
                                .FullName;

            this.EventLog
            .WriteEntry(string.Format("Log directory is: {0}",
                                      this.LogDirectory),
                        EventLogEntryType.Information);

            GlobalConsole.SetConsole(new ServiceConsole(this));

            var loggerFuncs = new DelegateLogger();

            loggerFuncs.Add(this.WriteEventLogMessage);

            var logger = new AggregateLogger();

            logger.Add(new AsyncLogger(loggerFuncs));

            var server = new ApplicationServer();

            try
            {
                if (!server.IsInitialized)
                {
                    var srvCtx = new SimpleAppServerContext(server);

                    var initCtx = new SimpleAppServerInitContext();
                    initCtx.Arguments        = args;
                    initCtx.Logger           = logger;
                    initCtx.ServerContext    = srvCtx;
                    initCtx.WorkingDirectory = workDir;

                    try
                    {
                        server.Initialize(initCtx);

                        this.EventLog
                        .WriteEntry("Server has been initialized.",
                                    EventLogEntryType.Information);
                    }
                    catch (Exception ex)
                    {
                        this.EventLog
                        .WriteEntry(string.Format("Server could not be initialized!{0}{0}{1}",
                                                  Environment.NewLine,
                                                  ex.GetBaseException() ?? ex),
                                    EventLogEntryType.Error);

                        throw;
                    }

                    srvCtx.InnerServiceLocator = server.GlobalServiceLocator;
                }

                try
                {
                    server.Start();

                    this.EventLog
                    .WriteEntry("Server has been started.",
                                EventLogEntryType.Information);
                }
                catch (Exception ex)
                {
                    this.EventLog
                    .WriteEntry(string.Format("Server could not be started!{0}{0}{1}",
                                              Environment.NewLine,
                                              ex.GetBaseException() ?? ex),
                                EventLogEntryType.Error);

                    throw;
                }

                this.Server = server;
            }
            catch
            {
                server.Dispose();

                throw;
            }
        }