美文网首页
Topshelf开源跨平台宿主服务框架

Topshelf开源跨平台宿主服务框架

作者: 翱翔的大脚丫 | 来源:发表于2016-03-23 20:47 被阅读539次

介绍:

Topshelf是一个开源的跨平台的宿主服务框架,支持Windows和Mono,只需要几行代码就可以构建一个很方便使用的服务宿主。

官网:http://topshelf-project.com
GitHub:http://github.com/topshelf/Topshelf

项目测试:

创建项目

  1. 新建一个控制台程序

  2. 添加引用
    通过nuget包管理器搜索TopShelf,安装:

    1.jpg
  3. 创建服务
    先创建一个类如MyService,继承ServiceControl,然后实现:
    可以看到Start方法和Stop方法。

    2.jpg
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Topshelf;

namespace app1
{
   public class MyService:ServiceControl
    {
        public bool Start(HostControl hostControl)
        {
            throw new NotImplementedException();
        }

        public bool Stop(HostControl hostControl)
        {
            throw new NotImplementedException();
        }
    }
}

在方法中实现要在服务程序中实现的功能代码。

3.jpg
using System;
using System.Timers;
using Topshelf;

namespace app1
{
   public class MyService:ServiceControl
    {
       readonly Timer _timer;
       public MyService()
       {
           _timer = new Timer(1000) { AutoReset = true };
           _timer.Elapsed += (sender, eventArgs) => Console.WriteLine("It is time of {0}.", DateTime.Now);
           
       }
       
        public bool Start(HostControl hostControl)
        {
            _timer.Start();
            return true;
        }

        public bool Stop(HostControl hostControl)
        {
            _timer.Stop();
            return true;
        }
    }
}
  1. 调用
    在Main中调用
    5.jpg
using Topshelf;

namespace app1
{
    class Program
    {
        static void Main(string[] args)
        {
            HostFactory.Run(x =>
            {
                x.Service<MyService>();

                //以local system模式运行
                x.RunAsLocalSystem();

                /*
                //启动类型设置
                x.StartAutomatically();//自动
                x.StartAutomaticallyDelayed();// 自动(延迟启动)
                x.StartManually();//手动
                x.Disabled();//禁用
                */
                //常规信息
                x.SetDescription("服务的描述信息"); //MyService服务的描述信息
                x.SetDisplayName("服务的显示名称"); //MyService服务的显示名称
                x.SetServiceName("服务名称"); //MyService服务名称

            });
        }
    }
}

  1. 服务设置完成后,可以直接双击以控制台的形式运行。

    6.jpg
  2. 安装服务
    安装:程序文件名.exe install
    启动:程序文件名.exe start
    停止:程序文件名.exe stop
    卸载:程序文件名.exe uninstall

    更多命令:程序文件名.exe help

跨平台(未验证)

Topshelf是一个开源的跨平台的宿主服务框架,不过本身只支持mono 命令行执行,不能使用Topshelf的命令行Start,Stop控制服务。

将程序上传到linux 系统执行。 执行:

mono 程序文件名.exe

注意:topshelf 对linux的支持需要加一个扩展包https://www.nuget.org/packages/Topshelf.Linux/,然后Mono 最好用最新的4.2 版本。

4.jpg

相关文章

  • Topshelf开源跨平台宿主服务框架

    介绍: Topshelf是一个开源的跨平台的宿主服务框架,支持Windows和Mono,只需要几行代码就可以构建一...

  • iOS优秀框架收集

    一、跨平台的移动开发框架 ReactNative(Facebook开源) framework7 PhoneGap ...

  • Flutter了解

    Flutter:谷歌推出的开源跨平台移动应用开发框架(使用Dart语言开发)。 前言:原生开发与跨平台开发 原生开...

  • flutter-MacOs App2

    Flutter 框架 Flutter 是 Google 开发的一套全新的跨平台、开源 UI 框架,支持 iOS、A...

  • ReactNative和原生的相互调用

    ReactNative? 是Facebook开源的跨平台移动应用开发框架 ReactNative的导入 iOS使用...

  • Apollo自动驾驶之无人驾驶概览

    Apollo 技术框架 Apollo 技术框架由四个层面组成,参考车辆平台、参考硬件平台、开源软件平台、云服务平台...

  • React-native + Nodejs打造移动端 + 服务端

    React Native (简称RN)是Facebook开源的跨平台移动应用开发框架,使用Javascript语言...

  • Topshelf 秒建 Windows 服务

    Topshelf 是一个用来部署 基于.NET Framework 开发的服务 的框架。它极大的简化服务创建与部署...

  • React native笔记

    一、框架介绍React Native (简称RN)是Facebook于2015年4月开源的跨平台移动应用开发框架,...

  • asp.net core 系列 1 概述

    一. 概述 ASP.NET Core 是一个跨平台的高性能开源框架,可以用来:建置 Web 应用程序和服务、I...

网友评论

      本文标题:Topshelf开源跨平台宿主服务框架

      本文链接:https://www.haomeiwen.com/subject/khxrlttx.html