【问题标题】:How to integrate topshelf to an existing windows service project?如何将 topshelf 集成到现有的 Windows 服务项目中?
【发布时间】:2018-05-01 06:50:53
【问题描述】:

我希望能够在 Visual Studio 中使用我的服务的 TopShelf 调试功能。

很多examplesdocumentation 指的是在Visual Studio 中创建一个Windows 控制台项目首先,然后然后 添加TopShelf、OWIN 等

但是,在我的情况下,我已经有一个非常好的和工作的 Windows 服务项目,称为 QShipsService.sln 等......它使用一个简单的连接服务(诚然是旧的 SOAP 遗留服务)。

有人可以指导我或提供如何使用 TopShelf 的示例,以及现有的非控制台类似项目吗?

【问题讨论】:

  • 如果你现有的项目已经是windows服务,还需要TopShelf做什么?
  • @ZoharPeled 错误地调试服务。现在请删除该反对票。
  • 任何人都可能投反对票。
  • @RobertHarvey SO 的痛苦
  • 对不起,不是我的投票。我不知道这会有什么帮助。我知道您可以在调试模式下运行服务,但我不记得它实际上是如何完成的。使用 TopShelf,您只需按 F5,由于代码是控制台应用程序,因此与任何其他控制台应用程序一样易于调试。

标签: c# visual-studio windows-services topshelf


【解决方案1】:

我找到了自己的解决方案...

我所做的假设是默认的 Windows 服务项目默认希望将程序注册为服务并在服务运行后启动 OnOpen()OnClose() 方法。

在我的例子中,我想重用基于 Timer() 的现有服务,它会每 4 小时启动一次 SOAP 调用并返回一些数据。我没有意识到ServiceConfigurator 试图调用它自己的Open()Close() 方法。

所以我注释掉了OnOpenOnClose 方法,并允许配置器通过Open() 方法调用我的工作进程,这就是我第一次要做的事情!

对于像我这样的菜鸟,这里是代码...

//using System.ServiceProcess;
using Topshelf;

namespace QShipsService
{
    static class Program
    {
        static void Main(string[] args)
        {
            HostFactory.Run(
                configure =>
                {
                    configure.Service<QShipsService.QshipsService>(
                        service =>
                        {
                            service.ConstructUsing(s => new QShipsService.QshipsService());
                            service.WhenStarted(s => s.QStart());
                            service.WhenStopped(s => s.QStop());
                        });

                    //Setup Account that window service use to run.
                    configure.RunAsLocalSystem();

                    //add details and names about the service
                    configure.SetServiceName("QshipsService");
                    configure.SetDisplayName("QshipsService");
                    configure.SetDescription("QshipsService Windows Service to extract data from the QSHIPS SOAP service. Data is recorded and maintained inside the SPOT's database in POT-DB.");
                });


            //## USE THIS IF WE'RE NOT USING TOPSHELF !! ##
            //    //this loads and starts the QshipsService (see QshipsService.cs program)
            //    ServiceBase[] ServicesToRun;
            //    ServicesToRun = new ServiceBase[]
            //    {
            //        new QShipsService.QshipsService()
            //    };
            //    ServiceBase.Run(ServicesToRun);
        }
    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-09-28
    • 1970-01-01
    • 2017-02-22
    • 2018-07-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-11-20
    相关资源
    最近更新 更多