Windows 服务开发框架介绍 - Topshelf

关于 TopShelf


Topshelfis a framework for hosting services written using the .NET framework. The
creation of services is simplified, allowing developers to create a simple
console application that can be installed as a service using Topshelf. The
reason for this is simple: It is far easier to debug a console application than
a service. And once the application is tested and ready for production, Topshelf
makes it easy to install the application as a service.

示例


第一步:新建一个 C# 控制台应用程序。


第二步:用 Nuget 引用 TopShelf 和 Log4net。


第三步:贴出下面的代码。


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Timers;

using Topshelf;
using Topshelf.Builders;
using Topshelf.Configurators;
using Topshelf.HostConfigurators;

namespace TopshelfStudy
{
public sealed class TimeReporter
{
private readonly Timer _timer;
public TimeReporter()
{
_timer = new Timer(1000) { AutoReset = true };
_timer.Elapsed += (sender, eventArgs) => Console.WriteLine("当前时间:{0}", DateTime.Now);
}
public void Start() { _timer.Start(); }
public void Stop() { _timer.Stop(); }
}

class Program
{
static void Main(string[] args)
{
HostFactory.Run(x =>
{
x.Service<TimeReporter>(s =>
{
s.ConstructUsing(settings => new TimeReporter());
s.WhenStarted(tr => tr.Start());
s.WhenStopped(tr => tr.Stop());
});

x.RunAsLocalSystem();

x.SetDescription("定时报告时间");
x.SetDisplayName("时间报告器");
x.SetServiceName("TimeReporter");
});
}
}
}

第四步:编译、Release 生成。把 Release 文件夹 Copy 到 C 盘。


第五步:以管理员身份运行 cmd,安装、启动。

SampleWindowsService.exe install

启动

SampleWindowsService.exe start

如果要卸载也很方便

SampleWindowsService.exe uninstall

效果如下:

最后 Windows Service 没有 Console.WriteLine,我们可以把输出信息输出到文件。

StreamWriter sw = new StreamWriter("e:\\temp\\log.log");
sw.AutoFlush = true;
Console.SetOut(sw);

参考网站:

http://www.cnblogs.com/shanyou/archive/2011/05/04/2037008.html

http://www.cnblogs.com/happyframework/p/3601995.html

http://www.cnblogs.com/chenxizhang/archive/2010/04/21/1716773.html

谢谢浏览!

Windows 服务开发框架介绍 - Topshelf,布布扣,bubuko.com

时间: 2024-07-30 20:28:37

Windows 服务开发框架介绍 - Topshelf的相关文章

.NET Core Generic Host Windows服务部署使用Topshelf

此文源于前公司在迁移项目到.NET Core的过程中,希望使用Generic Host来管理定时任务程序时,没法部署到Windows服务的问题,而且官方也没给出解决方案,只能关注一下官方issue #809 等他们方解决了. 官方文档只提供了一个<在 Windows 服务中托管 ASP.NET Core>的方案,可以使用Microsoft.AspNetCore.Hosting.WindowsServices类库来把Web应用部署为Windows服务.但是ASP.NET Core虽然是控制台程序

使用Topshelf组件构建简单的Windows服务

很多时候都在讨论是否需要了解一个组件或者一个语言的底层原理这个问题,其实我个人觉得,对于这个问题,每个人都有自己的看法,个人情况不同,选择的方式也就会不同了.我个人觉得无论学习什么,都应该尝试着去了解对应的原理和源码(这里就不要急着吐槽,容我说完).对底层的了解不是为了让你写出类似的东西,让你写也不可能写的出来,重写一个就需要以此修改整个底层结构,了解底层知识只是为了让你可以在写业务代码时,选择合适的方式,以此使底层与业务层配合达到效率最佳.任何一种方式有坏有好,需要合适的选择. 如果觉得楼主以

C#创建、安装一个Windows服务

C#创建.安装一个Windows服务http://blog.csdn.net/yysyangyangyangshan/article/details/10515035 关于WIndows服务的介绍,之前写过一篇:http://blog.csdn.net/yysyangyangyangshan/article/details/7295739.可能这里对如何写一个服务不是很详细.现在纯用代码的形式介绍一下windows服务是如何开发和安装的. 开发环境:Win7 32位:工具:visualstudi

Topshelf创建Windows服务

使用Topshelf创建Windows服务 概述 Topshelf是创建Windows服务的另一种方法,老外的一篇文章Create a .NET Windows Service in 5 steps with Topshelf通过5个步骤详细的介绍使用使用Topshelf创建Windows 服务.Topshelf是一个开源的跨平台的宿主服务框架,支持Windows和Mono,只需要几行代码就可以构建一个很方便使用的服务宿主. 引用安装 1.官网:http://topshelf-project.c

使用Topshelf 5步创建Windows 服务 z

使用Topshelf创建Windows 服务简要的介绍了创建Windows服务的另一种方法,老外的一篇文章Create a .NET Windows Service in 5 steps with Topshelf通过5个步骤详细的介绍使用使用Topshelf创建Windows 服务.Topshelf是一个开源的跨平台的宿主服务框架,支持Windows和Mono,只需要几行代码就可以构建一个很方便使用的服务宿主. 1.Topshelf的代码托管在http://topshelf-project.c

使用Topshelf创建Windows 服务

本文转载:http://www.cnblogs.com/aierong/archive/2012/05/28/2521409.html b.利用组件Topshelf 本方式特点:代码简单,开源组件,Windows服务可运行多个实例 Topshelf是一个开源的跨平台的服务框架,支持Windows和Mono,只需要几行代码就可以构建一个很方便使用的服务. 官方网站:http://topshelf-project.com 第1步:引用程序集TopShelf.dll和log4net.dll 第2步:创

【转】使用Topshelf创建Windows服务

转自:http://www.cnblogs.com/jys509/p/4614975.html 概述 Topshelf是创建Windows服务的另一种方法,老外的一篇文章Create a .NET Windows Service in 5 steps with Topshelf通过5个步骤详细的介绍使用使用Topshelf创建Windows 服务.Topshelf是一个开源的跨平台的宿主服务框架,支持Windows和Mono,只需要几行代码就可以构建一个很方便使用的服务宿主. 引用安装 1.官网

使用.NET Core创建Windows服务(二) - 使用Topshelf方式

原文:Creating Windows Services In .NET Core – Part 2 – The "Topshelf" Way 作者:Dotnet Core Tutorials 译者:Lamond Lu 译文:使用.NET Core创建Windows服务(二) - 使用Topshelf方式 使用.NET Core创建Windows服务 使用微软推荐方式 使用Topshelf方式 在前一篇文章中,我给大家介绍了,如何基于微软推荐方式使用.NET Core创建Windows

Caliburn.Micro开发框架介绍 -- Windows phone

Caliburn.Micro开发框架介绍 Caliburn是一套基于XAML的开发框架,它小巧而强大.利用它不但能提高开发效率,还可以提高XAML程序开发的可维护行.可扩展性和可测试性.Caliburn.Micro则是专门针对Windows phone开发的版本. MVVM简介 MVVM源于微软的软件开发模式,可以粗略的认为它是MVC模式的发展,原来Controller的职能被拆分,其中值转换器(Value Converter)和绑定器(binder)已经由框架实现,程序员可以更关注在逻辑实现上