Code Monkey home page Code Monkey logo

serviceself's Introduction

ServiceSelf

.NET 泛型主机的应用程序提供自安装为服务进程的能力,支持windows和linux平台。

功能

  • 自我服务安装
  • 自我服务卸载
  • 自我服务日志监听

1 nuget

https://www.nuget.org/packages/ServiceSelf/

2 使用示例

static void Main(string[] args)
{
    // 创建Host之前调用Service.UseServiceSelf(args)
    if (Service.UseServiceSelf(args))
    {
        var builder = WebApplication.CreateBuilder(args);

        // 为Host配置UseServiceSelf()
        builder.Host.UseServiceSelf();

        var app = builder.Build();
        app.MapGet("/", context => context.Response.WriteAsync("ServiceSelf"));
        app.Run();
    }
}
static void Main(string[] args)
{
    var serviceName = "app";
    var serviceOptions = new ServiceOptions
    {
        Arguments = new[] { new Argument("key", "value") },
        Description = "这是演示示例应用",
    };
    serviceOptions.Linux.Service.Restart = "always";
    serviceOptions.Linux.Service.RestartSec = "10";
    serviceOptions.Windows.DisplayName = "演示示例";
    serviceOptions.Windows.FailureActionType = WindowsServiceActionType.Restart;

    if (Service.UseServiceSelf(args, serviceName, serviceOptions))
    {
        var host = Host.CreateDefaultBuilder(args)
            // 为Host配置UseServiceSelf()
            .UseServiceSelf()
            .ConfigureServices(service =>
            {
                service.AddHostedService<AppHostedService>();
            })
            .Build();

        host.Run();
    }
}

3 服务控制

当yourapp集成ServiceSelf之后,在管理员或root下使用如下命令控制yourapp服务

windows平台

yourapp.exe start // 安装并启动服务
yourapp.exe stop // 停止并删除服务
yourapp.exe logs // 控制台输出服务的日志
yourapp.exe logs filter="key words" // 控制台输出服务的日志

linux平台

sudo ./yourapp start // 安装并启动服务
sudo ./yourapp stop // 停止并删除服务
sudo ./yourapp logs // 控制台输出服务的日志
sudo ./yourapp logs filter="key words" // 控制台输出服务的日志

serviceself's People

Contributors

xljiulang 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.