• 博客园logo
  • 会员
  • 众包
  • 新闻
  • 博问
  • 闪存
  • 赞助商
  • HarmonyOS
  • Chat2DB
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录
鱼要的是海洋,鸟要的是天空,我要的是自由!
平凡的人不平凡的人生
博客园    首页    新随笔    联系   管理    订阅  订阅

.NET自动服务程序—C#

应用程序开发中,常常需要实现这样一种功能:让服务器在每天的特定时刻运行固定的程序(或者实现固定的操作),比如让系统在每天的2:00备份数据库数据。要实现这样的功能,我们可以使用Windows服务(Windows service)。

Windows service是一种系统自动的、无人值守的程序(仅存在于Windows NT、2000和XP操作系统中),它能够在系统启动时开始运行。用户可以通过Service Control Manager (SCM) Applet或者一些特殊的service-control应用来访问Windows service,使服务在没有用户登录到系统之前执行。

        在.NET出现以前,编写Windows服务是VC++、Delphi才能办到的事情,

VB必须使用第三方控件才可以办到,而且编写起来特别的复杂。使用Microsoft®.NET Framework,我们可通过创建作为服务安装的应用程序来方便地创建Windows服务。

 

设计:

一个Windows服务程序,按照配置文件中的配置,在指定时刻运行指定程序。

流程:

启动服务à读取配置文件à启动定时器

定时器定时触发(比如每隔30秒)à循环需要运行组件时间à时间到à运行指定程序

编写:

创建一个Windows Service

ASPectratio="t">

将Server1.cs更名为SchedulerServer.cs,

双击SchedulerServer.cs打开设计页面,从工具栏的组件中拖Timer控件。

更名为SchedulerTimer,并设置Enabled为flase。

注意必须是Components里面的Timer控件,Windows Forms里面的Timer控件不行。

 

F7浏览代码可以看到如下代码

 

服务器启动的时候运行:

/// <summary>

/// Set things in motion so your service can do its work.

/// </summary>

protected override void OnStart(string[] args)

{

        // TODO: Add code here to start your service.

}

 

服务停止的时候运行:

/// <summary>

/// Stop this service.

/// </summary>

protected override void OnStop()

{

        // TODO: Add code here to perform any tear-down necessary to stop your service.

}

 

添加写日志函数,用来记录服务的日志:

public static void WriteLog(string strLog)

                {

                        string strPath;

                        strPath=System.Environment.SystemDirectory;

                        strPath+=@"\SchedulerServer.txt";

                        FileStream fs = new FileStream(strPath,FileMode.OpenOrCreate,FileAccess.Write);

                        StreamWriter m_streamWriter = new StreamWriter(fs);

                        m_streamWriter.BaseStream.Seek(0, SeekOrigin.End);

                        m_streamWriter.WriteLine(strLog);                       

                        m_streamWriter.Flush();

                        m_streamWriter.Close();

                        fs.Close();

                }

 

 

自动服务配置文件

配置文件保存在系统目录,Windows 2000为WinNT\System32,文件名为SchedulerServer.xml,存储了自动服务的所有配置信息,下面我们来看SchedulerServer.xml的格式:

<SchedulerServer>

        <AutoServer>

                <FilePath>C:\AutoBackup.dll</FilePath>

                <RunTime>02:00</RunTime>

        </AutoServer>

        <AutoServer>

                <FilePath>D:\AutoMail.dll</FilePath>

                <RunTime>03:00</RunTime>

        </AutoServer>

</SchedulerServer>

 

FilePath设置需要运行的组件的路径

RunTime设置需要运行的时间

如果有多个程序需要运行,只需要添加<AutoServer>节点

 

程序中添加读取配置文件函数:

private bool ReadConf()

                {

                        try

                        {

                                string strPath;

                                XmlDocument xmldoc=new XmlDocument();

                                XmlNodeList xmlnd;

 

                                strPath=System.Environment.SystemDirectory+@"\SchedulerServer.xml";

                                xmldoc.Load(strPath);

                                xmlnd=xmldoc.SelectNodes("SchedulerServer/AutoServer");

                                arrConf=new String[2,xmlnd.Count];

                               

                                for(int i=0;i<xmlnd.Count;i++)

                                {

                                        arrConf[0,i]=xmlnd[i].SelectSingleNode("FilePath").InnerXml.Trim();

                                        arrConf[1,i]=xmlnd[i].SelectSingleNode("RunTime").InnerXml.Trim();

                                }

                                return true;

                        }

                        catch(Exception e)

                        {

                                WriteLog(DateTime.Now.ToString());

                                WriteLog("Read Configuration Error:");

                                WriteLog(e.ToString());

                                return false;

                        }

 

                }
 

启动服务:

定义两个变量:

private string[,] arrConf;保存配置信息

private Assembly[] assObj;加载组件

 

在OnStart事件中添加如下代码:

protected override void OnStart(string[] args)

{                     

WriteLog("/************************************************************/");

        WriteLog("ScheculerServer Start at "+DateTime.Now.ToString());

        //Load Configuation

        if(!ReadConf())return;

 

        //Load Assembly

        try

        {

                assObj=new Assembly[arrConf.GetLength(1)];

                for(int i=0;i<assObj.Length;i++)

                {

                        assObj[i]=Assembly.LoadFrom(arrConf[0,i].ToString());

                        arrConf[0,i]="NotRuning";

                }

        }

        catch(Exception e)

        {

                WriteLog(DateTime.Now.ToString());

                WriteLog("Load Dll Error:");

                WriteLog(e.ToString());

        }

 

        //Start Time

        SchedulerTimer.Interval=30000;//设置每30秒触发

        SchedulerTimer.Enabled=true;//启动定时器

}

 

定时器触发:

此处完成检查时间是否运行

private void SchedulerTimer_Elapsed(object sender, System.Timers.ElapsedEventArgs e)

{

        try

        {

                //SchedulerTimer.Enabled=false;

                DateTime dtNow=DateTime.Now;

                DateTime dtRun;

                for(int i=0;i<arrConf.GetLength(1);i++)

                {

                        dtRun=Convert.ToDateTime(arrConf[1,i].ToString());

                        if(dtRun.AddSeconds(-30)<=dtNow && dtNow<=dtRun.AddSeconds(30))                                        {

                                if(arrConf[0,i].ToString().Trim()=="NotRuning")

                                {

                                        foreach(Type t in assObj[i].GetTypes())

                                        {

                                                if(t.IsClass && !t.IsAbstract && t.IsPublic)

                                        {

                                                        Object obj=Activator.CreateInstance(t);                        

                                                        MethodInfo mi=t.GetMethod("Run");

                                                        if(mi!=null)

                                                        {

                                                                mi.Invoke(obj,null);

                                                                obj=null;

                                                                GC.Collect();

                                                                break;

                                                        }

                                                        obj=null;

                                                        GC.Collect();                                                     

                                                }

                                        }

                                        arrConf[0,i]="OnRuning";                                             

                WriteLog("-------------------------------------------------------------");

                WriteLog(DateTime.Now.ToString()+":Runing "+arrConf[1,i]+" "+assObj[i].Location.ToString());

                WriteLog("-------------------------------------------------------------");                                                      

                        }

                       

                }

        catch(Exception ex)

        {

                                WriteLog("##########################################################");

        WriteLog(ex.ToString());

                                WriteLog("##########################################################");

        }

}

 

这时程序的主骨架就完成了,下一步需要制作Windows服务安装程序,切换到设计页面,在属性的右下脚有Add Installer字样,单击,VS.net自动帮你生成安装程序。

ASPectratio="t" v:ext="edit">

打开ProjectInstaller.cs文件,可以看到两个组件,设置其属性。

运行帐号选择LocalSystem,系统以本地系统帐号运行。

启动类型选择自动。

编译为exe,打开VS.NET cmd,进入exe目录,运行InstallUtil.exe SchedulerServer.exe安装服务,成功之后,打开服务管理可以看到刚才的服务,启动服务即可。

服务日志保存在系统目录下的SchedulerServer.txt

/************************************************************/

ScheculerServer Start at 12/24/2003 3:46:21 PM

-------------------------------------------------------------

12/24/2003 3:46:51 PM:Runing 15:47 e:\work\geid\src\geidautocheck\bin\debug\geidautocheck.dll

-------------------------------------------------------------

ScheculerServer Stop at 12/24/2003 3:48:55 PM

/************************************************************/

 

自动运行程序:

1..NET组件(dll)

2.入口为

public void Run()

{}



写好的windows服务程序,打包

按如下的几步来作就可以给服务程序作安装程序了:

将安装程序添加到服务应用程序
1:在“解决方案资源管理器”中,访问要为其添加安装组件的服务的“设计”视图。 

2:单击设计器的背景以选择服务本身,而不是它的任何内容。

3:设计器具有焦点时,右击然后单击“添加安装程序”。 

这时项目中就添加了一个新类 ProjectInstaller 和两个安装组件 ServiceProcessInstaller 和 ServiceInstaller,并且服务的属性值被复制到组件。 

4:单击 ServiceInstaller 组件,验证 ServiceName 属性的值已为与服务本身的 ServiceName 属性的值相同。 

5:若要确定如何启动服务,请单击 ServiceInstaller 组件并将 StartType 属性设置为适当的值。
StartType :
值  结果  
Manual 
 服务安装后,必须手动启动。有关更多信息,请参见如何:启动服务。
 
Automatic 
 每次计算机重新启动时,服务都会自动启动。 
 
Disabled 
 服务无法启动。
 

6:若要确定将要运行服务的安全上下文,请单击 ServiceProcessInstaller 组件并设置适当的属性值。(比如:如果要用系统用户,请选择LocalSystem而不是user)

7:重写需要为其执行自定义处理的所有方法。


8:创建您的安装项目和自定义操作,部署和安装您的服务。

按以上的几步就完成服务程序的安装程序制作,编译好后就可以安装了.
安装成功后,可以打开控制面板里的服务查看刚刚安好的服务程序是否存在.

net 无法启动服务 服务进程无法连接到服务控制器
   可能是服务程序写日志文件,没有权限


我们将研究如何创建一个作为Windows服务的应用程序。内容包含什么是Windows服务,如何创建、安装和调试它们。会用到System.ServiceProcess.ServiceBase命名空间的类。


什么是Windows服务?


  Windows服务应用程序是一种需要长期运行的应用程序,它对于服务器环境特别适合。它没有用户界面,并且也不会产生任何可视输出。任何用户消息都会被写进Windows事件日志。计算机启动时,服务会自动开始运行。它们不要用户一定登录才运行,它们能在包括这个系统内的任何用户环境下运行。通过服务控制管理器,Windows服务是可控的,可以终止、暂停及当需要时启动。

  Windows 服务,以前的NT服务,都是被作为Windows NT操作系统的一部分引进来的。它们在Windows 9x及Windows Me下没有。你需要使用NT级别的操作系统来运行Windows服务,诸如:Windows NT、Windows 2000 Professional或Windows 2000 Server。举例而言,以Windows服务形式的产品有:Microsoft Exchange、SQL Server,还有别的如设置计算机时钟的Windows Time服务。


创建一个Windows服务

  我们即将创建的这个服务除了演示什么也不做。服务被启动时会把一个条目信息登记到一个数据库当中来指明这个服务已经启动了。在服务运行期间,它会在指定的时间间隔内定期创建一个数据库项目记录。服务停止时会创建最后一条数据库记录。这个服务会自动向Windows应用程序日志当中登记下它成功启动或停止时的记录。

  Visual Studio .NET能够使创建一个Windows服务变成相当简单的一件事情。启动我们的演示服务程序的说明概述如下。

1. 新建一个项目
2. 从一个可用的项目模板列表当中选择Windows服务
3. 设计器会以设计模式打开
4. 从工具箱的组件表当中拖动一个Timer对象到这个设计表面上 (注意: 要确保是从组件列表而不是从Windows窗体列表当中使用Timer)
5. 设置Timer属性,Enabled属性为False,Interval属性30000毫秒
6. 切换到代码视图页(按F7或在视图菜单当中选择代码),然后为这个服务填加功能


Windows服务的构成

  在你类后面所包含的代码里,你会注意到你所创建的Windows服务扩充了System.ServiceProcess.Service类。所有以.NET方式建立的Windows服务必须扩充这个类。它会要求你的服务重载下面的方法,Visual Studio默认时包括了这些方法。

• Dispose – 清除任何受控和不受控资源(managed and unmanaged resources)
• OnStart – 控制服务启动
• OnStop – 控制服务停止

数据库表脚本样例

  在这个例子中使用的数据库表是使用下面的T-SQL脚本创建的。我选择SQL Server数据库。你可以很容易修改这个例子让它在Access或任何你所选择的别的数据库下运行。

CREATE TABLE [dbo].[MyServiceLog] (
   [in_LogId] [int] IDENTITY (1, 1) NOT NULL,
   [vc_Status] [nvarchar] (40)
           COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL,
   [dt_Created] [datetime] NOT NULL
) ON [PRIMARY]

Windows服务样例

  下面就是我命名为MyService的Windows服务的所有源代码。大多数源代码是由Visual Studio自动生成的。

using System;
using
 System.Collections;
using
 System.ComponentModel;
using
 System.Data;
using
 System.Data.SqlClient;
using
 System.Diagnostics;
using
 System.ServiceProcess;

namespace
 CodeGuru.MyWindowsService
{
  
public class
 MyService : System.ServiceProcess.ServiceBase
  
{
   
private
 System.Timers.Timer timer1;
   
/**/
/// <remarks> 
   
///
 Required designer variable.
   
/// </remarks>

   private System.ComponentModel.Container components = null;

   
public
 MyService()
   
{
       
//
 This call is required by the Windows.Forms 
       
// Component Designer.

     InitializeComponent();
   }


   
// The main entry point for the process
   static void Main()
   
{
     System.ServiceProcess.ServiceBase[] ServicesToRun;

   
     ServicesToRun 
= new
 System.ServiceProcess.ServiceBase[] 
{ new MyService() }
;

     System.ServiceProcess.ServiceBase.Run(ServicesToRun);
   }


   
/**//// <summary> 
   
///
 Required method for Designer support - do not modify 
   
///
 the contents of this method with the code editor.
   
/// </summary>

   private void InitializeComponent()
   
{
     
this.timer1 = new
 System.Timers.Timer();
     ((System.ComponentModel.ISupportInitialize)
(
this
.timer1)).BeginInit();
     
//
 
     
//
 timer1
     
// 

     this.timer1.Interval = 30000;
     
this.timer1.Elapsed +=
 
   
new System.Timers.ElapsedEventHandler(this
.timer1_Elapsed);
     
//
 
     
//
 MyService
     
// 

     this.ServiceName = "My Sample Service";
     ((System.ComponentModel.ISupportInitialize)
(
this
.timer1)).EndInit();

   }


   
/**//// <summary>
   
/// Clean up any resources being used.
   
/// </summary>

   protected override void Dispose( bool disposing )
   
{
     
if
( disposing )
     
{
      
if (components != null
) 
      
{
         components.Dispose();
      }

     }

     
base.Dispose( disposing );
   }


   
/**//// <summary>
   
/// Set things in motion so your service can do its work.
   
/// </summary>

   protected override void OnStart(string[] args)
   
{
     
this.timer1.Enabled = true
;
     
this.LogMessage("Service Started"
);
   }

 
   
/**//// <summary>
   
/// Stop this service.
   
/// </summary>

   protected override void OnStop()
   
{
     
this.timer1.Enabled = false
;
     
this.LogMessage("Service Stopped"
);
   }


   
/**//*
    * Respond to the Elapsed event of the timer control
    
*/

   
private void timer1_Elapsed(object sender, 
System.Timers.ElapsedEventArgs e)
   
{
     
this.LogMessage("Service Running"
);
   }


   
/**//*
    * Log specified message to database
    
*/

   
private void LogMessage(string Message)
   
{
     SqlConnection connection 
= null
;
     SqlCommand command 
= null
;
     
try

     
{
      connection 
= new
 SqlConnection( 
"Server=localhost;Database=SampleDatabase;Integrated 

Security=false;User Id=sa;Password=;");
command = new SqlCommand(
"INSERT INTO MyServiceLog (vc_Status, dt_Created) 

VALUES ('" + Message + "',getdate())", connection);
      connection.Open();
      
int numrows =
 command.ExecuteNonQuery();
     }

     
catch( Exception ex )
     
{
      System.Diagnostics.Debug.WriteLine(ex.Message);
     }

     
finally
     
{
      command.Dispose();
      connection.Dispose();
     }

   }

  }

}


安装Windows服务

  Windows服务不同于普通Windows应用程序。不可能简简单单地通过运行一个EXE就启动Windows服务了。安装一个Windows服务应该通过使用.NET Framework提供的InstallUtil.exe来完成,或者通过诸如一个Microsoft Installer (MSI)这样的文件部署项目完成。


添加服务安装程序

  创建一个Windows服务,仅用InstallUtil程序去安装这个服务是不够的。你必须还要把一个服务安装程序添加到你的Windows服务当中,这样便于InstallUtil或是任何别的安装程序知道应用你服务的是怎样的配置设置。

1. 将这个服务程序切换到设计视图
2. 右击设计视图选择“添加安装程序”
3. 切换到刚被添加的ProjectInstaller的设计视图
4. 设置serviceInstaller1组件的属性:
    1) ServiceName = My Sample Service
    2) StartType = Automatic
5. 设置serviceProcessInstaller1组件的属性
    1) Account = LocalSystem
6. 生成解决方案

  在完成上面的几个步骤之后,会自动由Visual Studio产生下面的源代码,它包含于ProjectInstaller.cs这个源文件内。

using System;
using
 System.Collections;
using
 System.ComponentModel;
using
 System.Configuration.Install;

namespace
 CodeGuru.MyWindowsService
{
  
/**/
/// <summary>
  
/// Summary description for ProjectInstaller.
  
/// </summary>

  [RunInstaller(true)]
  
public class
 ProjectInstaller : 
System.Configuration.Install.Installer
  
{
   
private
 System.ServiceProcess.ServiceProcessInstaller 
serviceProcessInstaller1;
   
private
 System.ServiceProcess.ServiceInstaller serviceInstaller1;
   
/**/
/// <summary>
   
/// Required designer variable.
   
/// </summary>

   private System.ComponentModel.Container components = null;

   
public
 ProjectInstaller()
   
{
     
// This call is required by the Designer.

     InitializeComponent();

     
// TODO: Add any initialization after the InitComponent call

   }


   
Component Designer generated code#region Component Designer generated code
   
/**//// <summary>
   
/// Required method for Designer support - do not modify
   
///
 the contents of this method with the code editor.
   
/// </summary>

   private void InitializeComponent()
   
{
     
this.serviceProcessInstaller1 = new
 
System.ServiceProcess.ServiceProcessInstaller();
     
this.serviceInstaller1 = new
 
System.ServiceProcess.ServiceInstaller();
     
//
 
     
//
 serviceProcessInstaller1
     
// 

     this.serviceProcessInstaller1.Account = 
System.ServiceProcess.ServiceAccount.LocalSystem;
     
this.serviceProcessInstaller1.Password = null
;
     
this.serviceProcessInstaller1.Username = null
;
     
//
 
     
//
 serviceInstaller1
     
// 

     this.serviceInstaller1.ServiceName = "My Sample Service";
     
this.serviceInstaller1.StartType =
 
System.ServiceProcess.ServiceStartMode.Automatic;
     
//
 
     
//
 ProjectInstaller
     
// 

     this.Installers.AddRange(new 
System.Configuration.Install.Installer[] 
{this.serviceProcessInstaller1, this.serviceInstaller1}
);
}

   
#endregion

  }

}


用InstallUtil安装Windows服务

  现在这个服务已经生成,你需要把它安装好才能使用。下面操作会指导你安装你的新服务。

1. 打开Visual Studio .NET命令提示
2. 改变路径到你项目所在的bin\Debug文件夹位置(如果你以Release模式编译则在bin\Release文件夹)
3. 执行命令“InstallUtil.exe MyWindowsService.exe”注册这个服务,使它建立一个合适的注册项。
4. 右击桌面上“我的电脑”,选择“管理”就可以打计算机管理控制台
5. 在“服务和应用程序”里面的“服务”部分里,你可以发现你的Windows服务已经包含在服务列表当中了
6. 右击你的服务选择启动就可以启动你的服务了

  在每次需要修改Windows服务时,这就会要求你卸载和重新安装这个服务。不过要注意在卸载这个服务前,最好确保服务管理控制台已经关闭,这会是一个很好的习惯。如果没有这样操作的话,你可能在卸载和重安装Windows服务时会遇到麻烦。仅卸载服务的话,可以执行相的InstallUtil命令用于注销服务,不过要在后面加一个/u命令开关。


调试Windows服务

  从另外的角度度看,调试Windows服务绝不同于一个普通的应用程序。调试Windows服务要求的步骤更多。服务不能象你对普通应用程序做的那样,只要简单地在开发环境下执行就可以调试了。服务必须首先被安装和启动,这一点在前面部分我们已经做到了。为了便于跟踪调试代码,一旦服务被启动,你就要用Visual Studio把运行的进程附加进来(attach)。记住,对你的Windows服务做的任何修改都要对这个服务进行卸载和重安装。


附加正在运行的Windows服务

  为了调试程序,有些附加Windows服务的操作说明。这些操作假定你已经安装了这个Windows服务并且它正在运行。

1. 用Visual Studio装载这个项目
2. 点击“调试”菜单
3. 点击“进程”菜单
4. 确保 显示系统进程 被选
5. 在 可用进程 列表中,把进程定位于你的可执行文件名称上点击选中它
6. 点击 附加 按钮
7. 点击 确定
8. 点击 关闭
9. 在timer1_Elapsed方法里设置一个断点,然后等它执行


总结

  现在你应该对Windows服务是什么,以及如何创建、安装和调试它们有一个粗略的认识了。Windows服务的额处的功能你可以自行研究。这些功能包括暂停(OnPause)和恢复(OnContinue)的能力。暂停和恢复的能力在默认情况下没有被启用,要通过Windows服务属性来设置。



可插拔windows服务

如何实现可插拔的windows服务
关于windows服务的创建相关资源很多,园子里面也有很多这样的文章。
整理了一下相关blog如下,
http://www.cnblogs.com/wuxilin/archive/2006/06/04/416838.html
http://www.cnblogs.com/wujm/archive/2005/05/12/154369.html
http://www.cnblogs.com/caca/archive/2005/02/25/109028.html
http://www.cnblogs.com/laiwen/archive/2005/08/21/219590.html

归纳一下步骤大概如下:
1.创建服务:
1.1我们应该创建windows服务类型的project,IDE会自动从serviceBase类继承一个类出来,在这个类里面有on_star和on_stop两个方法,可以在里面写入启动逻辑和停止逻辑
在设计视图下我们可以修改service的属性
Autolog                 是否自动写入系统的日志文件
CanHandlePowerEvent     服务时候接受电源事件
CanPauseAndContinue     服务是否接受暂停或继续运行的请求
CanShutdown             服务是否在运行它的计算机关闭时收到通知,以便能够调用 OnShutDown 过程
CanStop                 服务是否接受停止运行的请求
ServiceName             服务名
1.2选择服务组件,并切换到设计模式,右键->Add Installer,生成安装文件。安装文件中的属性修改可以参考下面的内容:
projectInstall参数修改Account(指定用户还是使用本地系统用户)
ServiceInstall参数修改:
描述名:Description,
显示名:DisplayName,
ServiName:服务名,要和刚才建立的服务名一一对应才行
StartType:(Manual:服务安装后,必须手动启动.Automatic:每次计算机重新启动时,服务都会自动启动;Disabled:服务无法启动)

2安装
安装服务要使用微软提供的工具InstallUtil,这个工具在c:/windows/wicrosoft.net/framework/[version]中
使用InstallUtil 安装,使用InstallUtil /u 反安装服务

如果还有什么可以参考刚才说的那几个blog,ok一切都很简单。但是如果要做为一个企业级的应用,往往会有别的场景需求,下面说一下我曾碰到的场景要求
1.我们的windows服务可能不止满足一种业务逻辑,比如:如果你的机器安装了oracle的话,就会有OracleService服务,我们启动数据库的时候可以只是启动tns服务和OracleService服务就可以了,但是实际上启动OracleService服务的同时会启动好几个服务,例如:DBWR(数据文件写入),LGWR(日志文件写入),SMON(系统监护),PMON(用户进程监),CKPT(检查点,同步数据文件,日志文件,控制文件等)。有时为了性能的考虑,一个服务也可能要多线程的运行业务逻辑。比如一个导入文件的数据接口服务,如果数据量非常大就要这样考虑。一般如果执行多个任务,可以有两种方法:1.开多个线程;2.在一个线程中顺序进行(可以加上时间控制)。我觉得这里最灵活的方法是用.net本省的多线程支持来解决。
2.要能在不改变系统结构的情况下,可以非常快速的支持新的服务要求。这说明我们在onstar方法中不能写入相关类的处理逻辑,必须要用某种方法解耦才行。

实现方法:
参考下面的类图

servcive类和具体的业务实现类解耦,我们可以使用工厂模式生成业务逻辑类,每个业务逻辑类启动在一个新的线程中,在.net中工厂模式可以用反射非常方便的实现,这块代码如下:

typeInfo = node.Attributes["type"].Value;
                    Type type 
= Type.GetType(typeInfo);
                    IService instance 
= (IService)Activator.CreateInstance(type);
                    
//初始化服务
                    instance.Initialize(node);
                    instanceArray.Add(instance);

                    
//在新线程中运行服务,每个服务使用相同的安全上下文
                    ThreadStart ts = new ThreadStart(instance.Start);
                    Thread t 
= new Thread(ts);
                    t.Start();


从配置文件中读出需要加载的业务逻辑类,实例化后用ThreadStart调用其中的Start方法,启动新线程。这里对配置文件的读取如果要求比较高可以采用Enterprise Libary的Login block来做。
在OnStop方法中调用每个类的Stop方法,如果想对每个Stop方法异步调用,可以用delegate封装接口的Stop方法用
BeginInvoke方法实现异步调用

  foreach (object o in instanceArray)
   
{
    
try
    
{
     IService service 
= (IService)o;
     
if (service !=null)
     
{
            
//异步调用每个服务的stop方法来停止业务组件
      OnStopDelegate osd = new OnStopDelegate(service.Stop);
      osd.BeginInvoke(
null,null);
     }

    }

    
catch (Exception ex)
    
{
     
//
    }

   }


这样,整个windows服务就实现了,这里还有个额外的好处是可以热部署,即不需要删除已经部署的windows服务,将新的DLL覆盖将以前的DLL覆盖,修改*.exe.config文件并重新启动就可以了。其实我觉得停止服务,重新编译部署后在启动服务花费的时间可能比这样还少些。

关于调试:
windows服务常用的调试方法是用调试->附加到进程的方法。
选择显示所有用户进程,我们已经部署并启动的windows服务进程就会出现,并跳到我们设置好的断点处。但是这样对service中的onstart部分调试比较麻烦。我还是推荐创建一个porjec做为调试用,这里面可以写onstart部分的代码,代码不多,非常方便。大家就可以象调试普通windows程序一样调试这windows服务代码了



微软对此在这两篇文章那个中给出了相应的解决方案,http://support.microsoft.com/kb/820639/zh-cn,http://support.microsoft.com/kb/842793/zh-cn。
实际上就是在使用Windows.Forms命名空间中的timer无效时则使用System.Timers中的组件,再不行的话使用Threading中的Timer组件。我遇到的是第一种情况,通过使用System.Timers中的Timer,问题解决了。

症状

您添加到 Windows 服务 Microsoft WindowsForms 计时器组件。 启用计时器, 然后您设置计时器以引发事件的间隔。 计算机, 上安装 Windows 服务, 然后您启动服务。 不引发 计时器 事件在 Windows 服务。

注意 : WindowsForms 计时器组件位于 System.Windows.Forms 命名空间中。

回到顶端

原因

WindowsForms 计时器组件用于 WindowsForms 环境。 WindowsForms 计时器组件不用于服务器环境。 计时器如果在 Windows 服务使用因此, 可能不引发事件。

回到顶端

解决方案

要解决此问题, 使用服务器计时器从命名空间 System.Timers 代替 WindowsForms 计时器从 System.Windows.Forms 命名空间。 要这样做, 请按照下列步骤操作:
1. 在要删除 Service 1 类命令窗口运行以下命令:
installutil /u WindowsService1.exe
注意 WindowsService1.exe 位于您的项目文件夹下 bin 文件夹中。 添加路径是您 WindowsService1.exe。
2. 切换到 Windows 服务项目。
3. 在 Service 1 设计器窗口, 单击 Timer 1 控件。
4. 按 DELETE 键从设计器窗口中删除该 Timer 1 控件。
5. 在工具栏上, 单击 组件 选项卡。
6. 从工具栏, 将 Timer 控件拖到设计器窗口。
7. 双击设计器窗口以查看代码窗口上 Timer 1 控件。
8. Timer 1 控件的 Tick 事件处理程序中移动到的 Timer 1 控件 Elapsed 事件处理程序代码
9. 移除 Tick 事件处理程序。
10. 在 生成 菜单上, 单击 BuildSolution@@@ 。
11. 在命令窗口运行以下命令:
installutil WindowsService1.exe
12. 在 管理工具 菜单上, 单击 服务 。
13. 右击 Service 1 , 然后单击 开始 。
14. 等待几秒钟, 右击 Service 1 , 然后单击 停止 。
15. 打开, C:\sample.txt 文件, 然后注意文本。

以 开始 和 停止 Tick 文本显示。

回到顶端

状态

此行为是设计使然。

回到顶端

更多信息

再现问题的步骤

1. 启动 MicrosoftVisualStudio.NET。
2. 通过使用 MicrosoftVisualBasic.NET 或 MicrosoftVisualC # .NET 打开一个新 Windows 服务项目。

默认情况下, 创建 Service 1 。
3. 在工具箱中, 单击 WindowsForms 选项卡。
4. 将一个 Timer 控件从工具箱拖到 Service 1 设计器窗口。
5. 在设计器窗口, 双击 Timer 1 控件。

显示 Service 1 代码窗口。
6. 如果您使用 VisualC # .NET, 将以下语句添加到开头的代码:
using System.IO;
7. 以下代码添加到 Service 1 类的 OnStart 过程。

VisualBasic.NET 代码
'Set the interval of the timer to 3 seconds.
            Timer1.Interval = 3000
            Timer1.Enabled = True
            'Open the sample.txt file in append mode.
            FileOpen(1, "C:\sample.txt", OpenMode.Append)
            'Print text to the "C:/sample.txt file.
            Print(1, "Start")
            FileClose()
VisualC # .NET 代码
//Set the interval of timer to 3 seconds.
            timer1.Interval =3000;
            //Enable the timer.
            timer1.Enabled =true;
            //Append the text to the sample file.
            StreamWriter writer =File.AppendText(@"C:\sample.txt");
            writer.WriteLine("Start");
            writer.Close();
8. 以下代码添加到 Service 1 类的 OnStop 过程。

VisualBasic.NET 代码
'Open the C:\sample.txt file in append mode.
            FileOpen(1, "C:\sample.txt", OpenMode.Append)
            'Print the text 'Stop' to the C:\sample.txt file.
            Print(1, "Stop")
            FileClose()
VisualC # .NET 代码
//Append the text Stop to the C:\sample.txt file.
            StreamWriter writer =File.AppendText(@"C:\sample.txt");
            writer.WriteLine("Stop");
            writer.Close();
9. 将以下代码添加到 Timer 1 组件的 Tick 事件。

VisualBasic.NET 代码
 'Set the enabled property to false.
            Timer1.Enabled = False
            'Open the C:\sample.txt file in append mode.
            FileOpen(1, "C:\sample.txt", OpenMode.Append)
            'Print the text 'Tick' to the C:\sample.txt file.
            Print(1, "Tick")
            FileClose()
            'Enable the timer.
            Timer1.Enabled = True
VisualC # .NET 代码
//Set the enable property to false.
            timer1.Enabled =false;
            //Append the text Tick to the C:\sample.txt file.
            StreamWriter writer =File.AppendText(@"C:\sample.txt");
            writer.WriteLine("Tick");
            writer.Close();
            timer1.Enabled =true;
10. 在 视图 菜单上, 单击 设计器 。
11. 右击 设计器 , 然后单击 添加 Installer 。

默认情况下, ServiceInstaller 1 和 ServiceProcessInstaller1 创建。
12. ServiceInstaller 1 , 右击, 然后单击 属性 。
13. 将 DisplayName 属性设置为 Service 1 。

注意 最好要将 ServiceName 属性设为 Service 1 。
14. 右击 ServiceProcessInstaller1 , 然后单击 属性 。
15. 在属性窗口, ServiceProcessInstaller1 将以 LocalSystem 帐户 属性。
16. 在 生成 菜单上, 单击 BuildSolution@@@ 。
17. 开始 , 依次 运行 。
18. 在运行窗口, 在 打开 框中, 键入 cmd , 然后按 ENTER 键。
19. 在命令提示符下, 运行以下命令:
installutil  WindowsService1.exe
注意 如果使用任何其他服务具有相同名称不安装您 Service 1 服务。
20. 完成安装步骤后, AdministrativeTools , 控制面板中单击, 然后单击 服务 。
21. 在服务窗口, 右击 Service 1 , 依次 开始 。
22. 等待几秒钟, 右击 Service 1 , 然后单击 停止 。
23. 打开 C:\sample.txt 文件。

显示文本 StartStop 。 不引发 Timer 1 Tick 事件, 并且不打印到 C:\sample.txt 文件 Tick 文本。
posted @ 2008-03-25 17:43  伊凡  阅读(2855)  评论(4)    收藏  举报
刷新页面返回顶部
博客园  ©  2004-2025
浙公网安备 33010602011771号 浙ICP备2021040463号-3