在开发中经常会遇到后台定时处理数据和任务的情况,处理这些事情大概有以下几种方案:

1.使用数据库的job功能。优点是在数据库中可以完成的就在数据库中完成,配置等基础设施数据库都提供,简单快捷。缺点是如果业务复杂,写SQL的存储过程也就越复杂,不便后期的维护。

2.自己开发程序处理。优点是灵活处理各种业务需求。但缺点是需要自己去配置Service服务或者自己完成类似Service固定处理的功能,额外增加了很多开发和维护的工作。

综合下来2中解决问题的方案各有利弊,在开发中应该根据实际情况来决定选取何种方案,有时也需要2中方案结合。

针对第2中方案,开源框架TopShelf可以最大程度的解决其弊端,所以在接下来的场景中会尝试使用这一框架。

参考文章:https://www.cnblogs.com/yanglang/p/7199913.html

1.TopShelf的安装,也就dll的引用

在这里折腾了几个小时,问题的原因是VS2010版本只能支持到.netFramework 4.0,而Topshelf 4.0.3版本以上的代码使用了.netFramework 4.5.2版本,这样是无法安装的。只能使用至少VS2013才可以。而VS这个IDE是要收费的,尤其是正规公司,所以有必要考虑Python语言的应用了。

下图是在VS2013下的情况:

2.TopShelf.Log4Net日志的安装,也就dll的引用

同上。

3.Demo案例使用


public class TownCrier
{
readonly Timer _timer;
public TownCrier()
{
_timer = new Timer(1000) {AutoReset = true};
_timer.Elapsed += (sender, eventArgs) => Console.WriteLine("It is {0} and all is well", DateTime.Now);
}
public void Start() { _timer.Start(); }
public void Stop() { _timer.Stop(); }
} public class Program
{
public static void Main()
{
var rc = HostFactory.Run(x => //1
{
x.Service<TownCrier>(s => //2
{
s.ConstructUsing(name=> new TownCrier()); //3
s.WhenStarted(tc => tc.Start()); //4
s.WhenStopped(tc => tc.Stop()); //5
});
x.RunAsLocalSystem(); //6 x.SetDescription("Sample Topshelf Host"); //7
x.SetDisplayName("Stuff"); //8
x.SetServiceName("Stuff"); //9
}); //10 var exitCode = (int) Convert.ChangeType(rc, rc.GetTypeCode()); //11
Environment.ExitCode = exitCode;
}
}
  1. Here we are setting up the host using the HostFactory.Run the runner. We open up a new lambda where the ‘x’ in this case exposes all of the host level configuration. Using this approach the command arguments are extracted from environment variables. We also capture the return code of the service - which we return on line 11.
  2. Here we are telling Topshelf that there is a service of type ‘TownCrier”. The lambda that gets opened here is exposing the service configuration options through the ‘s’ parameter.
  3. This tells Topshelf how to build an instance of the service. Currently we are just going to ‘new it up’ but we could just as easily pull it from an IoC container with some code that would look something like ‘container.GetInstance<TownCrier>()’
  4. How does Topshelf start the service
  5. How does Topshelf stop the service
  6. Here we are setting up the ‘run as’ and have selected the ‘local system’. We can also set up from the command line Interactively with a win from type prompt and we can also just pass in some username/password as string arguments
  7. Here we are setting up the description for the winservice to be use in the windows service monitor
  8. Here we are setting up the display name for the winservice to be use in the windows service monitor
  9. Here we are setting up the service name for the winservice to be use in the windows service monitor
  10. Now that the lambda has closed, the configuration will be executed and the host will start running.
  11. Finally, we convert and return the service exit code.

 4.调试运行效果

5.配置安装为windows service服务

   1.cd 到应用程序的debug目录下

   2. **.exe install

   

3.启动服务

  **.exe start

4.卸载服务

 **.exe uninstall

5.停止服务

**.exe stop

TopShelf 自动配置Service测试的更多相关文章

  1. ApplicationContextRunner如何简化自动配置测试

    1. 概览 众所周知,自动配置是Spring Boot的关键功能之一, 但测试自动配置可能会很棘手. 在以下部分中,我们将展示ApplicationContextRunner如何简化自动配置测试. 2 ...

  2. 在 Linux 中自动配置 IPv6 地址

    在 Linux 中自动配置 IPv6 地址 在本文中,我们将学习如何为 ULA 自动配置 IP 地址. 何时使用唯一本地地址 唯一本地地址unique local addresses(ULA)使用 f ...

  3. Loadrunner 脚本开发-利用Loadrunner生成Web service测试脚本

    脚本开发-利用Loadrunner生成Web service测试脚本 1.选择协议--Web Service,如下图 2.导入服务 入口1:点击Manage Services ->弹出窗中选择“ ...

  4. spring boot 系列之六:深入理解spring boot的自动配置

    我们知道,spring boot自动配置功能可以根据不同情况来决定spring配置应该用哪个,不应该用哪个,举个例子: Spring的JdbcTemplate是不是在Classpath里面?如果是,并 ...

  5. SpringBoot自动配置的实现原理

    之前一直在用SpringBoot框架,一直感觉SpringBoot框架自动配置的功能很强大,但是并没有明白它是怎么实现自动配置的,现在有空研究了一下,大概明白了SpringBoot框架是怎么实现自动配 ...

  6. Nginx 通过 certbot 为网站自动配置 SSL 证书并续期

    一.背景知识 1.1.http 和 https 是什么? 简单来说,http 是一个传输网页内容的协议,比如你看到的 http 开头的网站 http://www.163.com ,其网页上的文字.图片 ...

  7. Spring boot运行原理-自定义自动配置类

    在前面SpringBoot的文章中介绍了SpringBoot的基本配置,今天我们将给大家讲一讲SpringBoot的运行原理,然后根据原理我们自定义一个starter pom. 本章对于后续继续学习S ...

  8. SpringBoot自定义starter及自动配置

    SpringBoot的核心就是自动配置,而支持自动配置的是一个个starter项目.除了官方已有的starter,用户自己也可以根据规则自定义自己的starter项目. 自定义starter条件 自动 ...

  9. spring自定义自动配置注解

    我们知道springboot自动配置@EnableAutoConfiguration是通过@Import(AutoConfigurationImportSelector.class)来把自动配置组件加 ...

随机推荐

  1. 二层协议--MPLS协议总结

    1.MPLS是介于2层和3层之间的协议,主要应用在城域网中,作为集客专线.基站等承载VPN技术的关键技术. 2.MPLS利用MPLS标签进行转发,先通过IP单播路由的方式沿途分配好MPLS标签,分配完 ...

  2. vue路由的配置

    一.准备工作 1安装vue-cli  npm install vue-cli -g 2检查是否安装成功 vue -V(大写V) 3初始化一个新的项目 vue init  webpack vue-dem ...

  3. 【详细】【转】CentOS 7部署ASP.NET Core应用程序

    很早就看过关于net core部署在Linux上的文章,自己也曾亲自将项目部署在Linux上,今天看到这篇文章,为其格式之工整而转! 1.环境准备 网上看了一下,Linux云服务器还挺贵的,那就只好先 ...

  4. Expo大作战(三十二)--expo sdk api之Noifications

    简要:本系列文章讲会对expo进行全面的介绍,本人从2017年6月份接触expo以来,对expo的研究断断续续,一路走来将近10个月,废话不多说,接下来你看到内容,讲全部来与官网 我猜去全部机翻+个人 ...

  5. 洗礼灵魂,修炼python(50)--爬虫篇—基础认识

    爬虫 1.什么是爬虫 爬虫就是昆虫一类的其中一个爬行物种,擅长爬行. 哈哈,开玩笑,在编程里,爬虫其实全名叫网络爬虫,网络爬虫,又被称为网页蜘蛛,网络机器人,在FOAF社区中间,更经常的称为网页追逐者 ...

  6. 洗礼灵魂,修炼python(30)--装饰器(2)—>装饰器总结+进阶使用

    在上一篇博文的经典案例中,我想你应该对装饰器有很好的了解了,不过光有那些还不够真的,还需要总结和进阶一下,所以本篇博文解析装饰器进阶. 装饰器 1.什么是装饰器? 个人理解:装饰器又叫语法糖,指的是对 ...

  7. SSO阅读有感

    SSO比较详细且理解.赞 链接:https://www.cnblogs.com/ywlaker/p/6113927.html

  8. Hibernate 中的 idclass mapping 问题

    关于出现 idclass mapping 运行错误 @IdClass 注释通常用于定义包含复合键id的Class.即多个属性的关键复合. @IdClass(CountrylanguageEntityP ...

  9. cobaltstrike3.8服务器搭建及使用

    参考链接: https://www.ezreal.net/archives/166.htmlhttp://blog.cobaltstrike.com/category/cobalt-strike-2/ ...

  10. iframe无刷新跨域上传文件并获得返回值

    原文:http://geeksun.iteye.com/blog/1070607 需求:从S平台上传文件到R平台,上传成功后R平台返回给S平台一个值,S平台是在一个页面弹出的浮窗里上传文件,所以不能用 ...