【问题标题】:TopShelf - service starts but nothings worksTopShelf - 服务启动但没有任何效果
【发布时间】:2019-10-08 14:39:42
【问题描述】:

我编写了一个简单的代码,它应该使用 topshelf 自动启动。问题是,如果我从 Visual Studio 运行它,或者当我只是单击 exe 文件上的调试文件夹时,它启动时没有任何问题,但是......当我将它作为服务安装时,它什么也不做......

这是我的代码

using System;
using System.Collections.Generic;
using Topshelf;
using System.IO;

namespace ConsoleCRP
{    
    public class Program
    {
        public static void Main(){
                var rc = HostFactory.Run(x =>{
                    x.Service<ClientReportingScheduler>(s =>                                   
                    {
                        s.ConstructUsing(name => new ClientReportingScheduler());                                    
                        s.WhenStarted(tc => tc.Start());                         
                        s.WhenStopped(tc => tc.Stop());                          
                    });
                    x.StartAutomatically();
                    x.RunAsLocalSystem();                                       

                    x.SetDescription("This is automatic scheduler");                  
                    x.SetDisplayName("scheduler");                                  
                    x.SetServiceName("servicetest");                                  
                });                                                             

                var exitCode = (int)Convert.ChangeType(rc, rc.GetTypeCode());  
                Environment.ExitCode = exitCode;
            }
        }
    public class ClientReportingScheduler
        {
            public System.Timers.Timer _timer;
            public int startJob = 0;

            public ClientReportingScheduler()
            {
                startJob = 1000 * 5 ;

                _timer = new System.Timers.Timer(startJob) { AutoReset = true };
                _timer.Elapsed += (sender, eventArgs) =>
                {
                    Console.WriteLine("Starting scheduler query check at {0}" + Environment.NewLine, DateTime.Now);     
                };
            }

            public void Start()
            {
                _timer.Start();
            }
            public void Stop()
            {
                _timer.Stop();
            }
    }

}

我确实从 topshelf 文档中复制了示例,并且该代码只有一些小的更改。我在这里只粘贴了这个的简单版本(只是为了向你展示问题)。在完整版中,我也使用了 fluentscheduler,并且有很多代码连接到 postgress,获取一些数据并执行一些存储过程,但这不是问题。问题是,即使您在上面看到的这个小代码也根本不起作用。如果您启动新的控制台应用程序并将其粘贴到那里,您现在可以创建此程序(当然还可以从 nuget 添加 topshelf)。如果你启动它,它应该可以工作,但如果你将它作为服务安装,这根本就不起作用......

以下是我在作为服务安装时执行的步骤:
1) 以管理员身份打开 CMD
2) 使用调试进入目录
3) 以这种方式安装: ConsoleCRP.exe install
4) 启动服务:net start servicetest
5)如果要停止服务:net stop servicetest
6)如果要卸载:ConsoleCRP.exe卸载

结果如下:
配置结果:
[成功] 名称服务测试
[成功] DisplayName 调度程序
[成功] 说明这是自动调度器
[成功] ServiceName servicetest
Topshelf v4.2.1.215,.NET Framework v4.0.30319.42000
servicetest 服务正在运行,按 Control+C 退出。
在 08/10/2019 16:31:12 开始调度程序查询检查

在 08/10/2019 16:31:17 开始调度程序查询检查

在 08/10/2019 16:31:22 开始调度器查询检查

在 08/10/2019 16:31:27 开始调度器查询检查

那么告诉我,该代码有什么问题,它不能作为服务运行......?? 提前感谢您的帮助!

【问题讨论】:

  • 我刚刚注意到我手动粘贴了一个带有 start 的版本,它应该是 x.StartAutomatically();在我的程序中也是如此......现在也固定在原始版本中。

标签: c# windows service topshelf


【解决方案1】:

我发现了问题... 有2个问题。 首先,当这是服务时,它根本不会打开控制台应用程序,如果您想查看任何内容,您必须将其写入 txt 文件...

第二个(在我的完整项目中)是我正在使用我自己的记录器编写所有内容,该记录器正在使用 AppDomain.CurrentDomain.BaseDirectory 编写文件......当这是服务并且你必须编写时,这似乎是不允许的在其他地方登录。

实际上,当我解决了所有这些问题时,我也发现服务也无法打开某些服务器上的文件......所以如果有任何路径,如 \server\folder\subfolder\file. txt 服务不会打开该文件...至少在我的情况下。

所以我想这个案子已经解决了,但也许有这个问题的人会看到这个答案,它会有所帮助....

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-06-28
    • 1970-01-01
    • 1970-01-01
    • 2014-06-28
    相关资源
    最近更新 更多