• TopShelf 自动配置Service测试


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

    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

  • 相关阅读:
    利用jmSlip写一个移动端顶部日历选择组件
    JS写的排序算法演示
    jmSlip WEB前端滑屏组件
    如何:使用 Visual Basic 编写基于 Unity3D 的计算器
    验证 .NET 4.6 的 SIMD 硬件加速支持的重要性
    VB 2015 的 闭包(Closure)
    VS "15" 预览 5 中 VB 15 新增的功能
    演练:使用Xamarin.Forms开发产品介绍性质的应用(VB版)
    UWP游戏防内存修改器的方法
    优化win2d实现的萤火虫粒子效果
  • 原文地址:https://www.cnblogs.com/crazyguo/p/8472088.html
Copyright © 2020-2023  润新知