利用vs.net快速开发windows服务(总结)
引用 http://www.cnblogs.com/lovecherry/archive/2005/03/25/125527.html
在很多應用中需要做windows服務來操作數據庫等操作,比如
(1)一些非常慢的數據庫操作,不想一次性去做,想慢慢的通過服務定時去做,比如定時為數據庫備份等
(2)在.net Remoting中利用windows服務來做Host
利用vs.net我們可以在幾分鐘之內建立其windows服務,非常簡單
下面說一下步驟
1.?新建一個項目
2.?從一個可用的項目模板列表當中選擇Windows服務
3.?設計器會以設計模式打開
4.?從工具箱的組件表當中拖動一個Timer對象到這個設計表面上?(注意:?要確保是從組件列表而不是從Windows窗體列表當中使用Timer)?
5.?設置Timer屬性,Interval屬性200毫秒(1秒進行5次數據庫操作)
6.?然后為這個服務填加功能
7.雙擊這個Timer,然后在里面寫一些數據庫操作的代碼,比如
?SqlConnection conn=new SqlConnection("server=127.0.0.1;database=test;uid=sa;pwd=275280");
???SqlCommand comm=-new SqlCommand("insert into tb1 ('111',11)",conn);
???conn.Open();
???comm.ExecuteNonQuery();
???conn.Close();
8.?將這個服務程序切換到設計視圖
9.?右擊設計視圖選擇“添加安裝程序”
10.?切換到剛被添加的ProjectInstaller的設計視圖
11.?設置serviceInstaller1組件的屬性:?
????1)?ServiceName?=?My?Sample?Service
????2)?StartType?=?Automatic (開機自動運行)
12.?設置serviceProcessInstaller1組件的屬性??Account?=?LocalSystem
13.?改變路徑到你項目所在的bin\Debug文件夾位置(如果你以Release模式編譯則在bin\Release文件夾)
14.?執行命令“InstallUtil.exe?MyWindowsService.exe”注冊這個服務,使它建立一個合適的注冊項。(InstallUtil這個程序在WINDOWS文件夾\Microsoft.NET\Framework\v1.1.4322下面)
15.?右擊桌面上“我的電腦”,選擇“管理”就可以打計算機管理控制臺
16.?在“服務和應用程序”里面的“服務”部分里,你可以發現你的Windows服務已經包含在服務列表當中了
17.?右擊你的服務選擇啟動就可以啟動你的服務了
看看數據庫是不是一秒多了5個記錄啊
需要注意的是:
如果你修改了這個服務,路徑沒有變化的話是不需要重新注冊服務的,如果路徑發生了變化,需要先卸載這個服務InstallUtil.exe /u參數,然后再重新安裝這個服務,不能直接安裝。還有就是windows服務是沒有界面的,不要企圖用控制的輸出方式來輸出一些信息,你只能添加一個EventLog,通過WriteEntry()來寫日志。
關于怎么用windows服務來做一個遠程服務可以看一下http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnnetsec/html/SecNetHT15.asp
問題
用以上說的建立了一個服務,啟動后,timer卻不起作用,sql不執行,不知道為什么?服務都是順利啟動的,但卻不執行任務?
可以在 OnStart() 方法里加上 this.timer1.Enabled = true;
另外看一下this.timer1.Elapsed += new System.Timers.ElapsedEventHandler(this.timer1_Elapsed); 是否和你的事件關聯上
如何實現可插拔的windows服務
如果只是實現普通需求非常容易,可以參考文章里的那幾個blog。但是如果要做為一個企業級的應用,往往會有別的場景需求。這里描述了能在不改變系統結構的情況下,可以非常快速的支持新的服務要求,并且以多線程方式支持多種業務邏輯的weindows服務開發方法,最后是一些調試技巧。
文章地址: http://www.cnblogs.com/lodestar/archive/2007/04/18/718615.html??
用C#創建Windows服務(Windows Services)
文章地址: http://caca.cnblogs.com/archive/2005/02/25/109028.aspx
C#安裝卸載服務?
引用?http://blog.csdn.net/lizhizhe2000/archive/2006/09/19/1249209.aspx
這是一個安裝和卸載服務的類,有興趣可以看一下.
using?System;using?System.Runtime.InteropServices;
namespace?EAE.MyServiceInstaller
{
?class?ServiceInstaller
?{
??#region?Private?Variables
??private?string?_servicePath;
??private?string?_serviceName;
??private?string?_serviceDisplayName;
??#endregion?Private?Variables
??#region?DLLImport
??[DllImport("advapi32.dll")]
??public?static?extern?IntPtr?OpenSCManager(string?lpMachineName,string?lpSCDB,?int?scParameter);
??[DllImport("Advapi32.dll")]
??public?static?extern?IntPtr?CreateService(IntPtr?SC_HANDLE,string?lpSvcName,string?lpDisplayName,?
???int?dwDesiredAccess,int?dwServiceType,int?dwStartType,int?dwErrorControl,string?lpPathName,?
???string?lpLoadOrderGroup,int?lpdwTagId,string?lpDependencies,string?lpServiceStartName,string?lpPassword);
??[DllImport("advapi32.dll")]
??public?static?extern?void?CloseServiceHandle(IntPtr?SCHANDLE);
??[DllImport("advapi32.dll")]
??public?static?extern?int?StartService(IntPtr?SVHANDLE,int?dwNumServiceArgs,string?lpServiceArgVectors);
??[DllImport("advapi32.dll",SetLastError=true)]
??public?static?extern?IntPtr?OpenService(IntPtr?SCHANDLE,string?lpSvcName,int?dwNumServiceArgs);
??[DllImport("advapi32.dll")]
??public?static?extern?int?DeleteService(IntPtr?SVHANDLE);
??[DllImport("kernel32.dll")]
??public?static?extern?int?GetLastError();
??#endregion?DLLImport
//??///?
//??///?應用程序入口.
//??///?
//
//??[STAThread]
//??static?void?Main(string[]?args)
//??{
//
//???string?svcPath;
//???string?svcName;
//???string?svcDispName;
//???//服務程序的路徑
//???svcPath?=?@"d:\service\EAEWS.exe";
//???svcDispName="myEAEWS";
//???svcName=?"myEAEWS";
//???ServiceInstaller?c?=?new?ServiceInstaller();
//???c.InstallService(svcPath,?svcName,?svcDispName);
//???Console.Read();
//
//??}
??///?
??///?安裝和運行
??///?
??///?程序路徑.
??///?服務名
??///?服務顯示名稱.
??///?服務安裝是否成功.
??public?bool?InstallService(string?svcPath,?string?svcName,?string?svcDispName)
??{
???#region?Constants?declaration.
???int?SC_MANAGER_CREATE_SERVICE?=?0x0002;
???int?SERVICE_WIN32_OWN_PROCESS?=?0x00000010;
???//int?SERVICE_DEMAND_START?=?0x00000003;
???int?SERVICE_ERROR_NORMAL?=?0x00000001;
???int?STANDARD_RIGHTS_REQUIRED?=?0xF0000;
???int?SERVICE_QUERY_CONFIG?=?0x0001;
???int?SERVICE_CHANGE_CONFIG?=?0x0002;
???int?SERVICE_QUERY_STATUS?=?0x0004;
???int?SERVICE_ENUMERATE_DEPENDENTS?=?0x0008;
???int?SERVICE_START?=0x0010;
???int?SERVICE_STOP?=0x0020;
???int?SERVICE_PAUSE_CONTINUE?=0x0040;
???int?SERVICE_INTERROGATE?=0x0080;
???int?SERVICE_USER_DEFINED_CONTROL?=0x0100;
???int?SERVICE_ALL_ACCESS?=?(STANDARD_RIGHTS_REQUIRED?|?
????SERVICE_QUERY_CONFIG?|
????SERVICE_CHANGE_CONFIG?|
????SERVICE_QUERY_STATUS?|?
????SERVICE_ENUMERATE_DEPENDENTS?|?
????SERVICE_START?|?
????SERVICE_STOP?|?
????SERVICE_PAUSE_CONTINUE?|?
????SERVICE_INTERROGATE?|?
????SERVICE_USER_DEFINED_CONTROL);
???int?SERVICE_AUTO_START?=?0x00000002;
???#endregion?Constants?declaration.
???try
???{
????IntPtr?sc_handle?=?OpenSCManager(null,null,SC_MANAGER_CREATE_SERVICE);
????if?(sc_handle.ToInt32()?!=?0)
????{
?????IntPtr?sv_handle?=?CreateService(sc_handle,svcName,svcDispName,SERVICE_ALL_ACCESS,SERVICE_WIN32_OWN_PROCESS,?SERVICE_AUTO_START,SERVICE_ERROR_NORMAL,svcPath,null,0,null,null,null);
?????if(sv_handle.ToInt32()?==0)
?????{
??????CloseServiceHandle(sc_handle);
??????return?false;
?????}
?????else
?????{
??????//試嘗啟動服務
??????int?i?=?StartService(sv_handle,0,null);
??????if(i==0)
??????{
???????return?false;
??????}
??????CloseServiceHandle(sc_handle);
??????return?true;
?????}
????}
????else
?????return?false;
???}
???catch(Exception?e)
???{
????throw?e;
???}
??}
??///?
??///?反安裝服務.
??///?
??///?服務名.
??public?bool?UnInstallService(string?svcName)
??{
???int?GENERIC_WRITE?=?0x40000000;
???IntPtr?sc_hndl?=?OpenSCManager(null,null,GENERIC_WRITE);
???if(sc_hndl.ToInt32()?!=0)
???{
????int?DELETE?=?0x10000;
????IntPtr?svc_hndl?=?OpenService(sc_hndl,svcName,DELETE);
????if(svc_hndl.ToInt32()?!=0)
????{?
?????int?i?=?DeleteService(svc_hndl);
?????if?(i?!=?0)
?????{
??????CloseServiceHandle(sc_hndl);
??????return?true;
?????}
?????else
?????{
??????CloseServiceHandle(sc_hndl);
??????return?false;
?????}
????}
????else
?????return?false;
???}
???else
????return?false;
??}?
?}
}?
我用C#寫了一個服務,用定時器定時檢測,不符合某個條件我就退出服務。
下面的示例使用?ServiceController?類檢查?Telnet?服務的當前狀態。如果該服務已停止,此示例將啟動該服務。如果該服務正在運行,此示例將停止該服務。
//?If?it?is?started?(running,?paused,?etc),?stop?the?service.
//?If?it?is?stopped,?start?the?service.
ServiceController?sc?=?new?ServiceController("Telnet");
Console.WriteLine("The?Telnet?service?status?is?currently?set?to?{0}",?
sc.Status.ToString());
if?((sc.Status.Equals(ServiceControllerStatus.Stopped))?||
????(sc.Status.Equals(ServiceControllerStatus.StopPending)))
{
??//?Start?the?service?if?the?current?status?is?stopped.
??Console.WriteLine("Starting?the?Telnet?service");
??sc.Start();
}?
else
{
??//?Stop?the?service?if?its?status?is?not?set?to?"Stopped".
??Console.WriteLine("Stopping?the?Telnet?service");
??sc.Stop();
}?
//?Refresh?and?display?the?current?service?status.
sc.Refresh();
Console.WriteLine("The?Telnet?service?status?is?now?set?to?{0}.",?sc.Status.ToString());
Windows服務開發相關文章收集
來源:http://www.cnblogs.com/shiningrise/archive/2007/08/16/857762.html
|
|
總結
最近公司要我做這個,我看了相關的文章,便整理了一下.
實踐的時候也出現了服務不執行操作的現象,希望大家一起共同探討一下,相互學習.
總結
以上是生活随笔為你收集整理的利用vs.net快速开发windows服务(总结)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 用JScript.net写.net应用程
- 下一篇: 工作空间从Windows转向fc8