在传统的Windows服务开发过程中,需要添加一个服务安装程序,里面写安装,启动和停止服务等逻辑。现在,使用TopSelf可以简化这个过程。具体请看官网说明:

http://docs.topshelf-project.com/en/latest/index.html

开始前,先创建一个控制台程序,添加 Topshelf 程序包引用:

Install-Package Topshelf

首先,继承 ServiceControl 写一个应用服务类:

 internal class AppService : ServiceControl
{
ILog log = LogManager.GetLogger(typeof(AppService)); public void Initialize()
{
log.Info("Initialize");
Console.WriteLine("Engine starting....");
EngineContext.Initialize(false); Console.WriteLine("Engine started");
} public bool Start(HostControl hostControl)
{
Console.WriteLine("Hosts starting....");
ServiceHostHelper.OpenWcfService(); Console.WriteLine("Hosts started");
return true;
} public bool Stop(HostControl hostControl)
{
ServiceHostHelper.CloseWcfService();
return true;
}
}

然后,在主程序里面,写如下代码:

static void Main(string[] args)
{
try
{
HostFactory.Run(x =>
{
x.RunAsLocalSystem();
x.StartAutomatically();
//x.StartManually();
var defaultName = GetDefaultServiceName();
var config = DXNConfig.Current.AppServer;
x.SetServiceName("我的服务1");
x.SetDisplayName("我的服务1");
x.SetDescription("我的服务1描述"); x.Service(factory =>
{
var service = new AppService();
service.Initialize();
return service;
});
});
}
catch (Exception ex)
{
try
{
LogManager.GetLogger(typeof(Program)).Fatal("AppServer launch error", ex);
}
catch
{
}
//throw;
} }
}

这些服务名称性信息是可以修改的。
好了,现在编译,我们的程序就可以当服务使用了。

之后,就可以用命令行来安装服务:

@echo off

%~d0
cd %~dp0 cd.. rem ApplicationServer.exe install -servicename "ApplicationServer" -displayname "ApplicationServer" -description "XX业务管理系统"
ApplicationServer.exe install
@echo 启动服务...
ApplicationServer.exe start
echo 启动结束

启动服务:

sc start "ApplicationServer"

停止服务:

sc stop "ApplicationServer"

卸载服务:

ApplicationServer.exe uninstall

运行第一个启动服务的批处理文件,将输出下面的内容:

Configuration Result:
[Success] Name DEV_DXN_ApplicationServer
[Success] DisplayName ApplicationServer
[Success] Description XXX业务管理系统
[Success] ServiceName ApplicationServer
Topshelf v3.0.105.0, .NET Framework v4.0.30319.34014 正在运行事务处理安装。 正在开始安装的“安装”阶段。
Installing DXN ApplicationServer service
正在安装服务 DXN_ApplicationServer...
已成功安装服务DXN_ApplicationServer。
正在日志 Application 中创建 EventLog 源 ApplicationServer... “安装”阶段已成功完成,正在开始“提交”阶段。 “提交”阶段已成功完成。 已完成事务处理安装。 启动服务...
Configuration Result:
[Success] Name DXN_ApplicationServer
[Success] DisplayName ApplicationServer
[Success] Description XX管理系统
[Success] ServiceName ApplicationServer
Topshelf v3.0.105.0, .NET Framework v4.0.30319.34014
The ApplicationServer service is not installed.

过程全部完成。

使用TopSelf创建自宿主的Windows服务程序的更多相关文章

  1. 使用Topshelf创建自宿主的Windows服务程序

    在传统的Windows服务开发过程中,需要添加一个服务安装程序,里面写安装,启动和停止服务等逻辑.现在,使用TopSelf可以简化这个过程.具体请看官网说明: http://docs.topshelf ...

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

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

  3. 创建一个Windows服务程序与实现定时器效果

    1.创建一个Windows服务程序 一.   新建Window服务项目 二.   添加安装程序 三.   配置服务属性 四.   编写定时器代码 publicpartialclassService1 ...

  4. C#/.NET基于Topshelf创建Windows服务程序及服务的安装和卸载(极速,简洁)

    本文首发于:码友网--一个专注.NET/.NET Core开发的编程爱好者社区. 文章目录 C#/.NET基于Topshelf创建Windows服务的系列文章目录: C#/.NET基于Topshelf ...

  5. 用Visual C#创建Windows服务程序

    一.Windows服务介绍: Windows服务以前被称作NT服务,是一些运行在Windows NT.Windows 2000和Windows XP等操作系统下用户环境以外的程序.在以前,编写Wind ...

  6. 用C/C++创建windows服务程序

    转载:https://blog.csdn.net/chenyujing1234/article/details/8023816 一.演示过程下方代码演示了如何使用vs(C/C++)创建windows服 ...

  7. C# 创建、安装和卸载Windows服务程序

    1.新建一个windows服务程序. 2.点击这个服务类,从工具箱中加入一个Timer控件,右键这个Timer控件 命名为 timerOrderDeductionDetailJob,Enable设为T ...

  8. WCF学习之旅—WCF服务的Windows 服务程序寄宿(十一)

    上接    WCF学习之旅—WCF服务部署到IIS7.5(九) WCF学习之旅—WCF服务部署到应用程序(十) 七 WCF服务的Windows 服务程序寄宿 这种方式的服务寄宿,和IIS一样有一个一样 ...

  9. WCF服务二:创建一个简单的WCF服务程序

    在本例中,我们将实现一个简单的计算服务,提供基本的加.减.乘.除运算,通过客户端和服务端运行在同一台机器上的不同进程实现. 一.新建WCF服务 1.新建一个空白解决方案,解决方案名称为"WC ...

随机推荐

  1. windows命令——taskkill

    C:\Users\Administrator>taskkill /? TASKKILL [/S system [/U username [/P [password]]]] { [/FI filt ...

  2. Leetcode-190 Reverse Bits

    #190. Reverse Bits Reverse bits of a given 32 bits unsigned integer. For example, given input 432615 ...

  3. 模拟image的ajaxPrefilter与ajaxTransport处理

    ////////////////////////////////////////////////////////////////// // options 是请求的选项 // // originalO ...

  4. Use Qt in Debian for OpenCASCADE

    Use Qt in Debian for OpenCASCADE eryar@163.com Recently several OpenCASCADE enthusiasts want to buil ...

  5. WindowsError的错误代码详解

    0操作成功完成. 1功能错误. 2系统找不到指定的文件. 3系统找不到指定的路径. 4系统无法打开文件. 5拒绝访问. 6句柄无效. 7存储控制块被损坏. 8存储空间不足,无法处理此命令. 9存储控制 ...

  6. 不使用session,借助redis实现验证码

    1.首先看一下基本的流程 2.看一下代码 注:其中用到的一些工具类,可以到我的github上去下载  https://github.com/hjzgg/usually_util/tree/master ...

  7. IOS开发之绝对布局和相对布局(屏幕适配)

    之前如果做过Web前端页面的小伙伴们,看到绝对定位和相对定位并不陌生,并且使用起来也挺方便.在IOS的UI设计中也有绝对定位和相对定位,和我们的web前端的绝对定位和相对定位有所不同但又有相似之处.下 ...

  8. 【记录】GitHub/TortoiseGit 修改邮箱/提交者

    我使用 Git 客户端工具是 TortoiseGit,在提交更新的时候,不知何时起会出现下面这种情况: 正常提交作者信息显示应该是: 本来也没怎么注意,但是在提交历史中,记录就不显示出来了,也就是在首 ...

  9. [Math] Backpropagation

    没啥自己的内容,推荐若干链接,这些是真爱,你值得拥有. Principles of training multi-layer neural network using backpropagation ...

  10. swift中Range的使用书名

    在swift中Range有两种用法 1.把字符串转换成NSString来使用 //这里是把swift的字符换转换成了nsstring 使用 let str :NSString = text.strin ...