簡體   English   中英

TopShelf - 服務啟動但沒有任何效果

[英]TopShelf - service starts but nothings works

我編寫了一個簡單的代碼,應該使用 topshelf 自動啟動。 問題是,如果我從 Visual Studio 運行它,或者當我只是單擊 exe 文件上的調試文件夾時,它啟動時沒有任何問題,但是......當我將它作為服務安裝時,它什么也不做......

所以這是我的代碼

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

}

我確實從 topshelf 文檔中復制了示例,並且該代碼只有一些小的更改。 我在這里只粘貼了這個的簡單版本(只是為了向你展示問題)。 在完整版中,我也使用了 fluentscheduler,並且有很多代碼連接到 postgress,獲取一些數據並執行一些存儲過程,但這不是問題。 問題是,即使您在上面看到的這個小代碼也根本不起作用。 如果您啟動新的控制台應用程序並將其粘貼到那里,您現在可以創建此程序(當然還可以從 nuget 添加 topshelf)。 如果你啟動它,它應該可以工作,但如果你將它作為服務安裝,這根本不起作用......

以下是我在作為服務安裝時執行的步驟:
1)以管理員身份打開CMD
2)go到有debug的目錄
3)以這種方式安裝: ConsoleCRP.exe install
4)啟動服務:net start servicetest
5)如果要停止服務:net stop servicetest
6)如果要卸載:ConsoleCRP.exe卸載

結果如下:
配置結果:
[成功] 名稱服務測試
[成功] DisplayName 調度程序
[成功] 描述 這是自動調度器
[成功] ServiceName servicetest
Topshelf v4.2.1.215,.NET 框架 v4.0.30319.42000
servicetest 服務現在正在運行,按 Control+C 退出。
在 08/10/2019 16:31:12 開始調度程序查詢檢查

在 08/10/2019 16:31:17 開始調度程序查詢檢查

在 08/10/2019 16:31:22 開始調度程序查詢檢查

在 08/10/2019 16:31:27 開始調度程序查詢檢查

所以告訴我,那個代碼有什么問題,它不能作為服務工作......?? 提前感謝您的幫助!

我發現了問題……有兩個問題。 首先是,當這是服務時,它根本不會打開控制台應用程序,如果您想查看任何內容,您必須將其寫入 txt 文件...

第二個(在我的完整項目中)是我正在使用我自己的記錄器編寫所有內容,該記錄器正在使用 AppDomain.CurrentDomain.BaseDirectory 編寫文件......當這是服務並且您必須寫入登錄時,這似乎是不允許的別的地方。

實際上,當我解決所有這些問題時,我還發現服務也無法打開某些服務器上的文件……因此,如果有任何路徑,例如 \server\folder\subfolder\file.txt 服務將不打開那個文件......至少在我的情況下。

所以我想這個案子已經解決了,但也許有這個問題的人會看到這個答案,它會有所幫助....

暫無
暫無

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

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