以 Windows 服务的形式安装 Topsheld 应用程序

我使用 Visual Studio Express 2012创建了一个使用托普架(版本3.1.107.0)的控制台应用。这个应用程序作为一个控制台应用工作,但我不知道如何将它作为一个服务安装。我已经在 Visual Studio (Build,Publish)中发布了该项目,作为 Administrator 启动了命令提示符,导航到发布应用程序的文件夹,并从命令提示符运行 setup.exe-install。该应用程序是安装和运行的,但是作为一个控制台应用,而不是一个 Windows 服务。我错过了什么?

对于那些可能不熟悉 Topsheld 的人来说,它是一个 Windows 服务框架,用于。网络,并被认为是促进我上面描述的场景-开发和调试作为一个控制台应用,部署作为一个 Windows 服务。请参阅 http://docs.topshelf-project.com/en/latest/index.html上的文档。

73793 次浏览

Run your service.exe install to install the service.

See the Topshelf Command Line Reference documentation for more information.

  1. Start Visual Studio and create a new C# Console-Application
  2. Right click on references and go to manage NuGet-Packages
  3. Download and install Topshelf via NuGet
  4. Paste the Code below into your application and include all imports.
  5. Switch from “Debug” mode to “Release” and build the application.
  6. Run cmd.exe as administrator
  7. Navigate the console to

    .\myConsoleApplication\bin\Release\
    
  8. Run the command

    .\myConsoleApplication.exe install
    
  9. Run the command

    .\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");


});
}
}
}

Browse to the folder and run the command:

AppName.exe install

You must run your command prompt as an Admin.

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

.

--autostart

is for when windows reboots.

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.

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.