专业网站建设品牌,十四年专业建站经验,服务6000+客户--广州京杭网络
免费热线:400-683-0016      微信咨询  |  联系我们

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

当前位置:网站建设 > 技术支持
资料来源:网络整理       时间:2023/2/14 0:38:55       共计:3638 浏览

注意:

运行代码需要添加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然后再次抛出。

版权说明:
本网站凡注明“广州京杭 原创”的皆为本站原创文章,如需转载请注明出处!
本网转载皆注明出处,遵循行业规范,如发现作品内容版权或其它问题的,请与我们联系处理!
欢迎扫描右侧微信二维码与我们联系。
·上一条:C# winform程序怎么打包成安装项目(图解) | ·下一条:在 Windows 平台搭建 PHP 集成开发环境

Copyright © 广州京杭网络科技有限公司 2005-2025 版权所有    粤ICP备16019765号 

广州京杭网络科技有限公司 版权所有