Esempio n. 1
0
        public void ServiceTestPackageDeployer()
        {
            // Get security settings
            var security         = SecurityManager.CreateDefault();
            var securitySettings = SecurityManager.Serialize(security);

            // Start Package Deployer
            using (var service = new PackageDeployerService("Services\\Package Deployer", true, securitySettings))
            {
                service.Start();

                // Wait a bit
                Thread.Sleep(5000);

                // Test stop and clean-up (dispose)
                service.Stop();
            }
        }
Esempio n. 2
0
        /// <summary>
        /// Starts the services when the Windows service is started.
        /// </summary>
        protected override void OnStart(string[] args)
        {
            // Start service...
            // No service start/success message logging is required because the AutoLog feature is enabled
            // and required to catch messages during .NET AppDomain creation, e.g. assembly load failure.
            try
            {
                // Delay start when option is configured
                var delay = Settings.Default.StartDelay;
                while (delay-- > 0)
                {
                    RequestAdditionalTime(1000);
                    Thread.Sleep(1000);             // Set breakpoint here to debug service start-up
                }

                // Set current directory to service executable path (default is system directory)
                var programDirectory = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
                Debug.Assert(programDirectory != null);
                Directory.SetCurrentDirectory(programDirectory);

                // Get host name from configuration or local DNS name
                var hostName = Settings.Default.HostName;
                if (hostName != null)
                {
                    hostName = hostName.Trim();
                }
                if (String.IsNullOrWhiteSpace(hostName) ||
                    hostName.Equals("localhost", StringComparison.OrdinalIgnoreCase) ||
                    hostName.Equals("127.0.0.1", StringComparison.OrdinalIgnoreCase) ||
                    hostName.Equals("::1", StringComparison.OrdinalIgnoreCase))
                {
                    hostName = NetworkExtensions.GetFullHostName();
                }

                // Get root directory from configuration or service path
                var rootDirectory = Settings.Default.PackageDeployerRootDirectory;
                if (rootDirectory != null)
                {
                    rootDirectory = rootDirectory.Trim();
                }
                rootDirectory = !String.IsNullOrWhiteSpace(rootDirectory)
                                    ? Path.Combine(programDirectory, rootDirectory)
                                    : programDirectory;

                // Get security settings
                var security         = SecurityManager.CreateDefault();
                var securitySettings = SecurityManager.Serialize(security);

                // Start Package Deployer
                RequestAdditionalTime(Settings.Default.ServiceStartTimeout * 1000);
                _packageDeployer = new PackageDeployerService(hostName, Settings.Default.PackageDeployerPort,
                                                              rootDirectory, true, securitySettings);
                _packageDeployer.Error += OnHostedServiceError;
                _packageDeployer.Start();
            }
            catch (ConfigurationException error)
            {
                // Log configuration errors directly to event log (TraceSource will not work as config failed)
                EventLog.WriteEntry(MrdsToolkitConstants.ServiceHostTraceSourceName, error.GetFullMessage(true),
                                    EventLogEntryType.Error);
            }
            catch (Exception error)
            {
                // Log failure
                _log.TraceEvent(TraceEventType.Error, 0, Resources.ServiceFailedToStart,
                                error.GetFullMessage(true));

                // Re-throw exception to cause start to fail
                throw;
            }
        }