将控制台应用程序安装为 Windows 服务
我正在编写一个基于 TopShelf 的简单 Windows 服务。如何将我的应用程序安装为服务?我尝试执行SpyService.exe install
,但它不起作用。
接下来两种配置服务的方式有什么区别?
var cfg = RunnerConfigurator.New(
x =>
{
x.ConfigureService<SpyService>(s =>
{
s.Named("SpyService");
s.HowToBuildService(name => new SpyService());
s.WhenStarted(tc => {
XmlConfigurator.ConfigureAndWatch(new FileInfo(".\\log4net.config"));
tc.Start(); });
s.WhenStopped(tc => tc.Stop());
});
x.RunAsFromInteractive();
x.SetDescription("Сервис логирования действий пользователя.");
x.SetDisplayName("SpyService");
x.SetServiceName("SpyService");
});
Runner.Host(cfg, args);
我
var host = HostFactory.New(x =>
{
x.Service<SpyService>(s =>
{
s.SetServiceName("SpyService");
s.ConstructUsing(name => new SpyService());
s.WhenStarted(service =>
{
XmlConfigurator.ConfigureAndWatch(new FileInfo(".\\log4net.config"));
service.Start();
});
s.WhenStopped(service => service.Stop());
});
x.RunAsLocalSystem();
x.SetDescription("Сервис логирования действий пользователя.");
x.SetDisplayName("SpyService");
x.SetServiceName("SpyService");
});
host.Run();
注意到,如果我使用第二种方法,服务已成功安装,但无法像第一种方法那样使用 x.RunAsFromInteractive()
启动服务。
I'm writing a simple Windows Service based on TopShelf. How to install my application as a service? I tried to execute SpyService.exe install
, but it doesn't work.
What is the difference between next two ways of configuring the service?
var cfg = RunnerConfigurator.New(
x =>
{
x.ConfigureService<SpyService>(s =>
{
s.Named("SpyService");
s.HowToBuildService(name => new SpyService());
s.WhenStarted(tc => {
XmlConfigurator.ConfigureAndWatch(new FileInfo(".\\log4net.config"));
tc.Start(); });
s.WhenStopped(tc => tc.Stop());
});
x.RunAsFromInteractive();
x.SetDescription("Сервис логирования действий пользователя.");
x.SetDisplayName("SpyService");
x.SetServiceName("SpyService");
});
Runner.Host(cfg, args);
and
var host = HostFactory.New(x =>
{
x.Service<SpyService>(s =>
{
s.SetServiceName("SpyService");
s.ConstructUsing(name => new SpyService());
s.WhenStarted(service =>
{
XmlConfigurator.ConfigureAndWatch(new FileInfo(".\\log4net.config"));
service.Start();
});
s.WhenStopped(service => service.Stop());
});
x.RunAsLocalSystem();
x.SetDescription("Сервис логирования действий пользователя.");
x.SetDisplayName("SpyService");
x.SetServiceName("SpyService");
});
host.Run();
I noticed that if I use the second method the service is successfully installed, but there is not possible to start the service with x.RunAsFromInteractive()
as in first way.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您使用的是哪个版本的 Topshelf?旧语法是
SpyService.exe service install
,但已被简化。What version of Topshelf are you using? The old syntax was
SpyService.exe service install
but has been simplified.afaik,第一种方法在最新版本(2.2)中已过时。
关于 RunAsFromInteractive(),查看 topshelf 源代码,我看到它使用空用户名/密码调用 RunAs():
The first approach is obsoleted in the latest version (2.2), afaik.
Regarding RunAsFromInteractive(), looking at the topshelf source code, I see that it called RunAs() with empty username/password:
您可以先以管理员身份运行控制台命令,然后运行安装命令
You can run the Console Command as Administrator first, then run the install command