BeetleX进程服务管理组件应用

        有些时候需要在程序中启动和管理其他应用进程,当碰到这样的需求的时候可以通过Process对象来完成;为了让使用和管理更方便在这基础上封装 了BeetleX.ServicesProcess组件,通过组件的管理中心让进程操作更方便,同时还集成了Web套件可直接集成在BeetleX.FastHttpApi中进行Web管理。

        BeetleX.ServicesProcess组件提供了ProcessCenter对象来添加,管理,启动和停止等进程管理功能。具体功能方法如下:

public class ProcessCenter : IDisposable
    {
        public ProcessCenter();
        public ProcessCenter(ILogHandler logHandler);
        public ServiceManagementConfig Config { get; }
        public void Add(string name, string file, string path, string args, bool autoStartup = false);
        public void Add(ProcessInfo info);
        public void AutoStartup();
        public void ChangeUser(string admin, string pwd);
        public void Delete(string id);
        public void Dispose();
        public ServiceProcess GetProcess(string id);
        public void Modify(string id, ProcessInfo info);
        public void Start(string id);
        public void StartAll();
        public void Stop(string id);
        public void StopAll();
    }

以上是组件封装的方法,使用起来非常简单。

        接下来主要介绍如何在BeetleX.FastHttpApi中集成它的web管理功能;创建一个控制台项目,引用BeetleX.WebFamily组件;引用后编写以下代码:

class Program
    {
        static void Main(string[] args)
        {


            WebHost host = new WebHost();
            host.IsWindowsServices = true;
            WebHost.Title = "Service Management";
            WebHost.HeaderModel = "beetlex-process-header";
            WebHost.HomeModel = "beetlex-process-home";
            WebHost.TabsEnabled = false;
            host.RegisterComponent<Program>();
            host.RegisterComponent<BeetleX.ServicesProcess.ProcessCenter>();
            host.UseFontawesome();
            host.UseElement(PageStyle.Element);
            host.Setting(o =>
            {
                o.SetDebug();
                o.Port = 80;
                o.LogLevel = LogType.Info;
            });
            host.Initialize((http, vue, rec) =>
            {
                BeetleX.ServicesProcess.WebController controller = new BeetleX.ServicesProcess.WebController();
                controller.Init(new logHandler(http));
                http.ActionFactory.Register(controller, new BeetleX.FastHttpApi.ControllerAttribute { BaseUrl = "process" });
                rec.AddCss("website.css");
                vue.Debug();
            });
            host.Run();


        }
    }
    class logHandler : BeetleX.ServicesProcess.ILogHandler
    {
        public logHandler(BeetleX.FastHttpApi.HttpApiServer sever)
        {
            mServer = sever;
        }




        private BeetleX.FastHttpApi.HttpApiServer mServer;


        public void Error(string message)
        {
            mServer.GetLog(LogType.Error)?.Log(LogType.Error, null, message);
        }


        public void Info(string message)
        {
            mServer.GetLog(LogType.Info)?.Log(LogType.Info, null, message);
        }
    }

启动项目后可以查看启动日志

7b2f84d4b606fbdf1f5200f672a0e6b7.png

接下来就可以通过浏览器访问进程管理页面

c0a5408e7ae33269ca4635d26a64060e.png

如果有需要还可以把当前示例项目发布成windows service

54344c11a2987956f31bf67bbd4b9cdd.png

发布后即可以使用sc命令来创建、启动、停止和删除服务。

完整示例代码

https://github.com/beetlex-io/BeetleX-Samples/tree/master/Web.ServiceManagement

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值