GithubHelp home page GithubHelp logo

yinrunhao / timingconsole Goto Github PK

View Code? Open in Web Editor NEW
0.0 2.0 0.0 50 KB

一个基于.Net Standard 2.1的控制台程序模板框架,可在其轻松添加定时任务和命令行交互.

License: MIT License

C# 100.00%

timingconsole's Introduction

TimingConsole

一个基于.Net Standard 2.1的控制台程序模板框架,可在其轻松添加定时任务和命令行交互。使用了.Net标准的依赖注入扩展、日志扩展和配置文件扩展,能像在ASP.net Core中一样轻松获取对象和配置程序。

使用方法

如Demo项目中的例子所示。

1.定义自己的命令行处理类和定时任务处理类

命令行处理类需实现ICommand的接口

    /// <summary>
    /// 示例命令,用于返回字符串长度
    /// </summary>
    public class StrLenCommand : ICommand
    {
        public HandleResult Execute(string[] param)
        {
            HandleResult result = new HandleResult();

            if(param.Length > 1)
            {
                result.Message = $"parameter error";
                result.Code = 2;
            }
            else
            {
                result.Message = $"string length:{param[0].Length}";
                result.Code = 1;
            }

            return result;
        }
    }

定时任务处理类需要实现ICron接口

    /// <summary>
    /// 示例定时任务,定时向控制台打印Hello World
    /// </summary>
    public class HelloCron : ICron
    {
        public async Task<HandleResult> Execute()
        {
            HandleResult result = new HandleResult();
            // 假装工作了100ms
            await Task.Delay(100);
            // 输出信息
            result.Message = "Hello World";
            // 返回码,小于0退出程序
            result.Code = 1;
            return result;
        }

        public Task Exit()
        {
            return Task.CompletedTask;
        }

        public Task Start()
        {
            return Task.CompletedTask;
        }
    }

2.编写程序启动配置

程序启动配置类实现IAppStarup接口,在其接口方法中添加命令行处理类、定时任务类和依赖注入。

    /// <summary>
    /// 示例启动配置
    /// </summary>
    public class DemoStartup : IAppStartup
    {
        public void ConfigureCommand(IConfiguration config, CommandCollection commands)
        {
            // 添加命令行交互处理对象
            commands.AddCommand<StrLenCommand>("strlen");
        }

        public void ConfigureCron(IConfiguration config, CronCollection crons)
        {
            // 添加定时任务处理对象
            // 读取配置
            int interval = config.GetValue<int>("HelloInterval");
            crons.AddCron<HelloCron>(TimeSpan.FromSeconds(interval));
        }

        public void ConfigureServices(IConfiguration config, IServiceCollection services)
        {
            // 像ASP.net Core一样配置依赖注入
            services.AddSingleton<ILogger>((sp) =>
            {
                // 指定ILogger的依赖注入是NLog.config中配置好的AppLogger
                var factory = sp.GetService<ILoggerFactory>();
                return factory.CreateLogger("AppLogger");
            });
            services.AddLogging(loggingBuilder =>
            {
                // 使用NLog作为日志组件
                loggingBuilder.ClearProviders();
                loggingBuilder.SetMinimumLevel(LogLevel.Trace);
                loggingBuilder.AddNLog();
            });
        }
    }

3.开始运行

以指定的启动项配置获取默认控制台应用并运行。

    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Press Ctrl+C to exit");
            ConsoleAppBuilder.CreateDefaultConsoleApp<DemoStartup>().Run();
        }
    }

不使用默认实现

可以自行继承TimingConsole.Abstractions里面的ConsoleApp抽象类来实现更多自定义功能。

命令行使用示例

commands.AddCommand("strlen");

如上所示,在启动配置中添加了StrLenCommand命令,命令的名称是strlen 在控制台中输入:"strlen helloworld"即可以helloworld作为参数调用StrLenCommand的Execute方法。 控制台输入字符串解析规则: {命令名称} {参数1} {参数2} ...

注意事项

  1. 程序默认读取的配置文件是根目录下的appsettings.json文件。
  2. 程序启动后调用完定时任务的Start方法后不会立即执行定时任务,而是5秒后开始。
  3. 每次定时任务的执行和命令的执行获取的依赖注入范围都是独立的Scope,可以放心地将需要依赖注入的类型设定为AddScoped。
  4. 若定时任务和命令行执行类实现了IDisposable接口,则完成后会自动调用其Dispose方法。
  5. 命令行字符串以空格分隔参数。
  6. 程序退出时会等待所有定时任务执行完最后一次才退出。

timingconsole's People

Contributors

yinrunhao avatar

Watchers

James Cloos avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.