建立Windows Service的幾種方式

2021-09-02 13:22:11 字數 3668 閱讀 7609

最近由於工作需要,寫了一些windows服務程式,有一些經驗,我現在總結寫出來。

目前我知道的建立建立windows服務有3種方式:

a.利用.net框架類servicebase

b.利用元件topshelf

c.利用小工具instsrv和srvany

下面我利用這3種方式,分別做乙個windows服務程式,程式功能就是每隔5秒往程式目錄下記錄日誌:

a.利用.net框架類servicebase

本方式特點:簡單,相容性好

通過繼承.net框架類servicebase實現

第1步: 新建乙個windows服務

public

partial

class

service1 : servicebase

;_timer.elapsed += delegate ( object

sender , elapsedeventargs e )

", datetime.now ) );};}

protected

override

void onstart ( string

[ ] args )

", datetime.now ) );

}protected

override

void

onstop ( )

" , datetime.now ) +environment.newline );

}void witre ( string

context )

}

第2步: 新增installer

[runinstaller ( true

)]

public

partial

class

installer1 : system.configuration.install.installer

}

第3步:安裝,解除安裝 

cmd命令

installutil      windowsservice_test.exe  (安裝windows服務)

installutil /u   windowsservice_test.exe  (解除安裝windows服務)

b.利用元件topshelf

本方式特點:**簡單,開源元件,windows服務可執行多個例項

topshelf是乙個開源的跨平台的服務框架,支援windows和mono,只需要幾行**就可以構建乙個很方便使用的服務. 官方**:

第1步:引用程式集topshelf.dll和log4net.dll

第2步:建立乙個服務類myclass,裡面包含兩個方法start和stop,還包含乙個定時器timer,每隔5秒往文字檔案中寫入字元

public

class

myclass

;_timer.elapsed += delegate ( object

sender , elapsedeventargs e )

", datetime.now ) );};}

void witre ( string

context )

public

void

start ( )

", datetime.now ) );

}public

void

stop ( )

" , datetime.now ) +environment.newline );}}

第3步:使用topshelf宿主我們的服務,主要是topshelf如何設定我們的服務的配置和啟動和停止的時候的方法呼叫

class

program

);x.runaslocalsystem ( );

//服務的描述

x.setdescription ( "

topshelf_description");

//服務的顯示名稱

x.setdisplayname ( "

topshelf_displayname");

//服務名稱

x.setservicename ( "

topshelf_servicename");

} );}}

第4步: cmd命令

c.利用小工具instsrv和srvany

本方式特點:**超級簡單,windowsform程式即可,並支援程式互動(本人最喜歡的特點),好像不支援win7,支援xp win2003

首先介紹2個小工具:

instsrv.exe:用以安裝和解除安裝可執行的服務

srvany.exe:用於將任何exe程式作為windows服務執行

這2個工具都是是microsoft windows resource kits工具集的實用的小工具 

第1步: 新建windowsform程式

public

partial

class

form1 : form

private

void form1_load ( object

sender , eventargs e )

;_timer.tick += delegate ( object

_sender , eventargs _e )

", datetime.now ) );};}

void _timer_tick ( object

sender , eventargs e )

void witre ( string

context )

private

void button1_click ( object

sender , eventargs e )

}

第2步:安裝,解除安裝

服務的安裝步驟分5小步:

(1)開啟cmd,輸入以下內容,其中windowsforms_windowsservice為你要建立的服務名稱

格式:目錄絕對路徑\instsrv  windowsforms_windowsservice  目錄絕對路徑\srvany.exe

例如:d:\tempwork\win\debug\instsrv.exe  windowsforms_windowsservice  d:\tempwork\win\debug\srvany.exe

(2)regedit開啟登錄檔編輯器,找到以下目錄

hkey_local_machine\system\currentcontrolset\services\windowsforms_windowsservice

(3)滑鼠右鍵單擊windowsforms_windowsservice,建立乙個"項",名稱為"parameters"

例如:(5)開啟services.msc服務控制面板,找到windowsforms_windowsservice服務,滑鼠右鍵-屬性-登陸,勾選"允許服務與桌面互動"

啟動服務,可以看到程式介面

解除安裝服務

d:\tempwork\win\debug\instsrv.exe windowsforms_windowsservice remove

實現乙個簡單的windowsService

首先開啟vs,建立乙個windows service 程式 namespace windowsservice1 public partial class service1 servicebase thread th null public service1 initializecomponent t...

建立主鍵 建立外來鍵 建立約束

建立主鍵 三種方法 建立學生表 第一種 create table student sno char 5 primary key,學號 可以直接指定主鍵 sname char 20 not null,姓名 s char 3 not null,性別 sage integer not null,年齡 sd...

Oracle建立儲存過程 建立函式 建立包

一 oracle建立儲存過程 1 基本語法 create orreplace procedureupdate emp sal name inout type,name inout type,is begin endupdate emp sal 2 寫乙個簡單的例子修改emp表的ename欄位 cre...