欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

使用Topshelf框架操作Windows服務(wù)

 更新時(shí)間:2022年06月13日 08:52:44   作者:springsnow  
這篇文章介紹了使用Topshelf框架操作Windows服務(wù)的方法,文中通過(guò)示例代碼介紹的非常詳細(xì)。對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下

一、介紹

Topshelf是一個(gè)開(kāi)源的跨平臺(tái)的宿主服務(wù)框架,支持Windows和Mono,只需要幾行代碼就可以構(gòu)建一個(gè)很方便使用的服務(wù)宿主。

Topshelf是創(chuàng)建Windows服務(wù)的另一種方。它極大的簡(jiǎn)化服務(wù)創(chuàng)建與部署過(guò)程,它支持將控制臺(tái)應(yīng)用程序部署為服務(wù)。

下載

1、官網(wǎng):http://topshelf-project.com/ 這里面有詳細(xì)的文檔及下載

2、Topshelf的代碼托管在 http://github.com/topshelf/Topshelf/

二、使用

1、Topshelf 安裝

通過(guò) NuGet 安裝 Topshelf 包。

Install-Package Topshelf

2、Topshelf 配置

以下是我們以 Topshelf 來(lái)部署的一個(gè) gRPC 服務(wù)代碼,Topshelf 關(guān)鍵配置在 Main 方法內(nèi),更多的配置建議閱讀一下 官方文檔

class Program
{
  static void Main(string[] args)
  {
    // 配置和運(yùn)行宿主服務(wù)
    HostFactory.Run(x => 
    {
      // 指定服務(wù)類型。這里設(shè)置為 CacheService
      x.Service<CacheService>(s =>
      {
                s.ConstructUsing(name => new CacheService());// 通過(guò) new CacheService() 構(gòu)建一個(gè)服務(wù)實(shí)例 
                s.WhenStarted(tc => tc.Start());// 當(dāng)服務(wù)啟動(dòng)后執(zhí)行什么
                s.WhenStopped(tc => tc.Stop());// 當(dāng)服務(wù)停止后執(zhí)行什么
      });
            
      x.RunAsLocalSystem();// 服務(wù)用本地系統(tǒng)賬號(hào)來(lái)運(yùn)行,身份標(biāo)識(shí),有好幾種方式,如:x.RunAs("username", "password");  x.RunAsPrompt(); x.RunAsNetworkService(); 等
      
      x.SetDescription("緩存服務(wù)");// 服務(wù)描述信息      
      x.SetDisplayName("CacheService");// 服務(wù)顯示名稱     
      x.SetServiceName("CacheService"); // 服務(wù)名稱
    });
  }
}

public class CacheService
{
  private readonly string host = ConfigurationManager.AppSettings["Host"];
  private readonly string port = ConfigurationManager.AppSettings["Port"];

  readonly Server server;
  public CacheService()
  {
    server = new Server
    {
      Services = { MDCache.BindService(new CacheServiceImpl()) },
      Ports = { new ServerPort(host, Convert.ToInt32(port), ServerCredentials.Insecure) }
    };
  }
  public void Start() { server.Start(); }
  public void Stop() { server.ShutdownAsync(); }
}

3、安裝服務(wù)

通過(guò)以上配置,確保程序集 Build 成功后,進(jìn)入 bin\Debug 目錄下,執(zhí)行 install 命令,一個(gè) Windows 服務(wù)就誕生了。(如果出現(xiàn)需要以管理員身份啟動(dòng)的提示,重新以管理員身份啟動(dòng) cmd )。

xxx.exe install

4、啟動(dòng)服務(wù)

啟動(dòng):

xxx.exe start

也可以安裝成功后我們可以在 Windows 服務(wù)下找到并啟動(dòng)它。

image

注意:因?yàn)?serviceName 必須是唯一的,如果我們希望在同一臺(tái)機(jī)器上運(yùn)行多個(gè)相同的服務(wù),那么我們需要注釋掉硬編碼設(shè)置的 ServiceName 和 DisplayName ,然后通過(guò)命令參數(shù)來(lái)動(dòng)態(tài)指定服務(wù)名稱。

// 服務(wù)顯示名稱
//x.SetDisplayName("CacheService");
// 服務(wù)名稱
//x.SetServiceName("CacheService");
xxx.exe install -servicename cacheService
xxx.exe install -servicename cacheService1

5、服務(wù)卸載

卸載和啟動(dòng)的命令保持一致,只需要把 install 改成 uninstall 。

指定服務(wù)名稱卸載

三、Service Configuration 服務(wù)配置

以上為自定義模式,還有一種叫簡(jiǎn)單模式。繼承ServiceControl接口,實(shí)現(xiàn)該接口即可。

class Program
{
    public static void Main(string[] args)
    {
        var logCfg = new FileInfo(AppDomain.CurrentDomain.BaseDirectory + "log4net.config");
        XmlConfigurator.ConfigureAndWatch(logCfg);

        HostFactory.Run(x =>
        {
            x.Service<TownCrier>();
            x.RunAsLocalSystem();

            x.SetDescription("Sample Topshelf Host服務(wù)的描述");
            x.SetDisplayName("Stuff顯示名稱");
            x.SetServiceName("Stuff服務(wù)名稱");
        });
    }
}

public class TownCrier : ServiceControl
{
    private Timer _timer = null;
    readonly ILog _log = LogManager.GetLogger(typeof(TownCrier));
    public TownCrier()
    {
        _timer = new Timer(1000) { AutoReset = true };
        _timer.Elapsed += (sender, eventArgs) => _log.Info(DateTime.Now);

    }
    public bool Start(HostControl hostControl)
    {
        _log.Info("TopshelfDemo is Started");
        _timer.Start();
        return true;
    }

    public bool Stop(HostControl hostControl)
    {
        throw new NotImplementedException();
    }
}

到此這篇關(guān)于使用Topshelf框架操作Windows服務(wù)的文章就介紹到這了。希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

最新評(píng)論