本文转载:

http://www.cnblogs.com/aierong/archive/2012/05/28/2521409.html

http://www.cnblogs.com/jys509/p/4628926.html

官网github

https://github.com/Topshelf/Topshelf/downloads

b.利用组件Topshelf

本方式特点:代码简单,开源组件,Windows服务可运行多个实例

Topshelf是一个开源的跨平台的服务框架,支持Windows和Mono,只需要几行代码就可以构建一个很方便使用的服务. 官方网站:http://topshelf-project.com

第1步:引用程序集TopShelf.dll和log4net.dll

第2步:创建一个服务类MyClass,里面包含两个方法Start和Stop,还包含一个定时器Timer,每隔5秒往文本文件中写入字符

    public class MyClass
    {
        readonly Timer _timer;

        private static readonly string FileName = Directory.GetCurrentDirectory ( ) + @"\" + "test.txt";

        public MyClass ( )
        {
            _timer = new Timer ( 5000 )
            {
                AutoReset = true ,
                Enabled = true
            };

            _timer.Elapsed += delegate ( object sender , ElapsedEventArgs e )
            {
                this.witre ( string.Format ( "Run DateTime {0}" , DateTime.Now ) );
            };
        }

        void witre ( string context )
        {
            StreamWriter sw = File.AppendText ( FileName );
            sw.WriteLine ( context );
            sw.Flush ( );
            sw.Close ( );
        }

        public void Start ( )
        {
            this.witre ( string.Format ( "Start DateTime {0}" , DateTime.Now ) );
        }

        public void Stop ( )
        {
            this.witre ( string.Format ( "Stop DateTime {0}" , DateTime.Now ) + Environment.NewLine );
        }

    }

第3步:使用Topshelf宿主我们的服务,主要是Topshelf如何设置我们的服务的配置和启动和停止的时候的方法调用

    class Program
    {
        static void Main ( string [ ] args )
        {
            HostFactory.Run ( x =>
            {
                x.Service<MyClass> ( ( s ) =>
                {
                    s.SetServiceName ( "ser" );
                    s.ConstructUsing ( name => new MyClass ( ) );
                    s.WhenStarted ( ( t ) => t.Start ( ) );
                    s.WhenStopped ( ( t ) => t.Stop ( ) );
                } );

                x.RunAsLocalSystem ( );

                //服务的描述
                x.SetDescription ( "Topshelf_Description" );
                //服务的显示名称
                x.SetDisplayName ( "Topshelf_DisplayName" );
                //服务名称
                x.SetServiceName ( "Topshelf_ServiceName" );

            } );
        }
    }

第4步: cmd命令

ConsoleApp_Topshelf.exe  install    (安装Windows服务)

ConsoleApp_Topshelf.exe  uninstall  (卸载Windows服务)

代码下载:http://files.cnblogs.com/aierong/ConsoleApp_Topshelf.rar

使用Topshelf创建Windows 服务的更多相关文章

  1. 使用Topshelf创建Windows服务

    概述 Topshelf是创建Windows服务的另一种方法,老外的一篇文章Create a .NET Windows Service in 5 steps with Topshelf通过5个步骤详细的 ...

  2. Topshelf创建Windows服务

    使用Topshelf创建Windows服务 概述 Topshelf是创建Windows服务的另一种方法,老外的一篇文章Create a .NET Windows Service in 5 steps ...

  3. 【第三方插件】使用Topshelf创建Windows服务

    概述 Topshelf是创建Windows服务的另一种方法,老外的一篇文章Create a .NET Windows Service in 5 steps with Topshelf通过5个步骤详细的 ...

  4. C#/.NET基于Topshelf创建Windows服务的守护程序作为服务启动的客户端桌面程序不显示UI界面的问题分析和解决方案

    本文首发于:码友网--一个专注.NET/.NET Core开发的编程爱好者社区. 文章目录 C#/.NET基于Topshelf创建Windows服务的系列文章目录: C#/.NET基于Topshelf ...

  5. 使用Topshelf创建Windows服务[转载]

    概述 Topshelf是创建Windows服务的另一种方法,老外的一篇文章Create a .NET Windows Service in 5 steps with Topshelf通过5个步骤详细的 ...

  6. [Solution] Microsoft Windows 服务(2) 使用Topshelf创建Windows服务

    除了通过.net提供的windows服务模板外,Topshelf是创建Windows服务的另一种方法. 官网教程:http://docs.topshelf-project.com/en/latest/ ...

  7. 使用 Topshelf 创建 Windows 服务

    Ø  前言 C# 创建 Windows 服务的方式有很多种,Topshelf 就是其中一种方式,而且使用起来比较简单.下面使用 Visual Studio Ultimate 2013 演示一下具体的使 ...

  8. Quartz.NET+Topshelf 创建Windows服务

    由于项目开发中经常会有定时任务执行的需求,所以会第一时间就想到 windows 服务 的方式,但是做过开发的同学都知道windows服务不利于调试,安装也麻烦: 并且有开源的作业框架Quartz.NE ...

  9. Topshelf 创建windows服务注意事项

    其中项目应该是控制台应用程序 test.exe需要赋与管理员权限,右键属性可以定义. test.exe  install test.exe unstall

随机推荐

  1. 17173php招聘

    学历要求:大专工作经历:4年↑薪资待遇:面议工作地点:北京市 资深PHP工程师岗位职责\\\"1.独立负责公司互联网产品的开发与测试2.主导负责相关系统维护,确保系统运行稳定可靠3.主导推动 ...

  2. #Leet Code# Root to leaf

    语言:Python 描述:使用递归实现 def getList(self, node): if node is None: return [] if node.left is None and nod ...

  3. Socket 错误总结

    错误 因为并没有搞清楚accept函数的使用,所以导致不停的发送失败,同时还不知道错误在哪里,无意中看见errno这个库,可以记录错误的原因,才知道原因在于没有用客户端的套接字进行接收数据,而这个客户 ...

  4. CXF WebService 教程

    业务需求:常见WEB服务: 手机淘宝.京东…. 天气预报 手机号归属地 股票查询 发手机短消息 手机充值功能 中英文翻译 银行转账业务 公司的“进销存系统”在某商品缺货时自动给供应商下订单 ..... ...

  5. Hadoop下各技术应用场景

    数据采集和DataFlow 对于数据采集主要分为三类,即结构化数据库采集,日志和文件采集,网页采集.对于结构化数据库,采用Sqoop是合适的,可以实现结构化数据库中数据并行批量入库到hdfs存储.对于 ...

  6. Android开源项目发现--- 工具类依赖注入DI篇(持续更新)

    通过依赖注入减少View.服务.资源简化初始化,事件绑定等重复繁琐工作 1. AndroidAnnotations(Code Diet) android快速开发框架 项目地址:https://gith ...

  7. Android 用代码来实现selector

    众所周知,android可以通过XML文件来创建selector,以Drawable对象的形式安装到组件上,以提供统一的风格设置.但是在某些时候,我们需要通过代码的形式来实现相同的功能,例如组件数量非 ...

  8. python手记(31)

    #!/usr/bin/env python #-*- coding: utf-8 -*- import cv2 import numpy as np fn="test2.jpg" ...

  9. read by other session

    模拟场景: 同时开多个SESSION执行select * from test2; read by other session: 当会话必须等待其他会话将统一数据块读入缓冲存储器时,作为read by ...

  10. 译文:TransactionScope 与 Async/Await

    你可能不知道这一点,在 .NET Framework 4.5.0  版本中包含有一个关于 System.Transactions.TransactionScope 在与 async/await 一起工 ...