本文转载:

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. DateTimePicker时间控件:

    DateTimePicker时间控件: http://xdsoft.net/jqplugins/datetimepicker/ 可以参考文档设置各种属性,格式. 用法: 首先下载datetimepic ...

  2. [CSS]border边框

    border: 1px solid #ccc;    /*1像素 实线 灰色*/可分割成:border-width:1px;border-style: solid; border-color: #00 ...

  3. hadoop namenode多次格式化后,导致datanode启动不了

    jps hadoop namenode -format dfs directory : /home/hadoop/dfs --data --current/VERSION #Wed Jul :: CS ...

  4. MAC 安装Ruby On Rails

    MAC 安装Ruby On Rails 对于新入门的开发者,如何安装 Ruby, Ruby Gems 和 Rails 的运行环境可能会是个问题,本页主要介绍如何用一条靠谱的路子快速安装 Ruby 开发 ...

  5. Apache添加虚拟主机目录

    <VirtualHost 127.0.0.2:80> DocumentRoot d:/abcd ServerName 127.0.0.2:80</VirtualHost> &l ...

  6. Coursera《machine learning》--(6)逻辑回归

    六 逻辑回归(Logistic Regression:LR) 逻辑回归(Logistic Regression, LR)模型其实仅在线性回归的基础上,套用了一个逻辑函数,但也就是由于这个逻辑函数,使得 ...

  7. hibernate中的缓存机制

    一.为什么要用Hibernate缓存? Hibernate是一个持久层框架,经常访问物理数据库. 为了降低应用程序对物理数据源访问的频次,从而提高应用程序的运行性能. 缓存内的数据是对物理数据源中的数 ...

  8. [BZOJ 1016] [JSOI2008] 最小生成树计数 【DFS】

    题目链接:BZOJ - 1016 题目分析 最小生成树的两个性质: 同一个图的最小生成树,满足: 1)同一种权值的边的个数相等 2)用Kruscal按照从小到大,处理完某一种权值的所有边后,图的连通性 ...

  9. Android 使用HTTP(get和post)方式登陆服务器

    package com.wuyou.submittoserver; import android.os.Bundle; import android.support.v7.app.ActionBarA ...

  10. CAD文件导入AD09

    1.首先将CAD图纸倒出为DXF格式的文件. 2.在altium designer 的pcb编辑中点菜单文件--导入,在弹出的对话框中,选择导入文件的类型,选择 DWG,DXF类型.然后确定,再弹出的 ...