Windows服务错误:savedState字典不包含预期值,可能已损坏



我正在尝试使用一个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.NETFrameworkv4.0.30319InstallUtil.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.

实现一个空的public override void Commit(IDictionary savedState)方法。

最新更新