コード例 #1
0
        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
ファイル: ServiceExtensions.cs プロジェクト: zyj0021/Topshelf
        public static ServiceBuilderFactory CreateServiceBuilderFactory <TService>(Action <ServiceConfigurator <TService> > callback)
            where TService : class
        {
            if (callback == null)
            {
                throw new ArgumentNullException(nameof(callback));
            }

            var serviceConfigurator = new DelegateServiceConfigurator <TService>();

            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
        private static TestHost BuildTestHost(TestServicewithStopSupport servicewithStopSupport, Action <LeaderConfigurationBuilder <TestServicewithStopSupport> > builder)
        {
            var serviceConfigurator = new DelegateServiceConfigurator <TestServicewithStopSupport>();

            serviceConfigurator.ConstructUsing(() => servicewithStopSupport);
            serviceConfigurator.WhenStartedAsLeader(builder);
            serviceConfigurator.WhenStopped((testService, control) =>
            {
                testService.Stop();
                return(true);
            });

            var host = new TestHost(
                new WindowsHostSettings(),
                new WindowsHostEnvironment(new HostConfiguratorImpl()),
                serviceConfigurator.Build().Build(new WindowsHostSettings()));

            return(host);
        }