我正在尝试创建一个依赖于命令行输入的顶级服务。但是当使用命令行参数的配置时,服务不能“及时启动”。
我的服务配置:
如果删除语言设置的配置并对值进行硬编码,服务就会正常启动......有什么想法吗?
HostFactory.Run(conf =>
{
CultureInfo language = null;
conf.AddCommandLineDefinition("language", lang => { language = new CultureInfo(lang); });
conf.ApplyCommandLine();
var countryCode = new RegionInfo(language.Name).TwoLetterISORegionName.ToUpper();
conf.SetDescription("{0} Order Broker".FormatWith(countryCode));
conf.SetDisplayName("{0} Order Broker".FormatWith(countryCode));
conf.SetServiceName("{0}OrderBroker".FormatWith(countryCode));
conf.StartAutomatically();
conf.RunAsLocalSystem();
conf.Service<IOrderService>(svc =>
{
svc.ConstructUsing(name => Creator.Current.Create<IOrderService>());
svc.WhenStarted(service => service.Start(language));
svc.WhenStopped(service => service.Stop());
});
});
这是堆栈跟踪:
System.ComponentModel.Win32Exception: The service did not respond to the start
or control request in a timely fashion --- End of inner exception stack trace ---
at System.ServiceProcess.ServiceController.Start(String[] args)
at System.ServiceProcess.ServiceController.Start()
at Topshelf.Runtime.Windows.WindowsHostEnvironment.StartService(String serviceName)
at Topshelf.Hosts.StartHost.Run()
答案 0 :(得分:0)
所以这里的问题是当它作为服务启动时没有提供语言。您需要编辑服务的图像路径以包含该命令行参数。我认为你从app.config中读取它会好得多。