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.