• Windows Service简介:

一个Windows服务程序是在Windows操作系统下能完成特定功能的可执行的应用程序。Windows服务程序虽然是可执行的,但是它不像一般的可执行文件通过双击就能开始运行了,它必须有特定的启动方式。这些启动方式包括了自动启动和手动启动两种。对于自动启动的Windows服务程序,它们在Windows启动或是重启之后用户登录之前就开始执行了。只要你将相应的Windows服务程序注册到服务控制管理器(Service Control Manager)中,并将其启动类别设为自动启动就行了。而对于手动启动的Windows服务程序,你可以通过命令行工具的NET START 命令来启动它,或是通过控制面板中管理工具下的服务一项来启动相应的Windows服务程序。

同样,一个Windows服务程序也不能像一般的应用程序那样被终止。因为Windows服务程序一般是没有用户界面的,所以你也要通过命令行工具或是下面图中的工具来停止它,或是在系统关闭时使得Windows服务程序自动停止。因为Windows服务程序没有用户界面,所以基于用户界面的API函数对其是没有多大的意义。为了能使一个Windows服务程序能够正常并有效的在系统环境下工作,程序员必须实现一系列的方法来完成其服务功能。Windows服务程序的应用范围很广,典型的Windows服务程序包含了硬件控制、应用程序监视、系统级应用、诊断、报告、Web和文件系统服务等功能。

和Windows服务程序相关的命名空间涉及到以下两个:System.ServiceProcess System.Diagnostics

  • 用C#创建Windows服务的步骤:

1.创建Windows Service项目

从Visual C# 工程中选取 Windows 服务(Windows Service)选项,给工程一个新文件名,然后点击 确定。

2.向服务中函数功能实现

OnStart函数在启动服务时执行,OnStop函数在停止服务时执行。在这里,当启动和停止服务时,向一个文本文件中写入一些文字信息,代码如下:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.ServiceProcess;
using System.Text;
using System.Threading.Tasks;

namespace MyService
{
public partial class Service1 : ServiceBase
{
public Service1()
{
InitializeComponent();
}

protected override void OnStart(string[] args)
{
FileStream fs = new FileStream(@"d:\xx.txt", FileMode.OpenOrCreate, FileAccess.Write);
StreamWriter sw = new StreamWriter(fs);
sw.BaseStream.Seek(0, SeekOrigin.End);
sw.WriteLine("WindowsService: Service Started" + DateTime.Now.ToString() + "\n");

sw.Flush();
sw.Close();
fs.Close();
}

//protected override void OnContinue()
//{
// base.OnContinue();
//}

//protected override void OnPause()
//{
// base.OnPause(); // father class method inherit
//}

//protected override void OnShutdown()
//{
// base.OnShutdown();
//}

protected override void OnStop()
{
FileStream fs = new FileStream(@"d:\xx.txt", FileMode.OpenOrCreate, FileAccess.Write);
StreamWriter sw = new StreamWriter(fs);
sw.BaseStream.Seek(0, SeekOrigin.End);
sw.WriteLine("WindowsService: Service Stopped" + DateTime.Now.ToString() + "\n");
sw.Flush();
sw.Close();
fs.Close();

}
}
}

4.回到设计窗口点右键选择-添加安装程序 -生成serviceInstaller1和 serviceProcessInstaller1两个组件 
把serviceInstaller1的属性ServiceName改写为你的服务程序名,并把启动模 式设置为AUTOMATIC  
把serviceProcessInstaller1的属性account改写为 LocalSystem

5.编译链接生成服务程序

通过从生成菜单中选择生成来生成项目。

6.安装服务

用.net framework工具INSTALLUTIL安装服务程序即可。

用项目的输出作为参数,从命令行运行 InstallUtil.exe。在命令行中输入下列代码: 
installutil yourproject.exe

Hint: a windows service must first be installed using installutil.exe and then started with the serviceExplorer, windows Services Administrative tool or the NET START command.

7.卸载服务

用项目的输出作为参数,从命令行运行 InstallUtil.exe。

installutil /u yourproject.exe

  • 补充:

1.Service启动属性:

Manual      服务安装后,必须手动启动。

Automatic    每次计算机重新启动时,服务都会自动启动。

Disabled     服务无法启动。

2.新建的Service项目,其中各属性的含义(设计视图->右键属性):

  Autolog 是否自动写入系统的日志文件

  CanHandlePowerEvent 服务时候接受电源事件

  CanPauseAndContinue 服务是否接受暂停或继续运行的请求

  CanShutdown 服务是否在运行它的计算机关闭时收到通知,以便能够调用 OnShutDown 过程

  CanStop 服务是否接受停止运行的请求

  ServiceName 服务名

3. 也可以在系统服务管理器中,设置相应Service的属性或启动方式等

计算机管理 -> 服务和应用程序  -> 服务  -> ...

windows Service 创建部署的更多相关文章

  1. windows service创建使用整合

    C#创建Windows Service(Windows 服务)基础教程 C#winform windows服务程序创建与安装 C#实现WinForm随WINDOWS服务一起启动

  2. 使用Windows service创建一个简单的定时器

    一.需求 我们有时候可能会想要做一些定时任务,例如每隔一段时间去访问某个网站,或者下载一些东西到我们服务器上等等之类的事情,这时候windows service 是一个不错的选择. 二.实现 1.打开 ...

  3. Windows Service installutil 部署时,出错的解决办法-原创

    出错信息如下: ---------------------------------------------------------- ~~~~~... The Rollback phase compl ...

  4. C#的简单的Windows Service 创建与安装

    注意事项: 1. 添加调试代码 入口: 服务: 2. 再服务界面右键添加安装程序 3. 修改安装程序属性(Account) 4. 修改服务安装属性(DelayedAutoStart,ServiceNa ...

  5. 创建Windows Service

    基本参照使用C#创建Windows服务,添加了部分内容 目录 创建Windows Service 可视化管理Windows Service 调试 示例代码 创建Windows Service 选择C# ...

  6. 如何彻底的卸载和删除Windows service

    最近遇到很头疼的问题,安装到服务器的Windows Service卸载的时候出错了,结果在服务列表中就一直驻留,并且系统进程一直在运行,怎么都杀不掉. 最后终于找到办法了: 1.常规做法,批处理命令卸 ...

  7. .Net 持续集成 —— windows service

    上一篇讲了 Jenkins+WebDeploy+IIS完成 web项目部署,这篇继续讲windows service的部署. windows service 一般用于自动任务,定时完成某些操作.本文自 ...

  8. C# 创建Windows Service

    当我们需要一个程序长期运行,但是不需要界面显示时可以考虑使用Windows Service来实现.这篇博客将简单介绍一下如何创建一个Windows Service,安装/卸载Windows Servi ...

  9. windows 下部署 .netcore 到 windows service

    接上一篇 <windows 下部署 .netcore 到 iis>,这一篇记录一下怎么将 Asp.Net Core 以 windows 服务的方式部署. 一.修改代码 其实也很简单,只要调 ...

随机推荐

  1. Javase部分回顾(static (静态修饰符),修饰方法,单利的设计模式, 封装)

    static (静态修饰符)        1.修饰成员变量        静态变量的访问方式:            方式一 : 通过对象访问.                对象.变量名      ...

  2. 【SharePoint学习笔记】第3章 SharePoint列表新特性以及数据访问

    第3章 SharePoint列表新特性以及数据访问   使用CAML查询语言     CAML:协作应用程序标记语言 Collaboration Application Markup Language ...

  3. Oracle 备份与还原

    oracle 备份与还原 一.备份数据库(exp) 1.完全备份 exp demo/demo@orcl buffer=1024 file=d:\back.dmp full=y demo:用户名.密码 ...

  4. Android模拟器连接本李服务器localhost

    Android模拟(simulator)把它自己作为了localhost,也就是说,代码中使用 localhost或者127.0.0.1来访问,都是访问模拟器自己! 如果你想在模拟器simulator ...

  5. Quartz 线程处理

    官网 http://www.quartz-scheduler.net/ 相关的 Log 说明 http://netcommon.sourceforge.net/docs/2.1.0/reference ...

  6. VR全景项目外包团队— VR/AR相关领域介绍和VR全景案例

    VR/AR相关领域这里我要说的一点就是硬件.诚然,硬件的确很难搞,国内在这方面就是荒漠,所以,如果你有信心,完全可以开拓一片蓝海.注意我是说真正的硬件,那些把Google的纸盒子拿来改改就能融资千万的 ...

  7. DOJO DOM 功能

    In this tutorial, you'll learn about how to use Dojo to manipulate the DOM in a simple, cross-browse ...

  8. JVM1.6 GC详解

    前言  JVM GC是JVM的内存回收算法,调整JVM GC(Garbage Collection),可以极大的减少由于GC工作,而导致的程序运行中断方面的问题,进而适当的提高Java程序的工作效率. ...

  9. 【学】jQuery的源码思路2——$符号是如何封装的

    jQuery中的$符号功能很强大,原因在于对函数参数的个数以及种类的控制,还有对于面向对象思想的运用 function jQuery(args){ //接受参数,并对其判断 this.elements ...

  10. VirtrualBox使用已存在的镜像创建虚拟机

    再将一个已经存在的虚拟机镜像拷贝为另一个新的虚拟机镜像后,要将该新的镜像添加到新的虚拟机中时会出现错误提示,从而导致不能创建虚拟机.例如有'D:\App\VirtualBox VMs\CentOS_6 ...