目录nginx
Net Core的本质是窗口程序(windows下的表现形式是console窗口)。客户与本公司的产品部经理都反馈若是不当心关闭了窗口,整个程序被关闭,后果可能会很严重,故将软件经过TopShelf作成服务模式,经过cmd的指令来安装,启动,中止卸载此程序。web
好比内存超过1G时,设置重启。redis
避免窗口模式误关闭。数据库
Program.cs文件,详见注释json
var rc = HostFactory.Run(x => { /*运行MainService主程序*/ //建立一个MainService服务实例 x.Service<MainService>(s => { //通知TopShelf 这里有一个MainService类型的服务,经过s来配置他的参数 s.ConstructUsing(name => new MainService(Directory.GetCurrentDirectory())); //TopShelf启动服务 s.WhenStarted(tc => tc.Start()); //TopShelf中止服务 s.WhenStopped(tc => tc.Stop()); }); //x.RunAs("username", "password");也能够用户名密码方式运行 x.RunAsLocalSystem(); //服务描述 x.SetDescription("WEBAPIService"); //服务显示名称 x.SetDisplayName("WEBAPIService"); //服务名称 x.SetServiceName("WEBAPIService"); }); //转化退出编码 var exitCode = (int)Convert.ChangeType(rc, rc.GetTypeCode()); //设置退出编码 Environment.ExitCode = exitCode;
MainService.cs文件,详见注释。windows
namespace IBMS.WEBAPI { public class MainService { //建立一个webhost实例 private IWebHost _webHost; private readonly string _contentRoot; public MainService(string contentRoot) { _contentRoot = contentRoot; } //服务模式启动程序 public void Start() { // 获取连接字符串 var config = new ConfigurationBuilder() .SetBasePath(Directory.GetCurrentDirectory()) .AddJsonFile("appsettings.json") .Build(); //配置webhost _webHost = new WebHostBuilder() .UseKestrel() .UseContentRoot(_contentRoot) .UseUrls(config["urls"]) .UseStartup<Startup>() .UseSerilog() .Build(); var _logger = _webHost.Services.GetService<ILoggerFactory>().CreateLogger<MainService>(); _logger.Log(LogLevel.Information, new EventId(1001, "Starting"), "Service Starting"); //种子数据种入数据库 using (var scope = _webHost.Services.CreateScope()) { try { var context = scope.ServiceProvider.GetService<IIBMSContext>(); var concreteContext = (IBMSContext)context; concreteContext.Database.Migrate(); SeedData.Initialize(concreteContext); } catch (Exception ex) { // var _logger = scope.ServiceProvider.GetRequiredService<ILogger<MainService>>(); _logger.LogError(ex, "An error occurred while migrating or initializing the database."); } } //启动webhost _webHost.Start(); } public void Stop() { _webHost?.Dispose(); } } }
IBMS.WEBAPI.exe install
IBMS.WEBAPI.exe startapp
IBMS.WEBAPI.exe uninstall
IBMS.WEBAPI.exe stopide
若是您知道或者据说有以下问题的解决方案或者开源项目,烦请告知,让我也共同进步下,在此谢过。工具
好比windows上的msi安装包程序。ui
好比该配置工具可以读入配置文件的参数(config.js,my.ini,appsettings.json,nginx.conf,redis.windows.conf...),而且可以经过该配置管理工具以GUI的人机交互方式将用户本身的配置数据配置如对应的配置文件。
例如:
例如:
若是您知道以上3点问题的解决方案或者开源项目,恳请赐教,谢谢。