Console to run, debug, compile written in .Net Windows Service

Often see some people when debugging Windows service, very persistent debugging after additional process!
In fact, Windows applications written in .Net, including Windows services can be compiled into the Console program!
Even ASP.Net ASPX the codebehind Riga Main function, compiled Console also not a bad idea!
all things are Console! ( learn from "thinking in java" all things are subject! a bit far-fetched, even laughing table)
using the "Windows service" project Visual Studio .Net 2003 created a default is not the Console ,
you can in the project properties:
the "General properties -> General -> output type -> applications" forcibly designated as "console application"
to "configuration properties -> debug -> start options -> command line parameters" specified as an arbitrary string, such as: "/ cxxx"

And then the resultant C # service code, such as: Service.cs the following code to the Main function

the Main void static (String [] args)
{
    Servicel new new Servicel X = ();
    IF (args.length> 0)
    {
        Console.WriteLine ( "Console");
        x.OnStart (null);
        Console.ReadLine ();
    }
    the else
    {
        System.ServiceProcess.ServiceBase [] ServicesToRun;
        // same process can run multiple users. To
        // add another service to this process, change the downward
        // to create another service object. For example,
        //
        // New System.ServiceProcess.ServiceBase ServicesToRun = [] {new new Servicel (), new new MySecondUserService ()};
        //
        ServicesToRun new new System.ServiceProcess.ServiceBase = []  {X};
        System.ServiceProcess.ServiceBase.Run(ServicesToRun);
    }
}

Then you can run the F5, if a client calls, and you set a breakpoint, the debugger breakpoints can naturally!
This program which can run the service mode, you can also specify command-line parameters to run Console at runtime!
Absolutely ! InstallUtil does not affect the deployment of the service
I often add some tips System.Console.WriteLine in the program code!
say a few more:
Visual Studio generates the program code more services, not all of the code are necessary !
in fact, to achieve a simple Windows service, does not take too much code,
most of the key code Installer service ServiceName is to be consistent!
simple example below is of a 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 void the override OnStop ()        }

            // here to write your program




///            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 文件替换你自己的相应路径)
收集了一些有用的代码片断,
欢迎使用,提意见!

转载于:https://www.cnblogs.com/zhangchenliang/p/5165964.html

Guess you like

Origin blog.csdn.net/weixin_34344403/article/details/93495563