Dotnet Core Windows Service
在dotnet 中有topshelf 可以很方便的写windows 服务并且安装也是很方便的,命令行 运行.exe install 就直接把exe 程序安装成windows 服务。当然
代码也要做相应的修改,具体的可以参照例子。
在dotnet core 2.0 中 我们也有一个很方便的dll 来试用
https://github.com/PeterKottas/DotNetCore.WindowsService
通过Nuget来安装 Using nuget: Install-Package PeterKottas.DotNetCore.WindowsService
方便多个服务我们先定义一个接口
public interface IBaseService
{
void Start();
void Stop();
}
具体的实现呢 我举个例子,在例子中我们试用了个Timer,定时的完成某些任务,这样 我们就可以同时写好几个service 只要继续 IBaseService 就行,也比较方面安装
public class SyncService : IBaseService
{
private readonly System.Timers.Timer _timer;
private readonly ILogger logger;
public SyncService( ILoggerFactory loggerFactory)
{
_timer = new System.Timers.Timer(10000);
_timer.Elapsed += new ElapsedEventHandler(OnTimedEvent);
_timer.Interval = 2000;
_timer.AutoReset = true;
_timer.Enabled = false;
logger = loggerFactory.CreateLogger<SyncService>();
}
private void OnTimedEvent(object source, ElapsedEventArgs e)
{
Console.WriteLine(string.Format("SyncService:{0:yyyy-MM-dd HH:mm:sss}", DateTime.Now));
_timer.Enabled = false;
try
{
//do some job;
}
catch (Exception ex)
{
logger.LogError("SyncService Error {0}:", ex.Message);
}
Console.WriteLine(string.Format("SyncService:{0:yyyy-MM-dd HH:mm:sss}", DateTime.Now));
Thread.Sleep(5 * 60 * 1000);
_timer.Enabled = true;
}
private async Task<HttpResponseMessage> SyncData()
{
string url = configModel.DatabaseIncrementUrl;
var httpClient = new HttpClient();
return await httpClient.GetAsync(url);
}
public void Start()
{
_timer.Start();
_timer.Enabled = true;
}
public void Stop()
{
_timer.Stop();
_timer.Enabled = false;
}
}
class Program
{
static void Main(string[] args)
{
IConfigurationRoot Configuration;
// ILoggerFactory LoggerFactory;
var builder = new ConfigurationBuilder()
.SetBasePath(Path.Combine(AppContext.BaseDirectory))
.AddJsonFile("appsettings.json")
.AddEnvironmentVariables();
Configuration = builder.Build();
var services = new ServiceCollection();
services.AddOptions();
services.Configure<ConfigModel>(Configuration.GetSection("ConfigSetting"));
services.AddSingleton<IConfiguration>(Configuration);
services.AddTransient<ILoggerFactory, LoggerFactory>();
services.AddTransient<IBaseService, SyncService>();
services.AddTransient<IBaseService, CreateDBService>();
services.AddTransient<IBaseService, OnLineOrderService>();
var serviceProvider = services.BuildServiceProvider();
ServiceRunner<ServiceFactory>.Run(config =>
{
var name = config.GetDefaultName();
config.Service(serviceConfig =>
{
serviceConfig.ServiceFactory((extraArguments, controller) =>
{
return new ServiceFactory(serviceProvider.GetService<IEnumerable<IBaseService>>(), controller);
});
serviceConfig.OnStart((service, extraArguments) =>
{
Console.WriteLine("Service {0} started", name);
service.Start();
});
serviceConfig.OnStop(service =>
{
Console.WriteLine("Service {0} stopped", name);
service.Stop();
});
serviceConfig.OnError(e =>
{
Console.WriteLine("Service {0} errored with exception : {1}", name, e.Message);
});
});
config.SetName("SAASService");
config.SetDescription("SAAS Service For All Saas Client");
config.SetDisplayName("SAAS Service");
});
}
}
Dotnet Core Windows Service的更多相关文章
- dotnet Core 2.0学习笔记(一)
一:Dotnet Core Windows运行环境,标红部分要注意 https://docs.microsoft.com/en-us/dotnet/core/windows-prerequisites ...
- 如何托管ASP.NET Core应用到Windows Service中
(此文章同时发表在本人微信公众号"dotNET开发经验谈",欢迎右边二维码来关注.) 题记:正在构思一个中间件的设计,考虑是否既可以使用最新的技术,也可以兼顾传统的部署模式.所以有 ...
- 目前.NET Core创建Windows Service比较好的一个开源框架:DasMulli.Win32.ServiceUtils
新建一个.NET Core控制台程序,搜索并下载Nuget包:DasMulli.Win32.ServiceUtils GitHub 链接及使用指南 Write a windows service us ...
- ASP.NET Core应用到Windows Service中
托管到Windows Service中 众所周知,ASP.NET Core采用了和传统ASP.NET不同的托管和HTTP处理方式,即把服务器和托管环境完全解耦. ASP.NET Core内置了两个HT ...
- ubuntu上部署windows开发的dotnet core程序
目标:完成windows上开发的dotnet core程序部署至linux服务器上(Ubuntu 14.04) windows上开发dotnet core很简单,安装好VS2017,建立相关类型的项目 ...
- Windows 7 上面安装 dotnet core 之后 使用 应用报错的处理:api-ms-win-crt-runtime-l1-1-0.dll 丢失
Windows2016 使用 dotnet core的使用 安装了就可以了 但是发现 windows 7 不太行 报错如图示 没办法简单百度了下 https://www.microsoft.com/z ...
- dotnet core在Task中使用依赖注入的Service/EFContext
C#:在Task中使用依赖注入的Service/EFContext dotnet core时代,依赖注入基本已经成为标配了,这就不多说了. 前几天在做某个功能的时候遇到在Task中使用EF DbCon ...
- dotnet core多平台开发体验(mac os x 、windows、linux)
前言 随着net core rc2的发布,园子里面关于net core的入门文章也也多了起来,但是大多数都是在一个平台上面来写几个简单的例子,或者是在解释代码本身,并没有体现说在一个平台上面创建一个项 ...
- 分别在.NET Framework 与 .NET Core 框架下 编写Windows Service(windows服务程序)
前言,为什么会分别在两个框架下编写Windows Service,是因为最近在做区块链这块,使用的是NEO(小蚁区块链)的相关技术,NEO使用的是.net core 2.1,业务上需要写两个程序,一个 ...
随机推荐
- 逐步搭建Lamp环境之rpm软件包管理
Linux中的rpm软件包管理类似于windows下的"xxx软件管家"."xxx电脑管家",其作用主要用于查询软件的安装情况.安装软件.卸载软件. 以下针对这 ...
- 三十天学不会TCP,UDP/IP网络编程-ARP -- 连接MAC和IP
继续来做(da)推(guang)介(gao)我自己的!由于这两年接触到了比较多的这方面的知识,不想忘了,我决定把他们记录下来,所以决定在GitBook用半年时间上面写下来,这是目前写的一节,目前已完成 ...
- linux crontab yum安装
crontab工具来做计划任务,定时任务,执行某个脚本等等 1.检查是否已安装crontab # crontab -bash: crontab: command not found 执行 cronta ...
- 机房收费系统——转换list泛型时,字段名称不正确应出现故障
因为之前对泛型研究了一番,所以就開始尝试着在机房收费系统重构中增加了泛型的使用. 可是到了做学生查看剩余金额这个功能的时候,还是依照之前的方法做的,可是在载入数据的时候班级这个框就没有显示出来--然后 ...
- 设计模式 - 装饰者模式(Decorator Pattern) 具体解释
装饰者模式(Decorator Pattern) 具体解释 本文地址: http://blog.csdn.net/caroline_wendy/article/details/26707033 装饰者 ...
- Ubuntu使用之Svn命令小技巧
注: [svn Path]:是指要代替码分支的server绝对路径 [Path]:是指终端相对当前文件夹的相对路径.假设是在当前文件夹下,就省略路径 ①.取svnserver的代码: svn co [ ...
- 自学Zabbix3.5.7-监控项item-Applications
Applications应用程序用于对逻辑组中的项目进行分组.例如我们要监控MySQL,我们可以将所有和MySQL相关的item放到这个应用程序中.例如MySQL的availability of My ...
- vertical-align 和 img属性 和 鼠标样式
一.vertical-align 一)定义:定义行内元素的基线相对于该所在基线的垂直对齐.(只针对行类块inline/inline-block/<img>,块级不适用!) 二)语法: 三 ...
- Github-karpathy/char-rnn代码详解
Github-karpathy/char-rnn代码详解 zoerywzhou@gmail.com http://www.cnblogs.com/swje/ 作者:Zhouwan 2016-1-10 ...
- MySQL存储过程之游标实战
MySQL存储过程之游标实战 博主日前在解决一个项目需求时,没有什么好的方法,于是就来学习存储过程了,之前也是接触过,奈何年少贪玩,竟是全部又还给了大学老师-苦不堪言呐-. 先说一下业务需求吧 ...