. Net (C #) Configuration System: Use transactedinstaller

Source: Internet
Author: User

Note:

To run the code, you must add a reference to system. configuration. Install. dll.

The installation configuration class is in the system. configuration. Install namespace.

 

Transactedinstaller is like a transaction in a database. The whole process is either fully executed or not executed at all. This requires that once an unmodifiable error occurs during execution, rollback (Restore) is required. The installer class in. Net (in the system. configuration. Install namespace) does not have such built-in functions. If an exception occurs in the install method, the rollback method will not be called.

 

For example, define an installer class and throw an exception in the install method!

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 ("Restore ");

}

 

Public override void uninstall (idictionary savedstate)

{

Base. Uninstall (savedstate );

Console. writeline ("Uninstall ");

}

}

 

Then run:

New myinstaller (). Install (New hashtable ());

 

The result is a normal unhandled exception and the process ends.

 

Of course, you can write a similar method to complete the corresponding functions, such as the following class:

/// <Summary>

/// Code, slightly modified from:

/// 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 );

}

}

}

 

Finally, you can use transactedinstaller (sorry ...... The topic of the article appears so late. You only need to add the installer to the sub-Installer (through the installers attribute of the installer class ).

Code:

VaR transactedins = new transactedinstaller ();

Transactedins. installers. Add (New myinstaller ());

Transactedins. Install (New hashtable ());

 

Output:

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.

Restore

 

The rollback phase completed successfully.

 

Unhandled exception: system. invalidoperationexception: the installation failed,

And the rollback has been completed MED. ---> system. Exception: exception of type's

Ystem. Exception 'was thrown.

 

We can see that the restore operation has been executed, and the exception in the install method is encapsulated as invalidoperationexception and then thrown again.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.