c# – 如何使用TopShelf無法識別的CommandLine引數?
當我安裝並通過TopShelf作為Windows服務啟動它時,我想傳遞一些自定義引數到控制檯應用程式.
當我使用:
MyService install start /fooBar: Test
控制檯應用程式失敗:
[Failure] Command Line An unknown command-line option was found:DEFINE: fooBar = Test
題:
如何讓我的論據被TopShelf所認可,以便我可以消耗其價值觀?
編輯:這僅在執行.exe時起作用,而不是當作為服務執行時.作為替代,您可以將該選項新增為配置值,並在啟動時讀取(這可能是更好的做法):
using System.Configuration; // snip string foobar = null; HostFactory.Run(configurator => { foobar = ConfigurationManager.AppSettings["foobar"]; // do something with fooBar configurator.Service<ServiceClass>(settings => { settings.ConstructUsing(s => GetInstance<ServiceClass>()); settings.WhenStarted(s => s.Start()); settings.WhenStopped(s => s.Stop()); }); configurator.RunAsLocalService(); configurator.SetServiceName("ServiceName"); configurator.SetDisplayName("DisplayName"); configurator.SetDescription("Description"); configurator.StartAutomatically(); });
根據文件,您需要在此模式中指定命令:
-foobar:Test
您還需要在服務配置中新增定義:
string fooBar = null; HostFactory.Run(configurator => { configurator.AddCommandLineDefinition("fooBar", f=> { fooBar = f; }); configurator.ApplyCommandLine(); // do something with fooBar configurator.Service<ServiceClass>(settings => { settings.ConstructUsing(s => GetInstance<ServiceClass>()); settings.WhenStarted(s => s.Start()); settings.WhenStopped(s => s.Stop()); }); configurator.RunAsLocalService(); configurator.SetServiceName("ServiceName"); configurator.SetDisplayName("DisplayName"); configurator.SetDescription("Description"); configurator.StartAutomatically(); });
http://stackoverflow.com/questions/15004212/how-can-i-use-commandline-arguments-that-is-not-recognized-by-topshelf