当前位置: 移动技术网 > IT编程>开发语言>c# > 用.NET创建Windows服务的方法第1/2页

用.NET创建Windows服务的方法第1/2页

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

安装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
   }

   #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服务属性来设置。


about the author
mark strawmyer, mcsd, mcse (nt4/w2k), mcdba is a senior architect of .net applications for large and mid-size organizations. mark is a technology leader with crowe chizek in indianapolis, indiana. he specializes in architecture, design and development of microsoft-based solutions. you can reach mark at . 

2

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网