VS2010 创建WindowsService服务

新建一个Windows 服务

2.添加Installer

这一步很重要,在处理完你的业务逻辑后需要添加一个Installer才能是你的Windows服务被安装。

在VS中添加Installer

右键你的Service服务,选择View Designer

再在View Designer视图中右键Add Installer

这样Installer就添加好了。

3.设置服务参数

在添加Installer时会自动生成一个ProjectInstaller.cs,在这个文件中有个InitializeComponent方法,如下

[csharp]  view plain copy
  1. private void InitializeComponent()  
  2.   
  3.   {  
  4.   
  5.   this.serviceProcessInstaller1 = new System.ServiceProcess.ServiceProcessInstaller();  
  6.   
  7.   this.serviceInstaller1 = new System.ServiceProcess.ServiceInstaller();  
  8.   
  9.   //  
  10.   
  11.   // serviceProcessInstaller1  
  12.   
  13.   //  
  14.   
  15.   this.serviceProcessInstaller1.Password = null;  
  16.   
  17.   this.serviceProcessInstaller1.Username = null;  
  18.   
  19.   //  
  20.   
  21.   // serviceInstaller1  
  22.   
  23.   //  
  24.   
  25.   this.serviceInstaller1.ServiceName = "Service1";  
  26.   
  27.   //  
  28.   
  29.   // ProjectInstaller  
  30.   
  31.   //  
  32.   
  33.   this.Installers.AddRange(new System.Configuration.Install.Installer[] {  
  34.   
  35.   this.serviceProcessInstaller1,  
  36.   
  37.   this.serviceInstaller1});  
  38.   
  39.   }  
  40.   
  41.   
  42.   
  43.   this.serviceProcessInstaller1.Password = null;  
  44.   
  45.   this.serviceProcessInstaller1.Username = null;  


 


  为设置服务的登陆账号密码,如果不想设置用户名密码也可以采用本地系统帐户运行服务,代码如下:

  



  this.serviceProcessInstaller1.Account = System.ServiceProcess.ServiceAccount.LocalSystem;


  其他设置也可在此完成。
4.安装和卸载Windows服务

制作完成的Windows服务发布后是一个exe文件,要想在使用的机器上启用这个服务,我们要用微软提供的工具Installutil工具,利用命令行安装和卸载此服务。

installutil工具在目录:系统盘:\WINDOWS\Microsoft.NET\Framework\v4.0.30319下,运行cmd,输入

  C:\WINDOWS\Microsoft.NET\Framework\v4.0.30319\installutil xxxx.exe 回车,即可完成windows服务的安装。

  卸载则为输入 C:\WINDOWS\Microsoft.NET\Framework\v4.0.30319\installutil /u xxxx.exe 回车。

5.安装之后运行windowservice服务时有时可能会遇到service会自动down掉得情况,这种情况有肯能是编译时的exe文件是x64或者x86的情况,所以我们用mubuild手动build成anycpu的exe服务,可能会得到解决。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值