C# 如何将topshelf集成到现有的windows服务项目中?

C# 如何将topshelf集成到现有的windows服务项目中?,c#,visual-studio,windows-services,topshelf,C#,Visual Studio,Windows Services,Topshelf,我希望能够在VisualStudio中使用我的服务的TopShelf调试功能 其中很多都是指先在VisualStudio中创建Windows控制台项目,然后再添加TopShelf、OWIN等 然而,在我的例子中,我已经有了一个非常好的Windows服务项目,名为QShipsService.sln,等等。。。并且它使用一个简单的连接服务(当然是旧的SOAP遗留服务) 有人能告诉我或提供一个如何使用TopShelf的例子,以及现有的非控制台类项目吗?我找到了自己的解决方案 我所做的假设是默认的Win

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

其中很多都是指先在VisualStudio中创建Windows控制台项目,然后再添加TopShelf、OWIN等

然而,在我的例子中,我已经有了一个非常好的Windows服务项目,名为QShipsService.sln,等等。。。并且它使用一个简单的连接服务(当然是旧的SOAP遗留服务)


有人能告诉我或提供一个如何使用TopShelf的例子,以及现有的非控制台类项目吗?

我找到了自己的解决方案

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

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

因此,我注释掉了
OnOpen
OnClose
方法,并允许配置程序通过
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);
        }
    }
}
//使用System.ServiceProcess;
使用Topshelf;
命名空间QShipsService
{
静态类程序
{
静态void Main(字符串[]参数)
{
霍斯特工厂,快跑(
配置=>
{
配置服务(
服务=>
{
ConstructUsing(s=>newqshipsservice.QShipsService());
开始时(s=>s.QStart());
service.WhenStopped(s=>s.QStop());
});
//Windows服务用于运行的安装程序帐户。
configure.RunAsLocalSystem();
//添加有关服务的详细信息和名称
configure.SetServiceName(“QshipsService”);
configure.SetDisplayName(“QshipsService”);
configure.SetDescription(“用于从QSHIPS SOAP服务提取数据的QshipsService Windows服务。数据在POT-DB中的SPOT数据库中记录和维护”);
});
//##如果我们不使用TOPSHELF,请使用此选项##
////这将加载并启动QshipsService(请参阅QshipsService.cs程序)
//ServiceBase[]ServicesToRun;
//ServicesToRun=新的ServiceBase[]
//    {
//新的QShipsService.QShipsService()
//    };
//ServiceBase.Run(ServicesToRun);
}
}
}

如果您现有的项目已经是windows服务,您需要TopShelf做什么?@ZoharPeled err调试该服务。现在请取消否决票。任何人都有可能投否决票。@RobertHarvey SOSorry的痛苦,不是我的donevote。我不知道这会有什么帮助。我知道你可以在debog模式下运行服务,但我不记得它实际上是如何运行的。使用TopShelf,您只需点击F5,因为代码是一个控制台应用程序,它与任何其他控制台应用程序一样易于调试。