• C#开发部署Windows服务示例


    本文和大家一同分享一下如何使用C#语言来开发系统服务的方法,非常详细哦。

          默认情况下,服务运行在System账户之下。与一般的应用程序相比,服务自动运行的,并且无需与用户交互。

          在.Net Framework中,与Windows服务相关的类位于System.ServiceProcess命名空间之下,其中,ServiceBase类提供Windows服务的基类,我们编写的服务必须从该类派生。

          ServiceBase包含四个可被重载的方法:OnStart、OnStop、OnPause以及OnContinue,顾名思义,这四个方法在服务启动、停止、暂停以及继续动作时被调用(对服务的控制可以通过控制面板中,管理工具下的服务控制管理器实现(或直接在运行里面输入services.msc))。

          一个承载服务的可执行文件可以包含多个服务,每一个服务对应一个从ServiceBase派生的类。应注意,运行可执行文件与启动服务并不相同,运行可执行文件时,将调用服务类的构造函数,完成成将调用服务的OnStart方法。而启动服务仅仅是调用服务的OnStart方法。

          通过服务控制管理器(services.msc)可以实现与服务的交互,相应的,在ServiceBase中提供两个属性用于向管理器表明服务支持的控制动作:CanStop以及CanPauseAndContinue,当设置为True时,在管理器中服务的停止、暂停和继续菜单可用,反之,则不可用。

          另  外,CanShutdown用户表明在系统关闭时,是否向服务发送一个通知。

          与Windows服务相关的另一个重要类名为:ServiceInstaller,该类用于安装一个服务。一个ServiceBase应该对应一个ServiceInstaller。注意,服务的安装是通过工具installutil来进行的,该工具将会调用服务可执行文件中的Install类,用于实现服务的安装。

          一般情况下,我们从Install类中派生出我们自己的安装类,然后在构造函数中实例化服务对应的ServiceInstaller类(一个或多个)以及一个表示安装进程的ServiceProcessInstaller类(该类用于控制安装进程,如指定运行安装进程的账户)。在安装类中,我们应该指定RunInstallerAttribute属性,这样installutil工具才能通过反射技术找出可执行文件中存在的安装类。

          下面给出一个简单的服务,它每隔5秒钟向应用程序事件记录中写入一条信息(当前时间),通过该示例可了解服务的编写及调试方法:

    1、新建一个项目,名称为TestService,项目类型为Windows Service

    2、将服务代码文件名称修改为TestService

    3、切换到TestService的代码视图,编写代码如下:

     TestService.cs [http://www.xueit.com]
    using System;

    using System.Collections.Generic;

    using System.ComponentModel;

    using System.Data;

    using System.Diagnostics;

    using System.ServiceProcess;

    using System.Text;

    using System.Threading;


    namespace TestService

    {

        public partial class TestService : ServiceBase

        {

            Thread thread;

            public TestService()

            {

                InitializeComponent();


                this.ServiceName = ".Net Test Service";

               

            }


            protected override void OnStart(string[] args)

            {

                Console.WriteLine("The Service Is Runing!");


                if (thread == null)

                {

                    this.thread = new Thread(this.ThreadRun);

                }

                this.thread.IsBackground = true;

                this.thread.Start();

            }


            protected override void OnStop()

            {

                if (this.thread != null)

                {

                    if (this.thread.ThreadState == System.Threading.ThreadState.Running)

                    {

                        this.thread.Abort();

                    }

                }

            }


            private void ThreadRun()

            {

                while (true)

                {

                    Thread.Sleep(5000);

                    EventLog.WriteEntry(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));

                }

            }

        }

    }


    4、新建一个类,文件名为TestInstall.cs,代码如下:

     Code [http://www.xueit.com]
    using System;

    using System.Collections.Generic;

    using System.Linq;

    using System.Text;

    using System.ServiceProcess;

    using System.Configuration.Install;

    using System.ComponentModel;


    namespace TestService

    {

        [RunInstaller(true)]

        public class TestInstall : Installer

        {

            ServiceProcessInstaller processInstall;

            ServiceInstaller serviceInstall;


            public TestInstall()

            {

                this.processInstall = new ServiceProcessInstaller();

                this.serviceInstall = new ServiceInstaller();


                processInstall.Account = ServiceAccount.LocalSystem;

                this.serviceInstall.ServiceName = ".Net Test Service";


                this.Installers.Add(this.serviceInstall);

                this.Installers.Add(this.processInstall);

            }

        }

    }
    5、编译整个解决方案

    6、通过installutil命令安装服务:

         1)、进入Visual Studio 2008命令提示

         2)、切换当前路径到项目路径的Debug目录,如:

                cd D:\Others\Develop\CSharp\TestService\TestService\bin\Debug

         3)、安装服务,命令:

               installutil TestService.exe

    4、启动服务,开始 运行 输入services.msc,我们将看到服务列表中已经存在我们的服务,名称为.Net Test Service

    5、在名称上单击右键,启动服务

    6、如何调试服务呢?确保服务启动后,返回Visual Studio,选择调试菜单中的Attach To Process命令,在弹出窗口中勾选Show processes from all users,然后查找我们的服务进程:TestService.exe,选择它,然后点击Attach按钮。Attach后,如果我们在ThreadRun过程中加入断点,则下一个循环到达后服务将会挂起,进入调试状态。

    7、在本例中我们是不断地向应用程序日记中写入记录,要查看此结果可通过以下方式:启动事件查看器(开始 运行 输入eventvwr.msc),选择应用程序,在右方列表中会存在来源为.Net Test Service的记录,它既是我们的服务进程写入的。双击进入可看到事件描述(运行时得当前时间)

    8、卸载服务,通过如下命令(先通过服务控制管理器停止服务):

        installutil /u TestService.exe

  • 相关阅读:
    iOS的生命周期
    【iOS开发】在 App 中加入 AdMob 广告
    iOS性能优化:Instruments
    动态的计算行高 加载数据源 有多少显示多少 tableView 包含 colloctionView 显示复杂的界面写法
    Java String.split()注意点
    Java eclipse export jar包 包括第三方引入的jar
    微信网页授权获取用户基本信息
    android连接本地tomcat服务器,报timeout
    iOS内存管理retain,assign,copy,strong,weak
    static与全局与局部变量的区别
  • 原文地址:https://www.cnblogs.com/liufei88866/p/2001017.html
Copyright © 2020-2023  润新知