转载-https://www.cnblogs.com/yubao/p/8443455.html

Create Windows Service project using Visual Studio

Create files on starting and stopping service:

   using System.IO;

   protected override void OnStart(string[] args)
{
File.Create(AppDomain.CurrentDomain.BaseDirectory + "OnStart.txt");
} protected override void OnStop()
{
File.Create(AppDomain.CurrentDomain.BaseDirectory + "OnStop.txt");
}

Try to run Windows Program on Debug mode

Click "Start" directly:

How to create a service use exe file

C:\Windows\system32>sc create -h
DESCRIPTION:
Creates a service entry in the registry and Service Database.
USAGE:
sc <server> create [service name] [binPath= ] <option1> <option2>... OPTIONS:
NOTE: The option name includes the equal sign.
A space is required between the equal sign and the value.
type= <own|share|interact|kernel|filesys|rec|userown|usershare>
(default = own)
start= <boot|system|auto|demand|disabled|delayed-auto>
(default = demand)
error= <normal|severe|critical|ignore>
(default = normal)
binPath= <BinaryPathName to the .exe file>
group= <LoadOrderGroup>
tag= <yes|no>
depend= <Dependencies(separated by / (forward slash))>
obj= <AccountName|ObjectName>
(default = LocalSystem)
DisplayName= <display name>
password= <password>

Start service use "sc create" command

C:\Windows\system32>sc create MyService binpath="C:\Users\yubao\Documents\Visual Studio 2017\Projects\WindowsService1\WindowsService1\bin\Debug\WindowsService1.exe" start=auto
[SC] CreateService SUCCESS

Start service:

Delete Service use "sc delete" command:

C:\Windows\system32>sc delete MyService
[SC] DeleteService SUCCESS

Install Service on Windows

Install Service to Windows use InstallUtil.exe:

Where InstallUtil.exe is:

for win32: C:\Windows\Microsoft.NET\Framework\v4.0.30319

for win64: C:\Windows\Microsoft.NET\Framework64\v4.0.30319

Install Service:

C:\Windows\system32>InstallUtil.exe C:\Users\yubao\Documents\Visual Studio 2017\Projects\WindowsService1\WindowsService1\bin\Debug\WindowsService1.exe

Uninstall Service:

C:\Windows\system32>InstallUtil.exe /u  "C:\Users\yubao\Documents\Visual Studio 2017\Projects\WindowsService1\WindowsService1\bin\x64\Release\WindowsService1.exe"

Use Setup Project to Install windows service

Build Setup Project to generate Installer files:

Double click "Setup.exe/msi" to start installation. And then you will see this typicall error for x64 target platform: ( for win32, you will never see this error)

How to solve E1001 under x64 target system:

Download orca.exe from ORCA MSI Editor Standalone Download

Orca MSI Editor allows you to edit the properties of any MSI file. With this tool, you can change the title and text within the installer an look at how and where the files are delivered. It’s also handy if you need to “hack” an MSI to work with a newer version of Windows.

Edit .msi file with "orca.exe " tool:

modify binary path to  "C:\Windows\Microsoft.NET\Framework64\v4.0.30319\InstallUtilLib.dll"

Final install path:

How to Debug Windows Service program on Visual Studio:

Service1.cs

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Linq;
using System.ServiceProcess;
using System.Text;
using System.Threading.Tasks;
using System.IO; namespace WindowsService1
{
public partial class Service1 : ServiceBase
{
public Service1()
{
InitializeComponent();
} public void OnDebug()
{
string[] str = { "0" };
OnStart(str);
} protected override void OnStart(string[] args)
{
File.Create(AppDomain.CurrentDomain.BaseDirectory + "OnStart.txt");
} protected override void OnStop()
{
File.Create(AppDomain.CurrentDomain.BaseDirectory + "OnStop.txt");
} }
}

Program.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.ServiceProcess;
using System.Text;
using System.Threading.Tasks; namespace WindowsService1
{
static class Program
{
/// <summary>
/// The main entry point for the application.
/// </summary>
static void Main()
{
#if DEBUG
Console.WriteLine("This is debug mode");
Service1 myService = new Service1();
myService.OnDebug();
#else
ServiceBase[] ServicesToRun;
ServicesToRun = new ServiceBase[]
{
new Service1()
};
ServiceBase.Run(ServicesToRun);
#endif }
}
}

How to make service auto start after installation:

ProjectInstaller.cs


using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Configuration.Install;
using System.Linq;


using System.ServiceProcess;
using System.Threading.Tasks;


namespace WindowsService1
{
[RunInstaller(true)]
public partial class ProjectInstaller : System.Configuration.Install.Installer
{
public ProjectInstaller()
{
InitializeComponent();
}


private void serviceInstaller1_AfterInstall(object sender, InstallEventArgs e)
{
new ServiceController(serviceInstaller1.ServiceName).Start();
}
}
}

 

使用C#编程语言开发Windows Service服务的更多相关文章

  1. 震惊!Windows Service服务和定时任务框架quartz之间原来是这种关系……

    过场CG:   接到公司领导的文件指示,“小熊”需要在6月底去海外执行一个行动代号为[定时任务]的营救计划,这个计划关系到公司某个项目的生死(数据安全漏洞),作战部拟定两个作战方案: 方案一:使用务定 ...

  2. .NET开发Windows Service程序 - Topshelf

    在实际项目开发过程中,会经常写一些类似定时检查,应用监控的应用.这类应用在windows平台通常都会写成window service程序. 在百度上搜索一下'c#开发windows service', ...

  3. C# Windows Service服务的创建和调试

    前言 关于Windows服务创建和调试的文章在网络上的很多文章里面都有,直接拿过来贴在这里也不过仅仅是个记录,不会让人加深印象.所以本着能够更深刻了解服务项目的创建和调试过程及方法的目的,有了这篇记录 ...

  4. quartz.net结合Topshelf实现windows service服务托管的作业调度框架

    topshelf可以很简单方便的实现windows service服务,详见我的一篇博客的介绍 http://www.cnblogs.com/xiaopotian/articles/5428361.h ...

  5. .Net Windows Service(服务) 调试安装及System.Timers.Timer 使用

    Windows Service(服务)  是运行在后台的进程 1.VS建立 Windows 服务(.NET Framework) 2.添加Timer 双击Service1.cs可以拖控件(System ...

  6. ubuntu下安装 gSOAP 用于C/C++开发web service服务端与客户端

    昨天在ubuntu下进行安装gSOAP,费了很多时间,没成功,今天又来找了大量教程资料,终于一次成功,这里写下自己的安装步骤和方法,供大家参考. 首先下载gsoap,我下载的是gsoap-2.8.1. ...

  7. Windows Service 服务搭配FluentScheduler实现定时任务调度

    Windows Service 服务 创建Windows Service 项目 创建一个Windows Service项目,并将项目名称改为 TaskWindowService 在解决方案资源管理器内 ...

  8. 使用CXF开发Web Service服务

    1.使用CXF开发Web Service服务端 1.1 开发一个Web Service业务接口,该接口要用@WebService修饰 (1)创建一个Java项目MyServer (2)在MyServe ...

  9. [开发笔记]-Windows Service服务相关注意事项

    注意一:报错:“本地计算机上的 *** 服务启动后停止.某些服务在未由其他服务或程序使用时将自动停止.” 该问题主要的原因是 Service服务程序中有错误. 遇到这个问题时,无论是重新安装服务,还是 ...

随机推荐

  1. cookie和localstorge、sessionStorge的区别

    一.背景由来 cookie原来是用来网络请求携带用户信息的,只不过在HTML5出现之前,前端没有本地存储的方法,只能使用cookie代替 localstorge.sessionStorge是html5 ...

  2. 简单几步解决ie打不开闪退的问题 亲测有效

    起因: 银行U盾插入 IE自动打开银行门户网站 打不开 闪退 不插入之后 IE还是闪退, 修复之法 清除IE扩展 一些自己安装的扩展或是被恶意安装的扩展插件会导致IE无法启动 1. 按住windows ...

  3. 关于数据拓展及面试题讲解 Java

    强类型语言  要求变量的使用严格符合规定,所有变量都必须先定义后才能使用 弱类型语言 Java 的数控类型分为两大类 基本类型(primitive type) 引用类型(reference type) ...

  4. VMware 虚拟机图文安装和配置 AlmaLinux OS 8.6 教程

    前言: 这是<VMware 虚拟机图文安装和配置 Rocky Linux 8.5 教程>一文的姐妹篇教程,如果你需要阅读它,请点击这里. 2020 年,CentOS 宣布:计划未来将重心从 ...

  5. redis 2 主从和哨兵

    主从: 概念:将一台redis服务器数据复制到其他redis服务器,前者是master,后者是slave.数据复制是单向,从主节点复制到从节点.master以写为主,slave以读为主一个zhu主节点 ...

  6. 【数据库】MySQL如何删除索引

    1.查看表上的索引 -- 查看table_name表上的索引 show index from table_name ; 2.删除表上的索引 删除索引可以使用ALTER TABLE或DROP INDEX ...

  7. 1.还不会部署高可用的kubernetes集群?看我手把手教你使用二进制部署v1.23.6的K8S集群实践(上)

    公众号关注「WeiyiGeek」 设为「特别关注」,每天带你玩转网络安全运维.应用开发.物联网IOT学习! 本章目录: 0x00 前言简述 0x01 环境准备 主机规划 软件版本 网络规划 0x02 ...

  8. 【万字长文】使用 LSM Tree 思想实现一个 KV 数据库

    目录 设计思路 何为 LSM-Treee 参考资料 整体结构 内存表 WAL SSTable 的结构 SSTable 元素和索引的结构 SSTable Tree 内存中的 SSTable 数据查找过程 ...

  9. el-form 中的数组表单验证(数组可动态添加删除)

    除了一些简单的表单验证之外,我们还会有一些稍微复杂点的多层级表单的验证,如下图所示可点击添加,删除对数组进行操作,当点击确定时需要验证每一条form-item不能为空 其tempalte部分主要代码如 ...

  10. 初识 Redis 以及其基本使用方法

     1.什么是Redis  redis 是一个高性能的key-value数据库,它支持的类型更多 包括 string(字符串).list(链表).set(集合).zset(sorted set --有序 ...