示例#1
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;
            }
        }