简体   繁体   中英

Win Service works as Console Application but not as Windows Service

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

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

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