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,业务上需要写两个程序,一个 ...
随机推荐
- ssm开发使用redis作为缓存,使用步骤
1.关于spring配置文件中对于redis的配置 <!-- redis配置 --> <bean id="jedisPoolConfig" class=" ...
- Python字符串和日期相互转换的方法
import time,datetime # 日期转换成字符串 print time.strftime("%Y-%m-%d %X", time.localtime()) #字符串转 ...
- arguments.callee的临时指向特性
function r(){ alert('BBB'); } var a = { f: function(){ alert('AAA'); arguments.callee = r; } }; 弹出的都 ...
- 泛泰A870(高通APQ8064t 600 cpu) Mokee4.4.2(Android4.4) 图赏
watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvc3lob3N0/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/d ...
- Order笔记-数据库创建
过程: 1,为这个项目新建一个用户名(实例),专门用于这个项目 2,建表 问题: 列在此处不允许: 笔记: 建表设置默认值: alter table 表名 modify 字段名 default 默认值 ...
- 多线程day01
多线程作为Java中很重要的一个知识点,在此还是有必要总结一下的. 一.线程的生命周期及五种基本状态 关于Java中线程的生命周期,首先看一下下面这张较为经典的图: 上图中基本上囊括了Java中多线程 ...
- CRL快速开发框架升级到4.52,谈谈开发过程中的优化
CRL4.5版本已经稳定使用于目前的几个中型项目中,在实际使用中,也发现了不少问题,这些问题都在4.52中提交 CRL具体功能和使用请浏览 CRL快速开发框架系列教程 由于现在项目是一套业务系统,查询 ...
- vue2.0---vue-router总结(项目基于vue-cli)
vue2.0---vue-router总结(项目基于vue-cli) 1. 在项目中安装: npm install vue-router --save 2. 在项目中的引入: // The Vue b ...
- 关于close和shutdown
我们知道TCP是全双工的,可以在接收数据的同时发送数据.假设有主机A在和主机B通信,可以认为是在两者之间存在两个管道.就像这样:A ---------> BA <--------- B 1 ...
- Flink升级到1.4版本遇到的坑
Flink 1.4没出来以前,一直使用Flink 1.3.2,感觉还算稳定,最近将运行环境升级到1.4,遇到了一些坑: 1.需要将可运行程序,基于1.4.0重新编译一次 2.对比了一下flink-co ...