Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/313.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# TopShelf-服务开始,但一切都不起作用_C#_Windows_Service_Topshelf - Fatal编程技术网

C# TopShelf-服务开始,但一切都不起作用

C# TopShelf-服务开始,但一切都不起作用,c#,windows,service,topshelf,C#,Windows,Service,Topshelf,我已经编写了一个简单的代码,应该可以使用topshelf自动启动。问题是,如果我从Visual Studio运行它,或者在exe文件的调试文件夹中单击,它启动时没有任何问题,但是。。。。当我把它作为服务安装时,它什么也不做 这是我的代码 using System; using System.Collections.Generic; using Topshelf; using System.IO; namespace ConsoleCRP { public class Progr

我已经编写了一个简单的代码,应该可以使用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,那么现在就可以创建这个程序。如果你启动它,它应该会工作,但如果你安装这个服务,这是不工作的所有

以下是我在安装as service时执行的步骤: 1以管理员身份打开CMD 2使用debug转到目录 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退出。 在2019年10月8日16:31:12开始计划程序查询检查

在2019年10月8日16:31:17开始计划程序查询检查

在2019年10月8日16:31:22开始计划程序查询检查

在2019年10月8日16:31:27开始计划程序查询检查

那么告诉我,代码中有什么错误,它不能作为服务工作。。。?? 提前感谢您的帮助

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

我整个项目的第二点是,我使用自己的记录器编写所有内容,该记录器使用AppDomain.CurrentDomain.BaseDirectory编写文件。。。这似乎是不允许的,当这是serive,你必须写在其他地方的日志

事实上,当我解决所有这些问题时,我发现服务也不可能打开某些服务器上的文件。。。因此,如果存在任何路径,如\server\folder\subfolder\file.txt,服务将不会打开该文件。。。至少对我来说是这样


所以我想这个问题已经解决了,但可能有人遇到了这个问题,他会看到这个答案,这会有帮助……

我刚刚注意到我用start手动粘贴了一个版本,它应该是x.start自动;我的节目也是这样的。。。现在在oryginal也修复了。