官方文档:http://docs.topshelf-project.com/en/latest/overview/commandline.html
nuget 包:Topshelf
Topshelf is a library that simplifies the creation of Windows services using .NET.
Command-Line Reference
service.exe [verb] [-option:value] [-switch]
run Runs the service from the command line (default)
help, --help Displays help
install Installs the service
--autostart The service should start automatically (default)
--disabled The service should be set to disabled
--manual The service should be started manually
--delayed The service should start automatically (delayed)
-instance An instance name if registering the service
multiple times
-username The username to run the service
-password The password for the specified username
--localsystem Run the service with the local system account
--localservice Run the service with the local service account
--networkservice Run the service with the network service permission
--interactive The service will prompt the user at installation for
the service credentials
start Start the service after it has been installed
--sudo Prompts for UAC if running on Vista/W7/2008
-servicename The name that the service should use when
installing
-description The service description the service should use when
installing
-displayname The display name the the service should use when
installing
start Starts the service if it is not already running
stop Stops the service if it is running
uninstall Uninstalls the service
-instance An instance name if registering the service
multiple times
--sudo Prompts for UAC if running on Vista/W7/2008
Examples:
service install
Installs the service into the service control manager
service install -username:joe -password:bob --autostart
Installs the service using the specified username/password and
configures the service to start automatically at machine startup
service uninstall
Uninstalls the service
service install -instance:001
Installs the service, appending the instance name to the service name
so that the service can be installed multiple times. You may need to
tweak the log4net.config to make this play nicely with the log files.
示例命令行
在命令行中使用管理员身份运行
"XX系统服务.exe" install --autostart start
"XX系统服务.exe" uinstall
官方使用文档:https://github.com/Topshelf/Topshelf/blob/develop/doc/source/configuration/config_api.rst
示例代码:
public class TownCrier
{
readonly System.Timers.Timer _timer;
public TownCrier()
{
_timer = new System.Timers.Timer(1000) { AutoReset = true };
_timer.Elapsed += (sender, eventArgs) => Console.WriteLine("It is {0} and all is well", DateTime.Now);
}
public void Start() { _timer.Start(); }
public void Stop() { _timer.Stop(); }
}
main方法
var rc = HostFactory.Run(x => //1
{
x.Service<TownCrier>(s => //2
{
s.ConstructUsing(name => new TownCrier()); //3
s.WhenStarted(tc => tc.Start()); //4
s.WhenStopped(tc => tc.Stop()); //5
});
x.UseLog4Net();
x.RunAsLocalSystem(); //6
x.OnException(ex => { });
x.SetDescription("Sample Topshelf Host"); // 服务描述 //7
x.SetDisplayName("Stuff"); // 显示名称 //8
x.SetServiceName("Stuff"); // 服务名称 //9
}); //10
var exitCode = (int)Convert.ChangeType(rc, rc.GetTypeCode()); //11
Environment.ExitCode = exitCode;
//在这里,我们使用HostFactory设置主机。跑吧。我们打开了一个新的lambda,这里的“x”展示了所有的主机级配置。使用这种方法,可以从环境变量中提取命令参数。我们还捕获服务的返回代码——在第11行返回。
//这里我们告诉Topshelf有一个“TownCrier”类型的服务。这里打开的lambda通过' s '参数公开服务配置选项。
//这告诉Topshelf如何构建服务的实例。目前,我们只是要“新建它”,但我们也可以很容易地从IoC容器中取出它,用一些看起来像“容器”的代码。GetInstance<TownCrier>()'
//Topshelf如何启动服务
//Topshelf如何停止服务
//在这里,我们设置了“运行方式”,并选择了“本地系统”。我们还可以在命令行中使用win from类型提示符进行交互设置,我们还可以将一些用户名 / 密码作为字符串参数传入
//在这里,我们将为要在windows服务监视器中使用的winservice设置描述
//在这里,我们将设置winservice的显示名称,以便在windows服务监视器中使用
//在这里,我们为winservice设置服务名,以便在windows服务监视器中使用
//现在lambda已经关闭,配置将被执行,主机将开始运行。
//最后,我们转换并返回服务退出代码。
|