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

时间:2015-02-15 09:11:31

标签: c# windows-services windows-installer

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

1 个答案:

答案 0 :(得分:2)

实施空public override void Commit(IDictionary savedState)方法。