当前位置: 移动技术网 > IT编程>开发语言>.net > 使用C#调试Windows服务模板项目

使用C#调试Windows服务模板项目

2019年07月20日  | 移动技术网IT编程  | 我要评论

修电视,历届河南省委书记,喇叭天线

windows服务是非常强大的应用程序,可用于在backgorund中执行许多不同类型的任务。他们可以在不需要任何用户登录的情况下启动,并且可以使用除登录用户之外的其他用户帐户运行。但是,如果通过遵循常规服务开发步骤开发windows服务应用程序,即使在开发环境中也难以调试。

本文提出了一种不使用任何服务开发库(如topshelf)开发windows服务的不同方法,以便在开发阶段轻松监视和调试。

特征

示例项目具有以下功能;

  • 它在调试模式下作为控制台应用程序运行,在发布模式下作为常规windows服务运行。
  • 根据日志类型在控制台上显示具有不同文本颜色的日志消息。
  • windows服务相关操作和实际后台任务处理操作是分开的,因此可以轻松地对实际服务操作进行单元测试。
  • 可以使用installutil.exe安装和卸载服务。但是您不需要安装它来进行测试和调试。

准备此应用程序的步骤

示例项目是使用visual studio 2017准备的。

1-创建windows服务项目

在visual studio中,单击“文件”\“新建”\“项目”。选择“visual c#\ windows桌面\ windows服务”项目类型。

使用c#调试windows服务模板项目

2-将项目输出类型从windows应用程序更改为控制台应用程序

右键单击项目名称并选择“属性”,打开项目属性。

从“输出类型”选择列表中选择控制台应用程序。

使用c#调试windows服务模板项目

3-安装log4net软件包

从package manager控制台或manage nuget packages菜单选项安装log4net nuget软件包。

4-配置log4net

log4net是一个非常强大的日志库,可以将日志写入许多目标,如文本文件,控制台,数据库,windows事件日志,甚至可以将它们作为电子邮件发送。这些日志编写器(或目标)称为“appender”。log4net配置必须至少有一个appender,但它可能有很多。每个appender都有自己的设置。

log4net配置可以添加到app.config文件中,也可以是单独的文件。我更喜欢单独的文件方法,因此为log4net添加两个配置文件,用于调试和发布模式。名称无关紧要,您可以将它们命名为“ log4net.debug.config ”和“ log4net.prod.config ”。debug配置文件有两个appender; rollingfileappender和  coloredconsoleappender生产配置文件还有两个appender; rollingfileappender和  eventlog但  eventlogappender已注释掉,如果要编写windows事件日志,可以取消注释。

最小日志级别在<root>元素下定义<level>配置元素。对于调试配置级别是debug,对于生产它是info有效等级是; debuginfowarnerror有关详细信息,请参阅log4net文档

下一步配置步骤是告诉log4net库在调试和释放模式下使用哪个文件。为此,请打开assemblyinfo.cs文件并为log4net添加程序集级别属性。添加这些行以在调试和释放模式下在两个文件之间切换。

#if debug
 [ assembly:log4net.config.xmlconfigurator(configfile = “ log4net.debug.config”,watch = true)]
 #else
 [ assembly:log4net.config.xmlconfigurator(configfile = “ log4net.prod.config”,watch = 真的)]
 #endif

5-添加samplebackgroundservice类,其中包含我们的windows服务将执行的实际后台操作。

它是一个非常简单的类  startstop用于启动和停止后台任务处理线程的方法以及连续写入日志的线程方法。

class samplebackgroundservice
{
    // 从log4net获取此类的记录器logmanager 
    private  static ilog logger = logmanager.getlogger(typeof(samplebackgroundservice));

    // 启动线程
    public  void start(){...}

    // 停止线程
    public  void stop(){...}

    // 执行后台任务并写入日志的服务线程
    private  void servicethread()
    {
       while(!stoprequested)
       {
         // 写出不同类型的日志......
       }
    }
}

6-更新自动生成的windows service类中的代码

我已将自动添加的windows services类重命名为  samplewindowsservice此类继承自servicebase,并且是在启动或停止windows服务时调用方法onstart类的类onstop该类仅创建类的实例  samplebackgroundservice并调用其startstop方法。

7-更新program.cs文件中的main mathod,使其在debug和release模式下表现不同

创建新的windows服务项目时,自动生成的main方法包含用于创建和启动自动生成的windows服务的代码。但是,windows服务无法作为常规应用程序启动和托管。因此,当您尝试运行该应用程序时会出现一条错误消息。

要运行和测试我们的应用程序,我们不需要创建真正的windows服务实例,因为除了创建和启动samplebackgroundservice类的实例之外,它不包含任何代码  main方法中更新的代码在samplebackgroundservicedebug模式下创建并启动的实例,  并作为控制台应用程序运行。但是在发布模式下创建并运行真正的windows服务。

static  void main()
{
    ilog logger = logmanager.getlogger(typeof(program));

#if debug //在调试模式下作为常规控制台应用程序运行
     // 手动创建samplebackgroundservice类的实例并调用其start方法

    logger.info(“正在启动服务......”);

    samplebackgroundservice _backgroundservice = new samplebackgroundservice ();
    _backgroundservice.start();

    logger.info(“ 服务已启动。按enter键停止...”);
    到console.readline();

    logger.info(“ 停止服务......”);
    _backgroundservice.stop();
    logger.info(“已停止。”);

#else //在release模式     servicebase [] servicestorun中创建并运行真正的windows服务实例
 ;
    servicestorun = new servicebase []
    {
        新的 samplewindowsservice()
    };
    servicebase.run(servicestorun);
#endif
 }

8-添加service installer组件,以便能够使用installutil.exe安装此服务

要添加安装程序组件,请在解决方案资源管理器上双击samplewindowsservice.cs它将显示服务的设计视图。

右键单击设计区域,然后单击上下文菜单中的“添加安装程序”。

使用c#调试windows服务模板项目

这会将projectinstaller.cs和设计器文件添加到项目中。删除自动生成的代码  projectinstaller.initializecomponent()方法和自动生成的变量(serviceprocessinstaller1serviceinstaller1)。

将以下代码添加到  projectinstaller.cs文件中;

public  partial  class projectinstaller:installer
{
    public  const  string service_name = “ sample background service” ;

    private  readonly serviceprocessinstaller m_serviceprocessinstaller;
    private  readonly serviceinstaller m_serviceinstaller;

    public projectinstaller()
    {
        // 安装进程的安装程序(在本例中为'debuggablewindowsservice.exe')
        // 只能有一个serviceprocessinstaller 
        m_serviceprocessinstaller = new serviceprocessinstaller();
        m_serviceprocessinstaller.account = serviceaccount.localsystem;

       // 在应用程序中注册实际windows服务实现的安装程序
       // 可能有一个或多个serviceinstaller 
        m_serviceinstaller = new serviceinstaller();
        m_serviceinstaller.servicename = service_name;
        m_serviceinstaller.description = “ ” ;
        m_serviceinstaller.starttype = servicestartmode.automatic;
        m_serviceinstaller.delayedautostart = true ;            

        installers.add(m_serviceprocessinstaller);
        installers.add(m_serviceinstaller);

        的initializecomponent();
    }

// ...

}

如果要在安装服务之前和之后执行任何任务,可以覆盖适当的基本方法,例如  onbeforeinstall,  onbeforeuninstall...

您无需安装(或向windows服务注册表注册服务)您的服务即可运行和调试。但是,如果需要,可以使用installutil.exe来安装和卸载服务。installutil.exe位于.net framework安装文件夹下。例如,“ c:\ windows \ microsoft.net \ framework \ v4.0.30319 ”

要注册该服务,请打开命令行窗口并使用可执行文件的完整路径运行installutil.exe您可能需要“以管理员身份”运行命令行窗口才能注册服务。

c :\ windows \ microsoft.net \ framework \ v4.0.30319> installutil.exe“d:\ debuggablewindowsservice \ src \ bin \ release \ debuggablewindowsservice.exe”

要卸载服务,请使用/ u选项运行相同的命令。

c :\ windows \ microsoft.net \ framework \ v4.0.30319> installutil.exe / u“d:\ debuggablewindowsservice \ src \ bin \ release \ debuggablewindowsservice.exe”

9-在调试模式下运行应用程序以查看日志输出和调试

下面是在调试模式下运行应用程序时显示的示例输出。

使用c#调试windows服务模板项目

关于windows服务的重要说明

windows service应用程序在几个方面与其他常规应用程序不同。因此,应用程序在调试(控制台应用程序模式)和发布(windows服务模式)模式下的行为可能不同。

首先,当您在调试模式下运行应用程序时,其工作目录将是可执行文件所在的路径。例如“ d:\ debuggablewindowsservice \ src \ bin \ release \ debuggablewindowsservice.exe ”

但是,当您使用installutil.exe或安装应用程序安装它并从windows服务管理应用程序运行它时,其工作目录将是“ c:\ windows \ system32 ”或“ c:\ windows \ syswow64 ”,具体取决于您的服务是64位32d无论是在32位还是64位windows上运行。

如果要在安装目录中读取或写入文件而不是系统目录,则可以在启动时使用工作目录。例如;

environment.currentdirectory = path.getdirectoryname(assembly.getexecutingassembly()。location);

其次, windows服务可以使用除登录用户之外的其他用户帐户运行。当应用程序rusn作为真正的windows服务时,某些在调试模式下运行的操作可能无法运行。这些操作的示例是访问目录或网络路径,打开端口......

第三, windows服务没有用户界面,通常无法显示用户界面。windows操作系统阻止了他们对显卡的访问。遗憾的是,无法使用windows服务中的强大gpu。要了解有关此限制的更多信息,请搜索“会话0隔离”。

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网