只有累積,沒有奇蹟

2024年11月4日 星期一

[Windows] 註冊 Windows Service 服務

前言
最近專案有個需求要將排程透過 Windows Service 服務來執行,在 Windows OS 要註冊 Service 可以用  cmd  與  powershell  兩種方式來建立以及刪除 Service,兩種方式之前都有使用過但要再使用時都會上網查因此決定紀錄一下未來方便查詢,此篇就針對這兩種方式進行基本介紹與說明,若有問題歡迎提出一起討論或是給予指導。

使用命令提示字元 cmd
在執行時請先注意開啟 cmd 需要使用 Admin 權限執行,否則會有執行異常或是告知沒權限的錯誤訊息,以下為開啟時應用程式時用 administrator 開啟的畫面,用管理者身分執行 cmd

註冊服務
在 cmd 中可以透過 sc.exe 來建立 windows service,sc 全名為 service control,語法指令格式如下說明
描述:
        在登錄和服務資料庫中建立服務項目。
使用方法:
        sc <server> create [service name] [binPath= ] <option1> <option2>...

選項:
注意: 選項名稱包括等號。
      在等號和值之間必須空一格。
 type= <own|share|interact|kernel|filesys|rec|userown|usershare>
       (預設值 = own)
 start= <boot|system|auto|demand|disabled|delayed-auto>
       (預設值 = demand)
 error= <normal|severe|critical|ignore>
       (預設值 = normal)
 binPath= <.exe 檔案的二進位檔案路徑名稱>
 group= <載入順序群組>
 tag= <yes|no>
 depend= <相依性(以 / (反斜線) 隔開)>
 obj= <帳戶名稱|物件名稱>
       (預設值 = LocalSystem)
 DisplayName= <顯示名稱>
 password= <密碼>
舉例來說,要註冊 Windows Service 名為 TestService,其檔案位置在 D:\Job\TestService\Test.Jobs.exe 位置中,可以透過  create ServiceName  指令建立
D:\>sc create TestService binPath="D:\Job\TestService\Test.Jobs.exe"
[SC] CreateService 成功
建立成功可以看到新增 TestService 成功的訊息,接著我們使用   query ServiceName  查詢目前 service 狀態,剛建立好的 service 狀態會是 STOPPED (1)
D:\>sc query testservice

SERVICE_NAME: testservice
        TYPE               : 10  WIN32_OWN_PROCESS
        STATE              : 1  STOPPED
        WIN32_EXIT_CODE    : 1077  (0x435)
        SERVICE_EXIT_CODE  : 0  (0x0)
        CHECKPOINT         : 0x0
        WAIT_HINT          : 0x0

啟動服務
可以透過  start ServiceName  指令來啟動服務,要啟動時狀態為 START_PENDING (2)
D:\>sc start testservice

SERVICE_NAME: testservice
        TYPE               : 10  WIN32_OWN_PROCESS
        STATE              : 2  START_PENDING
                                (NOT_STOPPABLE, NOT_PAUSABLE, IGNORES_SHUTDOWN)
        WIN32_EXIT_CODE    : 0  (0x0)
        SERVICE_EXIT_CODE  : 0  (0x0)
        CHECKPOINT         : 0x0
        WAIT_HINT          : 0x7d0
        PID                : 26300
        FLAGS              : 
當正常執行,狀態就會更新為 RUN (4) 
SERVICE_NAME: testservice
        TYPE               : 10  WIN32_OWN_PROCESS
        STATE              : 4  RUNNING
                                (STOPPABLE, NOT_PAUSABLE, ACCEPTS_SHUTDOWN)
        WIN32_EXIT_CODE    : 0  (0x0)
        SERVICE_EXIT_CODE  : 0  (0x0)
        CHECKPOINT         : 0x0
        WAIT_HINT          : 0x0

停止服務
要停止服務的可以透過  stop ServiceName 指令,執行完指令會進行停止的動作,因此查看當下狀態會是 STOP_PENDING,當停止後再次查詢可以看到狀態改為 STOPPED (1)
D:\>sc stop testservice

SERVICE_NAME: testservice
        TYPE               : 10  WIN32_OWN_PROCESS
        STATE              : 3  STOP_PENDING
                                (STOPPABLE, NOT_PAUSABLE, ACCEPTS_SHUTDOWN)
        WIN32_EXIT_CODE    : 0  (0x0)
        SERVICE_EXIT_CODE  : 0  (0x0)
        CHECKPOINT         : 0x0
        WAIT_HINT          : 0x0

D:\>sc query testservice

SERVICE_NAME: testservice
        TYPE               : 10  WIN32_OWN_PROCESS
        STATE              : 1  STOPPED
        WIN32_EXIT_CODE    : 0  (0x0)
        SERVICE_EXIT_CODE  : 0  (0x0)
        CHECKPOINT         : 0x0
        WAIT_HINT          : 0x0

刪除服務
當要刪除服務的話執行  delete ServiceName  指令,其回傳訊息與 create 類似直接回傳刪除服務成功
D:\>sc delete testservice
[SC] DeleteService 成功

使用 Powershell 
同樣的在執行前需要使用管理者身分執行 powershell,否則會有執行異常或是告知沒權限的錯誤訊息,
以下為開啟時應用程式時用 administrator 開啟的畫面

註冊服務
在 cmd 中可以透過 sc.exe 來建立 windows service,sc 全名為 service control,語法指令格式如下說明
PS C:\> New-Service -Name "TestService" -BinaryPathName "D:\Jobs\TestService\Jobs.exe" -DisplayName "Test Service" -StartupType Manual -Description "This is a test service."

Status   Name               DisplayName
------   ----               -----------
Stopped  TestService        Test Service
參數介紹


  • Name : 指定服務的名稱 (唯一)
  • BinaryPathName : 服務的執行檔案路徑位置
  • DisplayName : 顯示的名稱
  • StartupType : 啟動類型
  • Description : 描述
  • 建立成功之後,在 GUI 呈現畫面如下
    要註冊 Windows Service 名為 TestService,其檔案位置在 D:\Job\TestService\Test.Jobs.exe 位置中,可以透過  create ServiceName  指令建立
    D:\>sc create TestService binPath="D:\Job\TestService\Test.Jobs.exe"
    [SC] CreateService 成功
    建立成功,接著我們查詢目前 service 狀態,在 powershell 指令比 cmd 稍微多一滴滴要使用 win32service在透過 filter 來查詢剛建立好的 testservice,與 cmd 建立的相同剛建好的狀態都是 Stopped 
    PS C:\> Get-WmiObject win32_service -Filter "name='testservice'"
    
    ExitCode  : 1077
    Name      : TestService
    ProcessId : 0
    StartMode : Manual
    State     : Stopped
    Status    : OK

    啟動服務
    可以透過  start ServiceName -Name  指令來啟動服務,與 cmd 不同的是會等到 running 在顯示目前狀態
    PS C:\> Start-Service -Name "testservice"
    PS C:\> Get-WmiObject win32_service -Filter "name='testservice'"
    
    ExitCode  : 0
    Name      : TestService
    ProcessId : 18188
    StartMode : Manual
    State     : Running
    Status    : OK              : 

    停止服務
    使用  stop ServiceName  指令停止當下 windows service,執行完指令會進行停止的動作,再次查詢服務狀態就會是已停止 STOPPED
    PS C:\> Stop-Service -Name "testservice"
    PS C:\> Get-WmiObject win32_service -Filter "name='testservice'"
    
    ExitCode  : 0
    Name      : TestService
    ProcessId : 0
    StartMode : Manual
    State     : Stopped
    Status    : OK

    刪除服務
    在 powersehll 中刪除時要特別注意,官方 MSDN 中提供的 Remove service 是 powershell 6 才提供的新功能,如果您的 powershell 版本低於 6 時候執行 remove serv 則會跳出錯誤訊息 : 無法辨識 'Remove-Service' 詞彙是否為 Cmdlet、函數,如果不確定電腦中使用的 powershell 版本為何,可以輸入以下指令,
    PS C:\> $PSVersionTable.PSVersion
    
    Major  Minor  Build  Revision
    -----  -----  -----  --------
    5      1      17763  316
    因此,Remove service 會根據版本不同執行不同的指令,如果 powershell 是 6.0 或以上請輸入指令
    Remove-Service someservice 
    powershell 是 6.0 以下
    Stop-Service 'testservice'; Get-CimInstance -ClassName Win32_Service -Filter "Name='testservice'" | Remove-CimInstance
    

    後記
    透過以上簡單的介紹說明了在 cmd 與 powershell 中如何新增、刪除、查詢及修改 windows service 的各種方式與指令,如果有需要更細節的內容說是說明,可以透過 MSDN 官方網站查詢相關指令與更多的應用細節,這裡就針對簡單操作做說明不在琢磨,希望自己的金魚腦寫完這篇之後可以記錄得更清楚些 ! 

    參考
    powershell create windows service

    2024年10月26日 星期六

    [NET] 使用 MethodBase.GetCurrentMethod 取得執行方法資訊

    前言 
    在過去 method 發生例外用 catch 包起來時,往往 catch 寫的 logName 都是 hard Code 寫死 method Name,但常常會發生 copy 來 copy 去的時候忘記改寫死的 method name 造成 log 寫錯誤狀況發生,之前使用 C# 4.5 提供的 CallerMemberName attribute 解決此問題,簡單範例如下
    private void Test(string message, [CallerMemberName] string memberName = "")
    {
        try
        {
            // do something
        }
        catch (Exception e)
        {
            Console.WriteLine($"Error Method : {memberName} , Message : {message}");
        }
    }
    但在 Method 都必須加上參數 CallerMember,此篇文章介紹另一種方式解決此問題

    使用方式
    在命名空間 System.Reflection 可以用 MethodBase 靜態方法取得當前方法的相關資訊,使用方式如下所示
    MethodBase.GetCurrentMethod()
    在 GetCurrencyMethod 方法還可以取得方法名稱、命名空間、全名等資訊
    MethodBase.GetCurrentMethod().Name ;
    MethodBase.GetCurrentMethod().DeclaringType.Namespace;
    MethodBase.GetCurrentMethod().DeclaringType.FullName;
    
    再回到今天想解決的問題,在發生例外使用用 Methodbase 取代 CallerMember 取得當下執行的方法,即可不用在 hardCode 在例外的訊息中,不用擔心 copy 到其他地方要再修改的問題,使用前須先using system.reflection 命名空間,範例 Code 如下
    using System.Reflection;
    
    static void Main(string[] args)
    {
        // 目前執行方法的類別名稱
        Console.WriteLine($"GetCurrentMethod.Name : {MethodBase.GetCurrentMethod().Name}");
        // 目前執行方法的命名空間
        Console.WriteLine($"GetCurrentMethod.DeclaringType.Namespace : {MethodBase.GetCurrentMethod().DeclaringType.Namespace}");
        // 目前執行方法的全名
        Console.WriteLine($"GetCurrentMethod.DeclaringType.FullName : {MethodBase.GetCurrentMethod().DeclaringType.FullName}");
    
        try
        {
            CallForException();
        }
        catch (Exception e)
        {
            Console.WriteLine("Exception Method Name : {0} ", GetException(MethodBase.GetCurrentMethod(), e));
        }
    
        Console.ReadKey();
    }
    
    private static string GetException(MethodBase methodBase, Exception e)
    {
        StackTrace trace = new StackTrace(e);
        StackFrame previousFrame = null;
    
        foreach (StackFrame frame in trace.GetFrames())
        {
            if (frame.GetMethod() == methodBase)
            {
                break;
            }
    
            previousFrame = frame;
        }
    
        return previousFrame?.GetMethod().Name;
    }
    
    private static void CallForException()
    {
        DoActualException();
    }
    
    private static void DoActualException()
    {
        throw new NotImplementedException();
    }
    輸出
    上面 sample Code 輸出如下
    • 執行的 method Name 為 main
    • 執行的 method Name 命名空間為 consoleApp1 
    • 發生例外 try catch 裡的方法名稱為 CallForException
    輕鬆上手,打完收工 !!

    參考
    MethodBase.GetCurrentMethod Method

    2024年9月14日 星期六

    [conference] DDDesign TW 2024 - Panel Discussion : 遺留工作負載(legacy workloads)

    分享心得
    從開發運維的角度來分享關於遺留工作負載(legacy workloads)的經驗與心得

    議程介紹
    主題 : 從開發運維的角度來分享關於遺留工作負載(legacy workloads)的經驗與心得
    今年主題爲「系統設計與社會技術年會(System Design & Socio-technical Conference)」。
    
    圍繞「遺留工作負載(legacy workloads)」和現代應用程式的演變,邀請大家共同探討這些工作負載在當今商業環境中所帶來的挑戰和機遇。討論遺留工作負載的演進,以及如何應對它們的變化,並探索系統設計的複雜性,重點考慮社會技術因素對軟體開發決策和執行的影響。期待深入探討企業決策者和一線執行團隊之間的合作方式,以實現系統設計和開發的目標。因此,我們將邀請國內外的領域驅動設計實踐家來分享經驗,希望觀衆不論程度都能從演講、互動環節、工作坊中獲得啟發並產生改變。
    

    主辦單位 :DDD TW
    議程表 : 連結



    Copyright © m@rcus 學習筆記 | Powered by Blogger

    Design by Anders Noren | Blogger Theme by NewBloggerThemes.com