C#使用Topshelf創建Windows服務


    一、項目創建

    創建一個控制台應用程序,項目右鍵->管理 NuGet 程序包->Topshelft及Topshelf.Log4Net。

    二、Topshelf配置

    一般來說,服務都會設置每隔多長時間執行一次任務,這里使用System.Threading.Timer來做個簡單的日志記錄,將日志寫入到Debug\Log文件夾下。

    2.1、Log4Net配置

    新建一個log4net.config的配置文件,在其屬性的復制到輸出目錄項下選擇始終復制。

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <configSections>
    <section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net" />
  </configSections>
  <log4net>
    <!-- Console部分log輸出格式的設定 -->
    <appender name="ConsoleAppender" type="log4net.Appender.ConsoleAppender">
      <layout type="log4net.Layout.PatternLayout">
        <conversionPattern value="%date [%thread] %-5level %logger - %message %newline" />
      </layout>
    </appender>
    <appender name="RollingLogFileAppender" type="log4net.Appender.RollingFileAppender">
      <file value="Log\"/>
      <appendToFile value="true"/>
      <maxSizeRollBackups value="10"/>
      <maximumFileSize value="1MB"/>
      <rollingStyle value="Date"/>
      <datePattern value='yyyy-MM-dd".log"' />
      <staticLogFileName value="false"/>
      <!--最小鎖定模型以允許多個進程可以寫入同一個文件-->
      <param name="lockingModel"  type="log4net.Appender.FileAppender+MinimalLock" />
      <layout type="log4net.Layout.PatternLayout">
        <conversionPattern value="%date %-5level %logger - %message %newline"/>
      </layout>
    </appender>
    <root>
      <level value="ALL" />
      <appender-ref ref="ConsoleAppender" />
      <appender-ref ref="RollingLogFileAppender" />
    </root>
  </log4net>
</configuration>

    2.2、TopshelfService

    新建一個TopshelfService類:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using Topshelf;
using Topshelf.Logging;

namespace LinkTo.Test.TopshelfService
{
    public class TopshelfService : ServiceControl
    {
        private static readonly LogWriter logger = HostLogger.Get<TopshelfService>();
        private static Timer timerAsync = null;
        private readonly int dueTimeInterval = 1000 * 5; //單位:毫秒
        private readonly int periodInterval = 1000 * 5;  //單位:毫秒

        /// <summary>
        /// 構造函數
        /// </summary>
        public TopshelfService()
        {
            timerAsync = new Timer(AutoAsyncCallback, null, Timeout.Infinite, Timeout.Infinite);
        }

        /// <summary>
        /// 啟動服務
        /// </summary>
        /// <param name="hostControl"></param>
        /// <returns></returns>
        public bool Start(HostControl hostControl)
        {
            try
            {
                logger.Info("HelloTopshelf Start");
                timerAsync.Change(dueTimeInterval, periodInterval);
            }
            catch (Exception ex)
            {
                logger.Info(ex.Message);
            }
            return true;
        }

        /// <summary>
        /// 停止服務
        /// </summary>
        /// <param name="hostControl"></param>
        /// <returns></returns>
        public bool Stop(HostControl hostControl)
        {
            try
            {
                logger.Info("HelloTopshelf Stop");
                if (timerAsync != null)
                {
                    timerAsync.Change(Timeout.Infinite, Timeout.Infinite);
                    timerAsync.Dispose();
                    timerAsync = null;
                }
            }
            catch (Exception ex)
            {
                logger.Info(ex.Message);
            }
            return true;
        }

        /// <summary>
        /// 回調函數
        /// </summary>
        /// <param name="state"></param>
        private void AutoAsyncCallback(object state)
        {
            try
            {
                timerAsync.Change(Timeout.Infinite, Timeout.Infinite);
                logger.Info("AutoAsyncCallback執行開始");
                Thread.Sleep(1000 * 10);
            }
            catch (Exception ex)
            {
                logger.ErrorFormat("AutoAsyncCallback執行異常:{0}", ex.Message);
            }
            finally
            {
                timerAsync.Change(dueTimeInterval, periodInterval);
                logger.Info("AutoAsyncCallback執行結束");
                logger.Info(Environment.NewLine);
            }
        }
    }
}

    2.3、配置和運行宿主服務

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Topshelf;

namespace LinkTo.Test.TopshelfService
{
    class Program
    {
        static void Main(string[] args)
        {
            HostFactory.Run(x =>
            {
                x.UseLog4Net("log4net.config");
                x.RunAsLocalSystem();
                x.Service(settings => new TopshelfService());
                //服務的描述
                x.SetDescription("你好,Topshelf!");
                //服務的顯示名稱
                x.SetDisplayName("Hello Topshelf Service");
                //服務名稱
                x.SetServiceName("HelloTopshelf");
            });
        }
    }
}

    三、安裝與卸載

    3.1、安裝服務

    在Debug文件夾下面,創建一個"安裝服務.bat"的批處理文件:

@echo on

rem 設置DOS窗口的背景顏色及字體顏色
color 2f

rem 設置DOS窗口大小 
mode con: cols=80 lines=25

@echo off
echo 請按任意鍵開始安裝LinkTo.Test.TopshelfService服務

rem 輸出空行
echo.
pause

LinkTo.Test.TopshelfService install
net start HelloTopShelf

pause

    3.2、卸載服務

    在Debug文件夾下面,創建一個"卸載服務.bat"的批處理文件:

@echo on

rem 設置DOS窗口的背景顏色及字體顏色
color 2f

rem 設置DOS窗口大小 
mode con: cols=80 lines=25

@echo off
echo 請按任意鍵開始卸載LinkTo.Test.TopshelfService服務

rem 輸出空行
echo.
pause

net stop HelloTopShelf
LinkTo.Test.TopshelfService uninstall

pause

    3.3、查看服務

    在運行中輸入"services.msc"進入服務,即可看到新建的HelloTopshelf服務:

    四、添加管理員權限要求

    項目右鍵->添加->新建項->應用程序清單文件。

    將requestedExecutionLevel節點的level設置為"requireAdministrator"。

<requestedExecutionLevel level="requireAdministrator" uiAccess="false" />

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM