Bre*_*lds 60 .net windows-services topshelf
使用Visual Studio Express 2012,我使用Topshelf(版本3.1.107.0)创建了一个控制台应用程序.该应用程序作为控制台应用程序,但我无法弄清楚如何将其作为服务安装.我从Visual Studio(Build,Publish)中发布了该项目,以管理员身份启动了命令提示符,导航到发布应用程序的文件夹,然后从命令提示符运行setup.exe -install.应用程序已安装并运行,但作为控制台应用程序,而不是Windows服务.我在这里错过了什么?
对于那些可能不熟悉Topshelf的人来说,它是.Net的Windows服务框架,应该促进我上面描述的场景 - 开发和调试作为控制台应用程序,部署为Windows服务.请参阅http://docs.topshelf-project.com/en/latest/index.html上的文档.
Tra*_*vis 76
运行您service.exe install的安装服务.
有关更多信息,请参阅Topshelf命令行参考文档.
Jon*_*ess 30
cmd.exe以管理员身份运行导航控制台到
.\myConsoleApplication\bin\Release\
Run Code Online (Sandbox Code Playgroud)运行命令
.\myConsoleApplication.exe install
Run Code Online (Sandbox Code Playgroud)运行命令
.\myConsoleApplication.exe start
Run Code Online (Sandbox Code Playgroud)码:
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");
});
}
}
}
Run Code Online (Sandbox Code Playgroud)