有很多时候,我们需要创建Windows Service。 这篇文章可以算是一个入门指南吧,希望对初学者有帮助.

要创建Windows Service, 首先选择Windows服务项目,如下图:

这里我想创建一个Windows服务,定时的执行一些任务。

public partial class Service1 : ServiceBase
{
public Service1()
{
InitializeComponent();
} protected override void OnStart(string[] args)
{
} protected override void OnStop()
{
}
}

OnStart :服务启动的时候执行,

OnStop:服务停止的时候执行

为了定时的执行任务,我修改代码如下:

namespace TimeWindowsService
{
public partial class Service1 : ServiceBase
{
System.Timers.Timer timer = null; public Service1()
{
InitializeComponent();
} protected override void OnStart(string[] args)
{
timer = new System.Timers.Timer();
timer.Elapsed += timer_Elapsed;
timer.Interval = 1000;
timer.Start();
} void timer_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
{
Console.WriteLine("Time elapsed");
} protected override void OnStop()
{
if (timer != null)
{
timer.Stop();
}
}
}
}

按F5运行代码,会提示:

这代表我们必须先使用Installutil.exe 来安装服务。

当然你可以打开命令行,然后输入命令来安装服务,不过在这里我打算使用外部工具来执行InstallUtil.exe。

其中命令是:C:\Windows\Microsoft.NET\Framework\v4.0.30319\InstallUtil.exe

点击确定,可以发现一个InstallService 命令。

点击InstallService,可以发现屏幕闪了一下,然后又过了,很明显是因为我们的命令出错了。

注意,我们在添加InstallService的时候,选择的是,所以即使有错误信息,也会一闪而过。

修改InstallService的命令如下:

再次运行InstallService,可以看到:

这代表InstallService 的时候出错了,找不到文件,我们希望它找的是TimeWindowsService.exe,

而不是\bin\debug\TimeWindowsService

所以正确的设置InstallService命令应该是下面这样:

保存,然后运行,结果如下:

还是没有安装成功,因为没有安装程序。

有很多教程会告诉你,需要添加一个像下面这样的类:http://www.cnblogs.com/jjstar/articles/20353.aspx

using System.Collections;
using System.Configuration.Install;
using System.ServiceProcess;
using System.ComponentModel; namespace WindowsService1
{
/// <summary>
/// myInstall 的摘要说明。
/// </summary>
///
[RunInstaller(true)]
public class myInstall : Installer
{ private ServiceInstaller serviceInstaller;
private ServiceProcessInstaller processInstaller;
public myInstall()
{
processInstaller = new ServiceProcessInstaller();
serviceInstaller = new ServiceInstaller(); processInstaller.Account = ServiceAccount.LocalSystem;
serviceInstaller.StartType = ServiceStartMode.Automatic;
serviceInstaller.ServiceName = "WindowsService1"; Installers.Add(serviceInstaller);
Installers.Add(processInstaller);
}
}
}

对于记性不好的人来说,很容易问的一个问题是:VS有提供相关的快捷键吗?,当然,VS提供了。

在Service1.cs 设计页面点击,添加安装程序,vs 就会自动帮你生成一个Installer 的类。

添加的类是:

serviceProcessInstaller1 :

安装一个可执行文件,该文件包含扩展 System.ServiceProcess.ServiceBase 的类。 该类由安装实用工具(如 InstallUtil.exe)在安装服务应用程序时调用。

serviceInstall1:

安装一个类,该类扩展 System.ServiceProcess.ServiceBase 来实现服务。 在安装服务应用程序时由安装实用工具调用该类。

细心的读者看看他们的属性就知道区别在哪里了。

为了简单,将account 设置为LocalSystem.

好了,安装程序已经弄完了,接着上面的InstallService 吧,注意要编译程序啊。

可以看到已经成功的安装服务了。

从任务管理器中,你也可以看到安装的服务:

OK,接着按F5 调试吧,可是:

依然出现了这个提示框,很显然,我们不能按F5 来调试windows服务

有很多文章讲述了如何调试windows服务。

1:比如这篇文章:http://www.cnblogs.com/downmoon/archive/2009/09/16/1567643.html,所采用的AttachProcess 方法。

可是有时候,由于各种各样的原因,服务无法启动,或者是服务由于某些原因而自动停止,所以也就不太容易附加到进程去调试了。

2:还有一些修改代码的方法,比如:

添加一个DebugOnStart 的方法

public void DebugOnStart()
{
this.OnStart(null);
} protected override void OnStart(string[] args)
{
timer = new System.Timers.Timer();
timer.Elapsed += timer_Elapsed;
timer.Interval = 1000;
timer.Start();
}

然后修改main函数如下:

static void Main()
{
//ServiceBase[] ServicesToRun;
//ServicesToRun = new ServiceBase[]
//{
// new Service1()
//};
//ServiceBase.Run(ServicesToRun); new Service1().DebugOnStart();
}

效果如下:

3:在构造函数中用Thread.Sleep. 然后快速的附加到进程。

这样,服务就会需要10秒才能启动,有10秒的时间,肯定可以”附加到进程”的吧

我个人比较喜欢的一种方式:

void timer_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
{
if (!Debugger.IsAttached)
{
Debugger.Launch();
} Console.WriteLine("Time elapsed");
}

这样在启动Service的时候,会提示:

点击确定后:

这种方式是,你可以在任何时间,任何需要调试的地方添加上面的语句。

具体的可以参考Debugger 类。

参考的文章:http://www.codeproject.com/Articles/19914/How-to-debug-a-Windows-Service-and-not-die-in-the

Windows 服务入门指南的更多相关文章

  1. C#创建Windows服务入门图解(VS2010)

    C#创建Windows服务入门图解(VS2010) Windows服务大家都知道,比如Audio.Theme都是大家比较熟悉的服务,他们可以设为自动启动的,并且在注册表的开机自启动项里是没有痕迹的.所 ...

  2. .Net创建windows服务入门

    本文主要记录学习.net 如何创建windows服务. 1.创建一个Windows服务程序 2.新建安装程序 3.修改service文件 代码如下 protected override void On ...

  3. C#开发Windows服务 入门

    Microsoft Windows 服务(即,以前的 NT 服务)使您能够创建在它们自己的 Windows 会话中可长时间运行的可执行应用程序. 服务可以在计算机启动时自动启动,可以暂停和重新启动而且 ...

  4. c#创建windows服务入门教程实例

    用c#中创建一个windows服务非常简单,与windows服务相关的类都在System.ServiceProcess命名空间下. 每个服务都需要继承自ServiceBase类,并重写相应的启动.暂停 ...

  5. 《Office 365开发入门指南》上市说明和读者服务

    写在最开始的话 拙作<Office 365开发入门指南>上周开始已经正式在各大书店.在线商城上市,欢迎对Office 365的开发.生态感兴趣的开发者.项目经理.产品经理参考本书,全面了解 ...

  6. C# Windows服务开发从入门到精通

    一.课程介绍 大家都知道如果想要程序一直运行在windows服务器上,最好是把程序写成windows服务程序:这样程序会随着系统的自动启动而启动,自动关闭而关闭,不需要用户直接登录,直接开机就可以启动 ...

  7. 分布式服务框架 Zookeeper(三)官方入门指南

    入门指南:使用ZooKeeper来协调分布式应用 这篇文档包含了让你快速上手ZooKeeper的信息.主要是针对那些想要试一把ZooKeeper的开发人员,包含了安装一个单一ZooKeeper服务器的 ...

  8. Microsoft Orleans 之 入门指南

    Microsoft Orleans 在.net用简单方法构建高并发.分布式的大型应用程序框架. 原文:http://dotnet.github.io/orleans/ 在线文档:http://dotn ...

  9. 《Gulp 入门指南》 : 使用 gulp 压缩 JS

    <Gulp 入门指南> : 使用 gulp 压缩 JS 请务必理解如下章节后阅读此章节: 安装 Node 和 gulp 访问论坛获取帮助 压缩 js 代码可降低 js 文件大小,提高页面打 ...

随机推荐

  1. nrf51822-添加DFU服务

    以ble_app_uart例子为基础,在其上添加dfu服务. Sdk中的bootloader提供了两个方式来进入升级模式,一种是按键,另一种是手机点击升级. 在bootloader代码相关代码如下 如 ...

  2. FW nexus docker

    原文地址: http://www.cnblogs.com/wzy5223/p/5410990.html Nexus 3.0 可以创建三种docker仓库: 1. docker (proxy)     ...

  3. java中关于集合的知识点梳理

    一:概述 1.集合的特点 只存储对象,集合长度是可变的,集合可以存储不同类型的对象. 2.集合框架     Collection    List | | Set ArrayList    Linked ...

  4. 原因是未找到“sgen.exe”,或未安装 .NET Framework SDK v2.0

    visual studio编译出现错误:错误 2 任务失败,原因是未找到“sgen.exe”,或未安装 .NET Framework SDK v2.0.该任务正在注册表项 HKEY_LOCAL_MAC ...

  5. MVC路由约束

    public static void RegisterRoutes(RouteCollection routes) { routes.IgnoreRoute("{resource}.axd/ ...

  6. Nginx反向代理的模拟

    CentOS起两台tomcat,端口分别是8080和8081! 1. nginx配置文件:nginx.conf upstream tomcats{ server 192.168.198.128:808 ...

  7. Advanced REST client的使用说明

    1.  为什么要使用REST Client 在实际企业开发过程中经常会有这样的需求: 1.我当前开发的这个系统是需要调用其他系统的接口,也就是我们需要频繁的测试接口,尝试不同的入参参数去查看返回结果, ...

  8. toggle笔记

    <!DOCTYPE html> <!-- saved from url=(0040)http://v3.bootcss.com/examples/carousel/ --> & ...

  9. POJ 1028解答

    #include <iostream>#include <cstdio>#include <cmath>#include <stack>#include ...

  10. 视差效果原理 background-attachment:fixed

    <html> <head> <style type="text/css"> body { background-image:url('http: ...