简体   繁体   中英

Windows service error: the savedState dictionary does not contain the expected values and might have been corrupted

I'm trying to create multiple services using one EXE file.

I've added a installer class:

[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;
    }

}

As you can see, I get the service name and parameter from the app config file:

 <add key="ServiceName" value="POCService1"/>
 <add key="ServiceDsiaply" value="POC Service 1"/>

The service class is empty and only have empty onStart and OnStop methods.

public partial class POCService : ServiceBase
{
    public POCService()
    {
        this.ServiceName = "POCService";
        InitializeComponent();
    }

    protected override void OnStart(string[] args)
    {
    }

    protected override void OnStop()
    {
    }
}

When I do the install using command line, %SystemRoot%\\Microsoft.NET\\Framework\\v4.0.30319\\InstallUtil.exe ServicePOC.exe

I get the error:

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)方法。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM