使用Topshelf建立Windows 服務

2022-09-03 09:51:09 字數 4702 閱讀 2562

winndows service 是一種可隨 windows 作業系統啟動而啟動的,在後台執行的,通常不和使用者產生互動的程式。它無法通過雙擊來執行,類似於 unix 守護程序(daemon processes),當使用者登出時它也不會停止。

windows 服務由三部分組成:

開發乙個windows服務通常也比較簡單,在開發的時候我們期望以命令列方式執行,想對windows服務有更多的控制,就有乙個windows服務框架topshelf 可以滿足,使用這個框架要求你使用乙個ioc容器,在框架中使用的是common service locator 介面,可以根據你的喜好去選擇你自己中意的ioc容器。

topshelf的基本介紹可以參看dru sellers 的介紹性文章 topshelf。下面的**就是建立了乙個windows服務:

using system; 

using system.collections.generic; 

using system.io; 

using system.timers; 

using log4net.config; 

using microsoft.practices.servicelocation; 

using structuremap; 

using topshelf; 

using topshelf.configuration;

internal class program 

);                 x.setdescription("sample topshelf host"); 

x.setdisplayname("stuff"); 

x.setservicename("stuff"); 

x.configureserviceinisolation("tc", s => 

);return new structuremapservicelocator(); 

}); 

s.whenstarted(tc => tc.start()); 

s.whenstopped(tc => tc.stop()); 

}); 

x.runaslocalsystem();

runner.host(cfg, args); } }

這裡我們使用了structuremap 作為ioc容器,建立了乙個structuremapservicelocator來掩藏structuremap,建立的windows服務的名稱是stuff,可以吊相應的方法啟動,停止服務。通過直接執行.exe檔案在控制台中執行或者除錯服務了。

通過命令執行,安裝解除安裝windows服務

stuff.exe                #控制台方式執行

stuff.exe /install     #安裝windows服務

stuff.exe /uninstall  #解除安裝windows服務

預設情況下,windows服務只能執行乙個例項,如果我們想執行多個例項怎麼辦,可以在topshelf的命令列引數中增加–instance來指定例項的名稱,也可以通過執行時讀取配置檔案來達到目的,我更喜歡使用後一種方式設定,在應用程式的配置檔案上增加個配置windowsserviceinstancename:

<?xmlversion="1.0"encoding="utf-8"?>

<configuration>

<file=>

<addkey="windowsserviceinstancename"value="stuff"/>

然後改造一下上述**

static void main(string args) 

);x.setdescription("sample topshelf host"); 

x.setdisplayname(instancename ); 

x.setservicename(instancename ); 

……這樣我們就可以達到在同台機器上安裝多個windows 服務例項,推薦大家使用這個windows服務框架topshelf ,可以簡化很多任務作和增加靈活性

windows 服務由三部分組成:

開發乙個windows服務通常也比較簡單,在開發的時候我們期望以命令列方式執行,想對windows服務有更多的控制,就有乙個windows服務框架topshelf 可以滿足,使用這個框架要求你使用乙個ioc容器,在框架中使用的是common service locator 介面,可以根據你的喜好去選擇你自己中意的ioc容器。

topshelf的基本介紹可以參看dru sellers 的介紹性文章 topshelf。下面的**就是建立了乙個windows服務:

using system; 

using system.collections.generic; 

using system.io; 

using system.timers; 

using log4net.config; 

using microsoft.practices.servicelocation; 

using structuremap; 

using topshelf; 

using topshelf.configuration;

internal class program 

);                 x.setdescription("sample topshelf host"); 

x.setdisplayname("stuff"); 

x.setservicename("stuff"); 

x.configureserviceinisolation("tc", s => 

);return new structuremapservicelocator(); 

}); 

s.whenstarted(tc => tc.start()); 

s.whenstopped(tc => tc.stop()); 

}); 

x.runaslocalsystem();

runner.host(cfg, args); } }

這裡我們使用了structuremap 作為ioc容器,建立了乙個structuremapservicelocator來掩藏structuremap,建立的windows服務的名稱是stuff,可以吊相應的方法啟動,停止服務。通過直接執行.exe檔案在控制台中執行或者除錯服務了。

通過命令執行,安裝解除安裝windows服務

stuff.exe                #控制台方式執行

stuff.exe /install     #安裝windows服務

stuff.exe /uninstall  #解除安裝windows服務

預設情況下,windows服務只能執行乙個例項,如果我們想執行多個例項怎麼辦,可以在topshelf的命令列引數中增加–instance來指定例項的名稱,也可以通過執行時讀取配置檔案來達到目的,我更喜歡使用後一種方式設定,在應用程式的配置檔案上增加個配置windowsserviceinstancename:

<?xmlversion="1.0"encoding="utf-8"?>

<configuration>

<file=>

<addkey="windowsserviceinstancename"value="stuff"/>

然後改造一下上述**

static void main(string args) 

);x.setdescription("sample topshelf host"); 

x.setdisplayname(instancename ); 

x.setservicename(instancename ); 

……這樣我們就可以達到在同台機器上安裝多個windows 服務例項,推薦大家使用這個windows服務框架topshelf ,可以簡化很多任務作和增加靈活性

使用Topshelf建立Windows服務

一 概述 topshelf是建立windows服務的一種方法,topshelf是乙個開源的跨平台的宿主服務框架,支援windows和mono,只需要幾行 就可以構建乙個很方便使用的服務宿主。二 安裝方法 3 在visual studio中新建乙個專案,只需要引用topshelf.dll 即可,為了日...

使用Topshelf建立Windows 服務

使用topshelf建立windows 服務 2 使用visual studio建立乙個控制台應用程式引用程式集topshelf.dll 合log4net.dll 用nuget 查詢topshelf 安裝 topshelf和 topshelf.log4net 即可 3 建立乙個簡單的服務類,裡面包含...

使用Topshelf建立Windows 服務

winndows service 是一種可隨 windows 作業系統啟動而啟動的,在後台執行的,通常不和使用者產生互動的程式。它無法通過雙擊來執行,類似於 unix 守護程序 daemon processes 當使用者登出時它也不會停止。windows 服務由三部分組成 開發乙個windows服務...