public static HostConfigurator Service <TService>(this HostConfigurator configurator,
                                                          Action <ServiceConfigurator <TService> > callback)
            where TService : class
        {
            if (configurator == null)
            {
                throw new ArgumentNullException("configurator");
            }
            if (callback == null)
            {
                throw new ArgumentNullException("callback");
            }

            var serviceConfigurator = new DelegateServiceConfigurator <TService>();

            callback(serviceConfigurator);

            configurator.UseServiceBuilder(x =>
            {
                ConfigurationResult configurationResult =
                    ValidateConfigurationResult.CompileResults(serviceConfigurator.Validate());
                if (configurationResult.Results.Any())
                {
                    throw new HostConfigurationException("The service was not properly configured");
                }

                ServiceBuilder serviceBuilder = serviceConfigurator.Build();

                return(serviceBuilder);
            });

            return(configurator);
        }
예제 #2
0
        public static ServiceBuilderFactory CreateServiceBuilderFactory <TService>(
            Func <HostSettings, TService> serviceFactory,
            Action <ServiceConfigurator> callback)
            where TService : class, ServiceControl
        {
            if (serviceFactory == null)
            {
                throw new ArgumentNullException(nameof(serviceFactory));
            }
            if (callback == null)
            {
                throw new ArgumentNullException(nameof(callback));
            }

            var serviceConfigurator = new ControlServiceConfigurator <TService>(serviceFactory);

            callback(serviceConfigurator);

            ServiceBuilder ServiceBuilderFactory(HostSettings x)
            {
                ConfigurationResult configurationResult = ValidateConfigurationResult.CompileResults(serviceConfigurator.Validate());

                if (configurationResult.Results.Any())
                {
                    throw new HostConfigurationException("The service was not properly configured");
                }

                ServiceBuilder serviceBuilder = serviceConfigurator.Build();

                return(serviceBuilder);
            }

            return(ServiceBuilderFactory);
        }
예제 #3
0
        /// <summary>
        /// Configures a new service host
        /// </summary>
        /// <param name="configureCallback">Configuration method to call</param>
        /// <returns>A Topshelf service host, ready to run</returns>
        public static Host New(Action <HostConfigurator> configureCallback)
        {
            if (configureCallback == null)
            {
                throw new ArgumentNullException("configureCallback");
            }

            var configurator = new HostConfiguratorImpl();

            Type declaringType = configureCallback.Method.DeclaringType;

            if (declaringType != null)
            {
                string defaultServiceName = declaringType.Namespace;
                if (!string.IsNullOrEmpty(defaultServiceName))
                {
                    configurator.SetServiceName(defaultServiceName);
                }
            }

            configureCallback(configurator);

            configurator.ApplyCommandLine();

            ConfigurationResult result = ValidateConfigurationResult.CompileResults(configurator.Validate());

            if (result.Message.Length > 0)
            {
                _log.InfoFormat("Configuration Result:\n{0}", result.Message);
            }

            return(configurator.CreateHost());
        }
예제 #4
0
        /// <summary>
        ///   Configures a new service host
        /// </summary>
        /// <param name="configureCallback"> Configuration method to call </param>
        /// <returns> A Topshelf service host, ready to run </returns>
        public static Host New(Action <HostConfigurator> configureCallback)
        {
            try
            {
                if (configureCallback == null)
                {
                    throw new ArgumentNullException("configureCallback");
                }

                var configurator = new HostConfiguratorImpl();

                Type declaringType = configureCallback.Method.DeclaringType;
                if (declaringType != null)
                {
                    string defaultServiceName = declaringType.Namespace;
                    if (!string.IsNullOrEmpty(defaultServiceName))
                    {
                        configurator.SetServiceName(defaultServiceName);
                    }
                }

                configureCallback(configurator);

                configurator.ApplyCommandLine();

                ConfigurationResult result = ValidateConfigurationResult.CompileResults(configurator.Validate());

                if (result.Message.Length > 0)
                {
                    HostLogger.Get(typeof(HostFactory))
                    .InfoFormat("Configuration Result:\n{0}", result.Message);
                }

                return(configurator.CreateHost());
            }
            catch (Exception ex)
            {
                HostLogger.Get(typeof(HostFactory)).Error("An exception occurred creating the host", ex);
                HostLogger.Shutdown();
                throw;
            }
        }
        /// <summary>
        ///   Configure the host to supervise the service
        /// </summary>
        /// <param name="hostConfigurator"> </param>
        /// <param name="configureCallback"> </param>
        public static HostConfigurator Supervise(this HostConfigurator hostConfigurator,
                                                 Action <SuperviseConfigurator> configureCallback)
        {
            var superviseConfigurator = new SuperviseServiceConfigurator();

            configureCallback(superviseConfigurator);

            hostConfigurator.UseServiceBuilder(x =>
            {
                ConfigurationResult configurationResult =
                    ValidateConfigurationResult.CompileResults(superviseConfigurator.Validate());
                if (configurationResult.Results.Any())
                {
                    throw new HostConfigurationException("The service was not properly configured");
                }

                ServiceBuilder serviceBuilder = superviseConfigurator.Build();

                return(serviceBuilder);
            });

            return(hostConfigurator);
        }