生活随笔
收集整理的這篇文章主要介紹了
BeetleX进程服务管理组件应用
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
????????有些時候需要在程序中啟動和管理其他應(yīng)用進(jìn)程,當(dāng)碰到這樣的需求的時候可以通過Process對象來完成;為了讓使用和管理更方便在這基礎(chǔ)上封裝 了BeetleX.ServicesProcess組件,通過組件的管理中心讓進(jìn)程操作更方便,同時還集成了Web套件可直接集成在BeetleX.FastHttpApi中進(jìn)行Web管理。
??????? BeetleX.ServicesProcess組件提供了ProcessCenter對象來添加,管理,啟動和停止等進(jìn)程管理功能。具體功能方法如下:
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管理功能;創(chuàng)建一個控制臺項目,引用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);}}
啟動項目后可以查看啟動日志
接下來就可以通過瀏覽器訪問進(jìn)程管理頁面
如果有需要還可以把當(dāng)前示例項目發(fā)布成windows service
發(fā)布后即可以使用sc命令來創(chuàng)建、啟動、停止和刪除服務(wù)。
完整示例代碼
https://github.com/beetlex-io/BeetleX-Samples/tree/master/Web.ServiceManagement
總結(jié)
以上是生活随笔為你收集整理的BeetleX进程服务管理组件应用的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。