I have a Windows Service that kick off a job based on schedule.
We wanted to add silent selft updates and have been trying to incorporate NAppUpdate for this purpose.
I have recompiled NAppUpdate from source (targeting .NET 4.5) and have attempted to recreate the example (current one relies on framework returning true/false which is no longer the case)...
Can someone please explain if I have this right...
private static void AutoUpdate(IUpdateSource updateSource)
{
if (updateSource == null)
{
throw new ArgumentNullException("updateSource", "Update source cannot be null!");
}
try
{
// ------------------------------------------------------------
// 1. initialize & verify
//
// set source
UpdateManager.Instance.UpdateSource = updateSource;
// Only check for updates if we haven't done so already
if (UpdateManager.Instance.State != UpdateManager.UpdateProcessState.NotChecked)
{
//NOTE: why rollback check only after != NotChecked?
if (UpdateManager.Instance.State == UpdateManager.UpdateProcessState.RollbackRequired)
{
UpdateManager.Instance.RollbackUpdates();
}
else
{
return;
}
}
// ------------------------------------------------------------
// 2. check for updates
//
UpdateManager.Instance.CheckForUpdates(); // NOTE: should we check the status == Checked first?
if (UpdateManager.Instance.UpdatesAvailable == 0)
{
return;
}
// ------------------------------------------------------------
// 3. prepare updates (download, stage, etc, etc)
//
UpdateManager.Instance.PrepareUpdates(); // NOTE: should we check the status == Prepared first?
// ------------------------------------------------------------
// 4. apply updates
//
UpdateManager.Instance.ApplyUpdates(false, true, false); // relaunchApplication, updaterDoLogging, updaterShowConsole
if (UpdateManager.Instance.State == UpdateManager.UpdateProcessState.AppliedSuccessfully)
{
Logger.Info(CultureInfo.InvariantCulture, m => m("4a. Update successful! [state: \"{0}\"]", UpdateManager.Instance.State));
}
else
{
Logger.Warn(CultureInfo.InvariantCulture, m => m("4b. Update was *not* successful! [state: \"{0}\"]", UpdateManager.Instance.State));
}
}
catch (NAppUpdateException e)
{
Logger.Error(CultureInfo.InvariantCulture, m => m("Update failed! [last state: \"{0}\"]", UpdateManager.Instance.State), e);
//NOTE: should we Rollback on exception?
}
catch (Exception e)
{
Logger.Error(CultureInfo.InvariantCulture, m => m("Exception occurred during update process! [last state: \"{0}\"]", UpdateManager.Instance.State), e);
//NOTE: should we Rollback on exception?
}
finally
{
Logger.Info(CultureInfo.InvariantCulture, m => m("Dumping log file & cleaning up..."));
UpdateManager.Instance.Logger.Dump();
UpdateManager.Instance.CleanUp();
}
}