简体   繁体   中英

TopShelf - service starts but nothings works

I have written a simple code that should start automatically using topshelf. The problem is that it starts without any problems if I run it from Visual Studio or when I just click in debug folder on exe file, but.... when I install it as a service it does nothing...

So here is my code

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();
            }
    }

}

I did copy the sample from topshelf documentation and there are only some small changes to that code. I have pasted here only the light version of this (just to show you the problem). In the full version I used as well fluentscheduler and there is a lot of code that connects to postgress, get some data and do some stored procedures, but this is not the problem. The problem is that even this small code that you can see above doesn't work at all. You can create this program now if you start new console application and just paste it there (and of course add topshelf from nuget). If you start it, it should work, but if you install this as service this is not working at all...

Here are as well steps that I did when installing as service:
1) open CMD as administrator
2) go to the directory with debug
3) install it that way: ConsoleCRP.exe install
4) start the service: net start servicetest
5) if you want to stop the service: net stop servicetest
6) if you want to unistall: ConsoleCRP.exe uninstall

Here is the result:
Configuration Result:
[Success] Name servicetest
[Success] DisplayName scheduler
[Success] Description This is automatic scheduler
[Success] ServiceName servicetest
Topshelf v4.2.1.215, .NET Framework v4.0.30319.42000
The servicetest service is now running, press Control+C to exit.
Starting scheduler query check at 08/10/2019 16:31:12

Starting scheduler query check at 08/10/2019 16:31:17

Starting scheduler query check at 08/10/2019 16:31:22

Starting scheduler query check at 08/10/2019 16:31:27

So tell me, what is wrong in that code that it just doesn't work as a service...?? thank you in advanced for your help!

I found the problem... There were 2 problems. First is that when this is service it will not open console application at all and if you want to see anything you must write it ie to txt file...

second (on my full project) was that I was writing everything using my own logger, which was writing file using AppDomain.CurrentDomain.BaseDirectory... it seems like this is not allowed when this is serive and you have to write the log in some other place.

and actually when I solved all of this I found as well that this is also not possible for service to open files that are on some servers... so if there are any paths like \server\folder\subfolder\file.txt service will not open that file... at least in my case.

So I guess that case is solved, but maybe someone who had this problem will see this answer and it will help....

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM