Win Service works as Console Application but not as Windows Service

297 views Asked by At

I created a Console Application service using Topshelf 4.2.1. to use it like a windows service. I am using Dapper to get data and update Microsoft SQL Server, I get the connections strings from app.config as well as constants in (folder path, folder limit) The Console Application works perfect but when I install as a service, it starts but not doing anything.

enter code here class Program
{
    static void Main(string[] args)
    {
        var exitCode = HostFactory.Run(x =>
        {
            x.Service<EmailMonitoring>(s =>
            {
                s.ConstructUsing(emailMonitoring => new EmailMonitoring());
                s.WhenStarted(emailMonitoring => emailMonitoring.Start());
                s.WhenStopped(emailMonitoring => emailMonitoring.Stop());
            });
            x.RunAsLocalSystem();
      

            x.SetServiceName("EmailMonitoring");
            x.SetDisplayName("Email Monitoring");
            x.SetDescription("Description");

        });

        int exitCodeValue = (int)Convert.ChangeType(exitCode, exitCode.GetTypeCode());
        Environment.ExitCode = exitCodeValue;
    }
}


enter code here private readonly Timer _timer;

    public EmailMonitoring()
    {
        _timer = new Timer(1000 * int.Parse(Helper.AppSetVal("intervalSeconds"))) { AutoReset = false };
        // when testing, use smaller interval and autoreset = false like example below
       // _timer = new Timer( 1000  ) { AutoReset = false };
        _timer.Elapsed += TimerElapsed;
    } 

    private void TimerElapsed(object sender, ElapsedEventArgs e)
    {
        ServiceHelper.ProcessNewRecords();
    }

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

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

There are 1 answers

0
Daniel Gheorghe On

SOLVED: In Microsoft Server SQL, set the login properties for NT AUTHORITY\SYSTEM to sysadmin. CLICK for screenshot