Skip to content

为.NET 泛型主机的应用程序提供自安装为服务进程的能力

License

Notifications You must be signed in to change notification settings

xljiulang/ServiceSelf

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

68 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

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" // 控制台输出服务的日志

About

为.NET 泛型主机的应用程序提供自安装为服务进程的能力

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages