基于this question上的示例,该示例处理将自定义参数传递到Topshelf,我现在希望能够干净利落地退出Topshelf。
我有以下代码,它确实可以工作,但是当它“返回”时,控制台显示一个丑陋的错误,声明为Topshelf.HostFactory Error: 0 : An exception occurred creating the host... The service was not properly configured... ServiceBuilderFactory must not be null
我应该使用什么而不是return来简单地告诉Topshelf退出并且不做任何事情?
string foo = null;
HostFactory.Run(x =>
{
x.AddCommandLineDefinition("foo", f => { foo = f; });
x.ApplyCommandLine();
if (!string.IsNullOrEmpty(foo))
{
Console.WriteLine("A value for Foo was received... exiting.");
return;
}
x.Service<MyService>(s =>
{
s.ConstructUsing(() => new MyService());
s.WhenStarted(z => z.Start());
s.WhenStopped(z => z.Stop());
});
x.StartAutomatically();
});发布于 2015-07-20 00:24:28
在这种情况下,你不应该在你的代码中调用.ApplyCommandLine(),这是由Topshelf自动处理的。重要的是要认识到,此时您正在配置主机,并且不应该抛出异常。
命令行值检查的最佳位置是ConstructUsing()方法,您可以在该方法中验证命令行参数是否存在。如果您的条件不满足,则抛出异常,服务将无法启动。
如果在其他地方执行此操作,则在没有指定该命令行参数的情况下,用于install/uninstall/etc.的命令行选项将不起作用。
https://stackoverflow.com/questions/31298125
复制相似问题