簡體   English   中英

Win 服務作為控制台應用程序工作,但不能作為 Windows 服務

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

我使用 Topshelf 4.2.1 創建了一個控制台應用程序服務。 像 Windows 服務一樣使用它。 我正在使用 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();
    }

已解決:在 Microsoft Server SQL 中,將 NT AUTHORITY\\SYSTEM 的登錄屬性設置為 sysadmin。 點擊截圖

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM