我正在尝试使用一个EXE文件创建多个服务。
我添加了一个安装程序类:
[RunInstaller(true)]
public partial class POCInstall : System.Configuration.Install.Installer
{
private ServiceProcessInstaller m_ServiceProcess;
private ServiceInstaller m_ServiceInstaller;
public POCInstall()
{
InitializeComponent();
m_ServiceProcess = new ServiceProcessInstaller();
m_ServiceProcess.Account = ServiceAccount.NetworkService;
m_ServiceProcess.Username = null;
m_ServiceProcess.Password = null;
m_ServiceInstaller = new ServiceInstaller();
m_ServiceInstaller.BeforeInstall += new InstallEventHandler(onBeforeInstall);
m_ServiceInstaller.BeforeUninstall += new InstallEventHandler(onBeforeUninstall);
m_ServiceInstaller.BeforeRollback += new InstallEventHandler(onBeforRollback);
m_ServiceInstaller.ServiceName = "POCService";
this.Installers.Add(m_ServiceProcess);
this.Installers.Add(m_ServiceInstaller);
}
private void onBeforRollback(object sender, InstallEventArgs e)
{
string serviceName = ConfigurationManager.AppSettings["ServiceName"];
string serviceDsiaply = ConfigurationManager.AppSettings["ServiceDsiaply"];
m_ServiceInstaller.ServiceName = serviceName;
m_ServiceInstaller.DisplayName = serviceDsiaply;
}
private void onBeforeUninstall(object sender, InstallEventArgs e)
{
string serviceName = ConfigurationManager.AppSettings["ServiceName"];
string serviceDsiaply = ConfigurationManager.AppSettings["ServiceDsiaply"];
m_ServiceInstaller.ServiceName = serviceName;
m_ServiceInstaller.DisplayName = serviceDsiaply;
}
private void onBeforeInstall(object sender, InstallEventArgs e)
{
string serviceName = ConfigurationManager.AppSettings["ServiceName"];
string serviceDsiaply = ConfigurationManager.AppSettings["ServiceDsiaply"];
m_ServiceInstaller.ServiceName = serviceName;
m_ServiceInstaller.DisplayName = serviceDsiaply;
}
}
如您所见,我从应用程序配置文件中获取服务名称和参数:
<add key="ServiceName" value="POCService1"/>
<add key="ServiceDsiaply" value="POC Service 1"/>
服务类为空,只有空的onStart和OnStop方法。
public partial class POCService : ServiceBase
{
public POCService()
{
this.ServiceName = "POCService";
InitializeComponent();
}
protected override void OnStart(string[] args)
{
}
protected override void OnStop()
{
}
}
使用命令行%SystemRoot%\Microsoft.NET\Framework\v4.0.30319\InstallUtil.exe ServicePOC.exe
我收到错误:
An exception occurred during the Rollback phase of the System.ServiceProcess.ServiceInstaller installer.
System.ArgumentException: The savedState dictionary does not contain the expected values and might have been corrupted.
答案 0 :(得分:2)
实施空public override void Commit(IDictionary savedState)
方法。