经常看到一些人在调试 Windows 服务时,很执著的在附加进程后调试!
其实 .Net 编写的 Windows 应用程序,包括 Windows 服务都可以编译成 Console 程序!
甚至于 ASP.Net ASPX 的 codebehind 里加个 Main 函数,编译成 Console 也未尝不可!
万事万物皆Console!(学自《thinking in java》万事万物皆对象!有点牵强,表笑偶)
利用 Visual Studio .Net 2003 创建的 "Windows 服务" 项目默认不是 Console,
你可以在项目属性中:
把 "通用属性->常规->输出类型->应用程序" 强行指定为 "控制台应用程序"
把 "配置属性->调试->启动选项->命令行参数" 指定为任意字符串,如: "/cxxx"

然后将生成的 C# 服务代码,如: Service.cs 的 Main 函数改为如下代码

static void Main(string[] args )
{
    Service1 x = new Service1();
    if (args.Length > 0)
    {
        Console.WriteLine("Console");
        x.OnStart(null);
        Console.ReadLine();
    }
    else
    {
        System.ServiceProcess.ServiceBase[] ServicesToRun;
        // 同一进程中可以运行多个用户服务。若要将
        //另一个服务添加到此进程,请更改下行
        // 以创建另一个服务对象。例如,
        //
        //   ServicesToRun = New System.ServiceProcess.ServiceBase[] {new Service1(), new MySecondUserService()};
        //
        ServicesToRun = new System.ServiceProcess.ServiceBase[] { x};
        System.ServiceProcess.ServiceBase.Run(ServicesToRun);
    }
}

接下来就可以 F5 运行了,如果有客户端调用,且你设了断点,自然就可以断点调试了!
这个程序即可以服务方式运行,也可以在运行时指定命令行参数以 Console 运行!
绝不不影响 InstallUtil 部署该服务!
我在 程序代码 里经常加些 System.Console.WriteLine 的提示信息!
再多说几句:
Visual Studio 生成的服务程序代码比较多,并不是所有的代码都是必要的!
其实实现一个最简单的 Windows 服务,用不了太多的代码,
Service 的 Installer 的最关键代码是 ServiceName 要一致!
下面就是一个 SimpleService 的简单例子

namespace Microshaoft
{
    using System;
    using System.ServiceProcess;
    using System.ComponentModel;
    using System.Security.Principal;
    using System.Configuration.Install;

    public class SimpleService : ServiceBase //继承于 ServiceBase
    {
        public static readonly string serviceName = "Hello-World Service1";

        public static void Main(string[] args)
        {
            SimpleService x = new SimpleService();
            int l = 0;
            if (args != null)
            {
                l = args.Length;
            }

            if (l > 0) //有参数时以 console 方式运行
            {
                Console.WriteLine("Run as Console");
                x.OnStart(null);
                Console.ReadLine();
            }
            else 
            //intallutil 成服务后
            //即: 无参数时,以 Service 方式运行
            {
                Console.WriteLine("Run as Service");
                ServiceBase.Run(x);
            }
        }
        public SimpleService()
        {
            CanPauseAndContinue = true;
            ServiceName = SimpleService.serviceName;
        }

        protected override void OnStart(string[] args)
        {

            Console.WriteLine(".Net Version: {0}", Environment.Version.ToString());
            Console.WriteLine("Current Identity: {0}", WindowsIdentity.GetCurrent().Name);
            Console.WriteLine("{0} started,at {1}", SimpleService.serviceName, DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.ss"));

            //log 写入 windows 应用程序日志
            EventLog.WriteEntry(string.Format(".Net Version: {0}", Environment.Version.ToString()));
            EventLog.WriteEntry(string.Format("Current Identity: {0}", WindowsIdentity.GetCurrent().Name));
            EventLog.WriteEntry(string.Format("{0} started, at {1}", SimpleService.serviceName, DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.ss")));

            //在这里写你的程序
        }

///        protected override void OnStop()
///        {
///            EventLog.WriteEntry("Hello-World Service stopped");
///        }
///
///        protected override void OnPause()
///        {
///            EventLog.WriteEntry("Hello-World Service paused");
///        }
///
///        protected override void OnContinue()
///        {
///            EventLog.WriteEntry("Hello-World Service continued");
///        }
    }

    //以下就是比普通应用程序多出的 ProjectInstaller
    [RunInstallerAttribute(true)]
    public class ProjectInstaller: Installer
    {

        private ServiceInstaller serviceInstaller;
        private ServiceProcessInstaller processInstaller;

        public ProjectInstaller(){

            processInstaller = new ServiceProcessInstaller();
            serviceInstaller = new ServiceInstaller();

            // Service will run under system account
            processInstaller.Account = ServiceAccount.LocalSystem;

            // Service will have Start Type of Manual
            serviceInstaller.StartType = ServiceStartMode.Manual;

            serviceInstaller.ServiceName = SimpleService.serviceName;

            Installers.Add(serviceInstaller);
            Installers.Add(processInstaller);
        }
    }
}

请用命令行编译:
csc service1.cs
编译生成 service1.exe ,其实就是一个 Console!
如果编译时加上 /debug 还可以结合 DbgCLR.exe 工具进行断点调试!
用命令行:
InstallUtil.exe service1.exe
安装成服务!
或者在 cmd 命令行状态,运行如下命令行:
service1.exe /xxx
就可以 Console 运行该 "服务"!
如果你有 System.Console.WriteLine 的提示信息,看着多爽!
有些时候 "服务" 方式启动不了时,用 Console 运行还可以很轻易的发现错误!

我个人现在工作中,基本不写大量代码!
因此基本不用 Visual Studio,很少写 WinForm、WebForm,只写 Console!
只是使用 EditPlus + SDK + DbgCLR 编写、调试一些功能简单的测试代码!
接下来吐血●●●推荐下载我强大的 EditPlus:
http://microshaoft.googlepages.com/EditPlus.v2.21.b381.zip
注意: 解压后请复制到你的D盘根目录,即目录为 D:/EditPlus
如果你想放在其他盘或目录下,请打开 EditPlus 目录下的一些 INI 文件,并替换路径,保存!
可以编写、编译:
C#、Java、C/C++ 等
并集成了一些有用的 .Net 命令行工具,如: wsdl.exe、installutil、regasm、tlbimp 等
(如果命令行工具的路径不对,请自行打开 tool.ini 文件替换你自己的相应路径)
收集了一些有用的代码片断,
欢迎使用,提意见!

以 Console 方式运行、调试、编译 .Net 编写的 Windows 服务的更多相关文章

  1. 【JavaService】使用Java编写部署windows服务

    如果你玩windows系统,你对服务这个东西并不会陌生,服务可以帮我们做很多事情,在不影响用户正常工作的情况下,可以完成很多我们需要的需求. 众所周知,微软的visio studio内置的Servic ...

  2. C#编写的windows服务安装后启动提示“服务启动后又停止了”

    使用C#编写的windows服务安装到服务器上行后进行启动时,总是提示“服务启动后又停止了”. 检查了服务逻辑是没问题,安装在开发本地也是正常,网上查了资料说是可能是服务没有注册,我检查了服务是正常注 ...

  3. 编写C# Windows服务,用于杀死Zsd.exe进程

    最近经常在我的xp系统进程中出现Zsd.exe进程.刚开始他占用内存不是很大.但是过了一段时间就会变成几百M 机器就会变得很卡,网上说Zsd可能是病毒.所以我就想要不写一个Windows服务,让他每隔 ...

  4. .net Windows服务程序和安装程序制作图解 及 VS 2010创建、安装、调试 windows服务(windows service)

    .net Windows服务程序和安装程序制作 最近项目中用到window服务程序,以前没接触过,比较陌生,花了两天的时间学习了下,写了个简单的服务,但在制作安装程序的时候,参照网上很多资料,却都制作 ...

  5. C# Windows服务的创建、安装、调试

    一.查看已有的Windows服务 选择菜单"开始"-〉"控制面板"-〉"管理工具"-〉"服务"来查看现有系统中的服务 二 ...

  6. windows服务的创建、安装和调试

    1.创建 windows服务 项目   文件 -> 新建项目 -> 已安装的模板 -> Visual C# -> windows ,在右侧窗口选择"windows 服 ...

  7. VS创建、安装、调试 windows服务(windows service)

    1.创建 windows服务 项目   文件 -> 新建项目 -> 已安装的模板 -> Visual C# -> windows ,在右侧窗口选择"windows 服 ...

  8. C# VS 2010创建、安装、调试 windows服务(windows service)

    在一个应用程序中创建多个 windows 服务的方法和 1083 的解决办法 错误解决方案 ------------------------------------------------------ ...

  9. 手把手教用C#编写Windows服务 并控制服务 安装、启动、停止、卸载

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

随机推荐

  1. 【无私分享:ASP.NET CORE 项目实战(第八章)】读取配置文件(二) 读取自定义配置文件

    目录索引 [无私分享:ASP.NET CORE 项目实战]目录索引 简介 我们在 读取配置文件(一) appsettings.json 中介绍了,如何读取appsettings.json. 但随之产生 ...

  2. Android 手机卫士7--黑名单拦截

    1,黑名单数据库创建 三个字段(_id 自增长字段 phone 黑名单号码 mode 拦截类型) 创建表的sql语句 create table blacknumber (_id integer pri ...

  3. glibc 各版本发布时间以及内核默认glibc版本

    最近有些软件要求glibc 2.14+,centos 6.x自带的版本是2.12的,特查了下glibc 各版本发布时间以及与对应的内核,如下: Complete glibc release histo ...

  4. Oracle安装

    1.根据自己的操作系统,到Oracle官网下载相应的安装包 下载地址:http://download.oracle.com/otn/nt/oracle11g/112010/win32_11gR2_cl ...

  5. 使用jquery.qrcode生成二维码(转)

    jQuery 的 qrcode 插件就可以在浏览器端生成二维码图片. 这个插件的使用非常简单: 1.首先在页面中加入jquery库文件和qrcode插件. <script type=" ...

  6. observejs改善组件编程体验

    传送门 observejs:https://github.com/kmdjs/observejs 本文演示:http://kmdjs.github.io/observejs/list/ 本文代码:ht ...

  7. Atitit 在线支付系统功能设计原理与解决方案 与目录

    Atitit 在线支付系统功能设计原理与解决方案 与目录 1.1. 支付系统1 1.2. 独立的支付子体系..微服务架构..1 1.3. 参考书籍1 支付战争 [The PayPal Wars:Bat ...

  8. Android中使用Notification实现宽视图通知栏(Notification示例二)

    Notification是在你的应用常规界面之外展示的消息.当app让系统发送一个消息的时候,消息首先以图表的形式显示在通知栏.要查看消息的详情需要进入通知抽屉(notificationdrawer) ...

  9. json相关类库,java对象与json相互转换

    有效选择七个关于Java的JSON开源类库 转自:http://www.open-open.com/lib/view/open1397870197828.html 翻译: (英语原文:http://w ...

  10. Action.c(58): Error -27796: Failed to connect to server "hostname"

    分析: 因为负载生成器的性能太好发数据特别快,服务器响应也特别快,从而导致负载生成器的端口在没有timeout之前就全部占满了. 解决方案一:   在负载生成器的注册表HKEY_LOCAL_MACHI ...