简体   繁体   English

Win 服务作为控制台应用程序工作,但不能作为 Windows 服务

[英]Win Service works as Console Application but not as Windows Service

I created a Console Application service using Topshelf 4.2.1.我使用 Topshelf 4.2.1 创建了一个控制台应用程序服务。 to use it like a windows service.像 Windows 服务一样使用它。 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.我正在使用 Dapper 获取数据并更新 Microsoft SQL Server,我从 app.config 中获取连接字符串以及(文件夹路径、文件夹限制)中的常量控制台应用程序运行良好,但是当我作为服务安装时,它启动但是什么都不做。

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.已解决:在 Microsoft Server SQL 中,将 NT AUTHORITY\\SYSTEM 的登录属性设置为 sysadmin。 CLICK for screenshot点击截图

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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