windows服务的编写
1.要添加的引用
using System.ServiceProcess;
using System.ServiceModel ;
using WcfServiceLibraryAgain;//自己写的WCF服务的名称
2.写windows服务
public partial class MainService : ServiceBase
{
ServiceHost host;
public MainService()
{
InitializeComponent();
}
protected override void OnStart(string[] args)
{
Type serviceType = typeof(Service1);//托管REST或非REST服务,方式一样。Service1就是要托管的wcf服务的类
host = new ServiceHost(serviceType);
host.Open();
}
}
注意:要将WCF中的配置文件拷贝到windows服务程序的配置文件中。(这点很重要)
手动安装windows服务
1.将Windows服务程序切换到设计视图, 右击设计视图选择“添加安装程序”
![](http://upload-images.jianshu.io/upload_images/9128511-65607b5cfda4a9e3.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/378)
![](http://upload-images.jianshu.io/upload_images/9128511-e89b5dc3f203dc1b.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/700)
2.切换到刚被添加的ProjectInstaller的设计视图
设置serviceInstaller1组件的属性:
1) ServiceName = 服务名称
2) StartType = Automatic ,即自动,Manual是手动,剩下那个是禁用,不过这都不重要,安装好后再服务管理器中可以很方便的设置。
设置serviceProcessInstaller1组件的属性
1) Account = LocalSystem,账户一般设置为本地系统
DisPlayName中设置的名字即你打开服务管理器中服务的名称,我这里叫MyService1
然后重新生成一下
3.右键点击开始按钮,选择命令提示符(管理员)(A),也就是以管理员身份运行cmd,按如下路径找到对应安装服务的程序C-windows-Microsoft.NET-Framework-v4.0.30319-InstallUtil.exe,在cmd中输入 cd C:WindowsMicrosoft.NETFrameworkv4.0.30319(即InstallUtil.exe的路径,在其属性中拷贝,必需右键粘贴),回车运行(注意cd 后要留空格)
![](http://upload-images.jianshu.io/upload_images/9128511-957efe88323841bc.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/597)
右键解决方案,选择在资源管理器中打开文件夹,找到要安装的windows服务的exe文件,WindowService1-bin-Debug,拷贝其路劲,然后再cmd中继续输入命令InstallUtil.exe+空格+路径(注意路径要完整,Debug后还要加上名称.exe),然后回车运行完成安装,关键截图如下:
![](http://upload-images.jianshu.io/upload_images/9128511-2e019189aaeb0c5d.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/636)
![](http://upload-images.jianshu.io/upload_images/9128511-b8fff09784a36994.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/609)
![](http://upload-images.jianshu.io/upload_images/9128511-ebd6d98493a8651f.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/418)
![](http://upload-images.jianshu.io/upload_images/9128511-25bbb1af6d8316c5.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/648)
至此服务已安装成功!打开服务管理器查看服务是否存在。
![](http://upload-images.jianshu.io/upload_images/9128511-0c30320e5e51b4db.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/638)
手动卸载windows服务
如果觉得写的服务有问题或者是其他原因想卸载服务,在cmd中与安装是几乎相同的,第一步完全以样,第二步的命令变为InstallUtil.exe +空格+ –u +路径(注意路径要完整,Debug后还要加上名称.exe)
![](http://upload-images.jianshu.io/upload_images/9128511-792f164542e4fa96.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/635)
补充:还可以用VS自带的工具完成服务的安装与下载,这时不需要做cmd中的第一步,直接到第二步就可以了,要方便一点,卸载时该用命令u,下面详细说一下用VS自带的工具怎么卸载服务:
![](http://upload-images.jianshu.io/upload_images/9128511-d5523ecf5504e2bb.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/700)
![](http://upload-images.jianshu.io/upload_images/9128511-36105439bf49c88c.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/601)
这时要注意还是要以管理员身份运行
![](http://upload-images.jianshu.io/upload_images/9128511-e00c9c1ed3bff7e0.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/670)
安装几乎相同,就不用说了!
总结:手动安装是服务安装最老旧的方法,我们不能一直用这个方法,但是学会了手动安装会让我们对服务的装原理更了解一些,对写自动安装与卸载还是有帮助的,希望对大家有所帮助!