将Topshelf应用程序安装为Windows服务

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命令行参考文档.

  • 谢谢!我的错误是我试图安装发布应用程序时创建的 setup.exe,而我应该在 bin\release 文件夹中安装 servicename.exe 文件!哦!现在都在工作。 (2认同)

Jon*_*ess 30

  1. 启动Visual Studio并创建一个新的C#控制台应用程序
  2. 右键单击引用并转到管理NuGet-Packages
  3. 通过NuGet下载并安装Topshelf
  4. 将下面的代码粘贴到您的应用程序中并包含所有导入.
  5. 从"调试"模式切换到"发布"并构建应用程序.
  6. cmd.exe以管理员身份运行
  7. 导航控制台到

    .\myConsoleApplication\bin\Release\
    
    Run Code Online (Sandbox Code Playgroud)
  8. 运行命令

    .\myConsoleApplication.exe install
    
    Run Code Online (Sandbox Code Playgroud)
  9. 运行命令

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


Sun*_*Awa 7

浏览到该文件夹​​并运行命令:

AppName.exe install
Run Code Online (Sandbox Code Playgroud)

您必须以管理员身份运行命令提示符.