如何在单个可执行文件中创建完整的服务?有一个现成的应用程序(你可以采取零winforms)。我添加了 services.cs,没有任何效果,没有例外。
ManagedInstallerClass.InstallHelper - 读取 - 失败。试图通过 RunInstaller 安装 - 也不起作用。
我尝试通过 sc create 安装(在命令行上) - 它已安装。
Services.cs 文本
using System;
using System.ServiceProcess;
using System.Configuration.Install;
using System.Collections;
using System.Collections.Specialized;
using System.Windows.Forms;
namespace CaiNiaoServiceWinForms{
class Service:ServiceBase {
static string myname = "AMyService";
public Service()
{
this.ServiceName = myname;
this.CanStop = true;
this.CanPauseAndContinue = false;
this.AutoLog = true;
}
static public void Run() {
ServiceBase[] ServicesToRun = new ServiceBase[] { new Service() };
ServiceBase.Run(ServicesToRun);
}
protected override void OnStart(string[] args){ // TODO: add startup stuff
}
protected override void OnStop(){
// TODO: add shutdown stuff
}
//---------------------------------
// Service config for UI
// тут работает
static ServiceController GetService() {
foreach (ServiceController item in ServiceController.GetServices())
if (item.ServiceName == myname) return item;
return null;
}
public static bool Installed {
get { // тут работает
return (GetService()!=null);
}
set { // тут не работает
string servicePath = System.Reflection.Assembly.GetEntryAssembly().Location;
if (value) { /*install*/
MyInstaller inst = new MyInstaller();
//inst.Install(dict); не работает, даже если создать словарь
//inst.Commit(dict);
}
//ManagedInstallerClass.InstallHelper(new[] { "/i" , servicePath, "/SERVICE" });
//ManagedInstallerClass.InstallHelper(new[] { servicePath , "/SERVICE" });
} else { /*uninstall*/
ManagedInstallerClass.InstallHelper(new[] { "/u" , servicePath, "/SERVICE" });
}
}
}
public static void DoStart () {
if (Installed) GetService().Start();
}
public static void DoStop () {
if (Installed) GetService().Stop();
}
}
//------------------
[System.ComponentModel.RunInstaller(true)]
public partial class MyInstaller: Installer {
// https://docs.microsoft.com/ru-ru/dotnet/api/system.serviceprocess.serviceinstaller
private ServiceInstaller serviceInstaller;
private ServiceProcessInstaller processInstaller;
public MyInstaller() {
processInstaller = new ServiceProcessInstaller();
serviceInstaller = new ServiceInstaller();
processInstaller.Account = ServiceAccount.LocalSystem;
serviceInstaller.StartType = ServiceStartMode.Manual;
serviceInstaller.Context =
new System.Configuration.Install.InstallContext(null, new string[]{"/SERVICE"});
}
}
}
我的主要(program.cs)
static void Main(string[] args)
{
try {
if (args.Length > 0) {
if (args[0] == "/SERVICE") {
Service.Run();
return;
}
if (args[0].ToUpper() == "/INSTALL") {
Service.Installed = true;
return;
}
if (args[0].ToUpper() == "/UNINSTALL") {
Service.Installed = false;
return;
}
}
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new MainForm(args));
} catch (Exception e) {
MessageBox.Show(e.ToString());
}
}
启动服务... 根据微软的说法,要启动服务,必须使用特殊工具或其特殊实用程序手动完成(使用 InstallHelper 等时)。我使用 WMI 安装。
PS 要安装启动服务,程序必须从管理员(包括管理单元)下启动。
服务的“模板”变成了这样
程序的文本是