好得很程序员自学网

<tfoot draggable='sEl'></tfoot>

VS2013创建Windows服务与调试服务的图文方法

1、创建windows服务

 

说明:

a)description 服务描述,直接显示到windows服务列表中的描述;

b)displayname 服务显示名称,直接显示到windows服务列表中的名称;

c)servicename 服务进程名称,安装与卸载服务时的唯一标识。

单击[serviceprocessinstaller1],在其属性窗口中设置account帐号方式,建议为localservice(当然也可以account属性改为 localsystem,这样,不论是以哪个用户登录的系统,服务总会启动)。

编写安装和卸载脚本,并将放在bin/debug或bin/release文件夹下。

安装脚本

?

%systemroot%\microsoft.net\framework\v4.0.30319\installutil.exe %~dp0exe程序的名称.exe

net start 服务名称

sc config 服务名称 start= auto

pause

这里注意,在exe程序的名称前面有 %~dp0 这是代表当前位置

服务名称 对应 上面我们创建服务时servername的名称

卸载脚本

?

%systemroot%\microsoft.net\framework\v4.0.30319\installutil.exe /u %~dp0exe程序的名称.exe

pause

同时还要注意一下,本人用的.net4.0的版本,所以\microsoft.net\framework\v4.0.30319\installutil.exe 这一段要根据你机器安装.net的版本来定。

其实脚本主要是通过installutil.exe 来进行安装和卸载服务的,同时此处涉及的批处理命令不多。

2、调试windows服务

在项目中不用启动windows服务项目,而是直接附加进程来进行调试。

 

在可用进程中,查找到你刚才通过脚本安装的服务就可以了。

再发一个写入服务代码的demo

?

public partial class mmsserver : servicebase

   {

     private timer time = new timer();

     public mmsserver()

     {

       initializecomponent();

     }

 

     protected override void onstart( string [] args)

     {

#if debug

       if (!debugger.isattached)

         debugger.launch();

       debugger. break ();

#endif

       writelog( "服务启动,时间:" + datetime.now.tostring( "hh:mm:ss" ) + "\r\n" );

       time.elapsed += new elapsedeventhandler(methodevent);

       time.interval = 3 * 1000;

       time.start();

     }

 

     protected override void onpause()

     {

#if debug

       if (!debugger.isattached)

         debugger.launch();

       debugger. break ();

#endif

       writelog( "服务暂停,时间:" + datetime.now.tostring( "hh:mm:ss" ) + "\r\n" );

       base .onpause();

     }

     protected override void oncontinue()

     {

#if debug

       if (!debugger.isattached)

         debugger.launch();

       debugger. break ();

#endif

       writelog( "服务恢复,时间:" + datetime.now.tostring( "hh:mm:ss" ) + "\r\n" );

       base .oncontinue();

     }

 

     protected override void onshutdown()

     {

       writelog( "计算机关闭,时间:" + datetime.now.tostring( "hh:mm:ss" ) + "\r\n" );

       base .onshutdown();

     }

 

     private void methodevent( object source, system.timers.elapsedeventargs e)

     {

       time.enabled = false ;

       string result = string .empty;

       try

       {

         //.........

         result = "执行成功,时间:" + datetime.now.tostring( "hh:mm:ss" ) + "\r\n" ;

       }

       catch (exception ex)

       {

         result = "执行失败,原因:" + ex.message + "\r\n" ;

       }

       finally

       {

         writelog(result);

         time.enabled = true ;

       }

     }

     protected override void onstop()

     {

#if debug

       if (!debugger.isattached)

         debugger.launch();

       debugger. break ();

#endif

       writelog( "服务停止,时间:" + datetime.now.tostring( "hh:mm:ss" ) + "\r\n" );

     }

     /// <summary>

     /// 日志记录

     /// </summary>

     /// <param name="loginfo"></param>

     private void writelog( string loginfo)

     {

       try

       {

         string logdirectory = appdomain.currentdomain.basedirectory + "\\logs" ;

         if (!directory.exists(logdirectory))

         {

           directory.createdirectory(logdirectory);

         }

         string filepath = logdirectory + "\\" + datetime.now.tostring( "yyyy-mm-dd" ) + ".txt" ;

         file.appendalltext(filepath, loginfo);

       }

       catch

       {

 

       }

     }

   }

以上就是关于vs2013创建windows服务与调试服务的全部内容了,希望大家以后多多支持服务器之家

dy("nrwz");

查看更多关于VS2013创建Windows服务与调试服务的图文方法的详细内容...

  阅读:55次