.net 将 Topshelf 应用程序安装为 Windows 服务
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18206738/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Installing a Topshelf application as a Windows service
提问by Brendan Reynolds
Using Visual Studio Express 2012, I've created a console application using Topshelf (Version 3.1.107.0). The application works as a console application, but I can't figure out how to install it as a service. I've published the project from within Visual Studio (Build, Publish), started a command prompt as Administrator, navigated to the folder where the application was published, and run setup.exe -install from the command prompt. The application is installed and runs, but as a console application, not a Windows service. What am I missing here?
使用 Visual Studio Express 2012,我使用 Topshelf(版本 3.1.107.0)创建了一个控制台应用程序。该应用程序用作控制台应用程序,但我不知道如何将其安装为服务。我已经从 Visual Studio(构建、发布)中发布了项目,以管理员身份启动了命令提示符,导航到应用程序发布所在的文件夹,然后从命令提示符运行 setup.exe -install。该应用程序已安装并运行,但作为控制台应用程序,而不是 Windows 服务。我在这里缺少什么?
For those who may not be familiar with Topshelf, it is a Windows Service framework for .Net and is supposed to facilitate the scenario I describe above - develop and debug as a console application, deploy as a Windows Service. See the documentation at http://docs.topshelf-project.com/en/latest/index.html.
对于那些可能不熟悉 Topshelf 的人来说,它是一个用于 .Net 的 Windows 服务框架,应该有助于我上面描述的场景 - 作为控制台应用程序开发和调试,作为 Windows 服务部署。请参阅http://docs.topshelf-project.com/en/latest/index.html 上的文档。
回答by Travis
Run your service.exe installto install the service.
运行您service.exe install的安装服务。
See the Topshelf Command Line Referencedocumentation for more information.
有关详细信息,请参阅Topshelf 命令行参考文档。
回答by Jonas_Hess
- Start Visual Studio and create a new C# Console-Application
- Right click on references and go to manage NuGet-Packages
- Download and install Topshelf via NuGet
- Paste the Code below into your application and include all imports.
- Switch from “Debug” mode to “Release” and build the application.
- Run
cmd.exeas administrator Navigate the console to
.\myConsoleApplication\bin\Release\Run the command
.\myConsoleApplication.exe installRun the command
.\myConsoleApplication.exe start
- 启动 Visual Studio 并创建一个新的 C# 控制台应用程序
- 右键单击引用并转到管理 NuGet-Packages
- 通过 NuGet 下载并安装 Topshelf
- 将下面的代码粘贴到您的应用程序中并包含所有导入。
- 从“调试”模式切换到“发布”并构建应用程序。
cmd.exe以管理员身份运行将控制台导航到
.\myConsoleApplication\bin\Release\运行命令
.\myConsoleApplication.exe install运行命令
.\myConsoleApplication.exe start
Code:
代码:
using System;
using System.Threading;
using Topshelf;
using Topshelf.Runtime;
namespace MyConsoleApplication
{
public class MyService
{
public MyService(HostSettings settings)
{
}
private SemaphoreSlim _semaphoreToRequestStop;
private Thread _thread;
public void Start()
{
_semaphoreToRequestStop = new SemaphoreSlim(0);
_thread = new Thread(DoWork);
_thread.Start();
}
public void Stop()
{
_semaphoreToRequestStop.Release();
_thread.Join();
}
private void DoWork()
{
while (true)
{
Console.WriteLine("doing work..");
if (_semaphoreToRequestStop.Wait(500))
{
Console.WriteLine("Stopped");
break;
}
}
}
}
public class Program
{
public static void Main()
{
HostFactory.Run(x =>
{
x.StartAutomatically(); // Start the service automatically
x.EnableServiceRecovery(rc =>
{
rc.RestartService(1); // restart the service after 1 minute
});
x.Service<MyService>(s =>
{
s.ConstructUsing(hostSettings => new MyService(hostSettings));
s.WhenStarted(tc => tc.Start());
s.WhenStopped(tc => tc.Stop());
});
x.RunAsLocalSystem();
x.SetDescription("MyDescription");
x.SetDisplayName("MyDisplayName");
x.SetServiceName("MyServiceName");
});
}
}
}
回答by Sunny Okoro Awa
Browse to the folder and run the command:
浏览到文件夹并运行命令:
AppName.exe install
You must run your command prompt as an Admin.
您必须以管理员身份运行命令提示符。
回答by granadaCoder
So this is an old question, but I want to add some command line options.
所以这是一个老问题,但我想添加一些命令行选项。
MyTopShelfImplementation.exe install -servicename "MyServiceName" -displayname "My Display Name" --autostart start
MyTopShelfImplementation.exe install -servicename "MyServiceName" -displayname "My Display Name" --autostart start
.
.
--autostart
- 自动开启
is for when windows reboots.
用于 Windows 重新启动时。
start
开始
is for starting the service IMMEDIATELY after you install it
用于在安装后立即启动服务
Now, the "names" you can also specify in code
现在,您还可以在代码中指定“名称”
HostFactory.Run(x =>
{
////x.SetDescription("My Description");
x.SetDisplayName("My Display Name");
x.SetServiceName("My Service Name");
////x.SetInstanceName("My Instance");
So if the .exe runs as console app (or as windows service) may be some combination of setting these values in code and/or passing them in via the command line.
因此,如果 .exe 作为控制台应用程序(或作为 Windows 服务)运行,则可能是在代码中设置这些值和/或通过命令行传递它们的某种组合。
I would expect if you did not set the "names" in code AND you did not pass the "names" in by command line args, then you'll get console behavior.
我希望如果您没有在代码中设置“名称”并且没有通过命令行参数传递“名称”,那么您将获得控制台行为。

