隐藏

.NET(C#)配置系统:使用TransactedInstaller

发布:2020/7/6 15:03:02作者:管理员 来源:本站 浏览次数:1155

注意:

运行代码需要添加System.configuration.install.dll的引用。

安装配置类在System.Configuration.Install命名空间内。

 

TransactedInstaller就像数据库中的Transaction(事务),整个过程要么全部执行,要么一点也不执行,这需要执行过程中一旦有不可修正的错误发生,要进行回滚(还原)操作。而.NET中的安装程序类:Installer类(System.Configuration.Install命名空间内)并没有内置这样的功能。如果在Install方法中出现了异常,RollBack方法是不会被调用的。

 

比如定义一个安装程序类,在Install方法中抛出异常!

class MyInstaller : Installer

{

    public override void Install(IDictionary stateSaver)

    {

        base.Install(stateSaver);

        throw new Exception();

    }

 

    public override void Rollback(IDictionary savedState)

    {

        base.Rollback(savedState);

        Console.WriteLine("还原");

    }

 

    public override void Uninstall(IDictionary savedState)

    {

        base.Uninstall(savedState);

        Console.WriteLine("卸载");

    }

}

 

然后运行:

new MyInstaller().Install(new Hashtable());

 

结果就是普通的未处理异常,进程结束。

 

当然,你可以自己写一个类似的方法来完成相应功能,比如下面这个类:

/// <summary>

/// 代码,稍作修改自:

/// https://groups.google.com/group/microsoft.public.dotnet.languages.csharp/browse_thread/thread/4d45e9ea5471cba4/4519371a77ed4a74?hl=en

/// </summary>

class InstallHelper

{

    public static void Install(bool uninstall, Installer inst)

    {

        try

        {

            Console.WriteLine(uninstall ? "uninstalling" : "installing");

            using (inst)

            {

                IDictionary state = new Hashtable();

                try

                {

                    if (uninstall)

                    {

                        inst.Uninstall(state);

                    }

                    else

                    {

                        inst.Install(state);

                        inst.Commit(state);

                    }

                }

                catch

                {

                    try

                    {

                        inst.Rollback(state);

                    }

                    catch { }

                    throw;

                }

            }

        }

        catch (Exception ex)

        {

            Console.Error.WriteLine(ex.Message);

        }

    }

}

 

最后,使用TransactedInstaller也可以(抱歉……文章的主题这么晚才出现),只需要把安装程序添加到子安装程序中(通过Installer类的Installers属性)。

代码:

var transactedIns = new TransactedInstaller();

transactedIns.Installers.Add(new MyInstaller());

transactedIns.Install(new Hashtable());

 

输出:

Running a transacted installation.

 

Beginning the Install phase of the installation.

 

An exception occurred during the Install phase.

System.Exception: Exception of type 'System.Exception' was thrown.

 

The Rollback phase of the installation is beginning.

还原

 

The Rollback phase completed successfully.

 

Unhandled Exception: System.InvalidOperationException: The installation failed,

and the rollback has been performed. ---> System.Exception: Exception of type 'S

ystem.Exception' was thrown.

 

可以看到,还原操作已经被执行,Install方法内的异常被包装成InvalidOperationException然后再次抛出。

Open-mouthed smile