Skip to content

Instantly share code, notes, and snippets.

@greenkiwi
Created June 30, 2014 17:29
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save greenkiwi/cfb496b2eacf7bb215f0 to your computer and use it in GitHub Desktop.
Save greenkiwi/cfb496b2eacf7bb215f0 to your computer and use it in GitHub Desktop.
Add-In-Express ClickOnce Update Service - monitor current ClickOnce install silently for updated, fire an event if there is an update
using HivePointAddIn.Utils;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Xml.Linq;
namespace AddIn.Services
{
partial class UpdateService
{
public static UpdateService Instance = new UpdateService();
}
partial class UpdateService
{
public event EventHandler UpdateAvailable;
public bool IsUpdateAvailable { get; private set; }
protected void OnUpdateAvailable()
{
EventHandler handler = UpdateAvailable;
if (null != handler)
{
handler(this, null);
}
}
private UpdateService()
{
}
public void Start()
{
if (AddinModule.CurrentInstance.IsNetworkDeployed())
{
SilentCheck(3);
}
}
private void SilentCheck(int delay_sec)
{
if (!IsUpdateAvailable)
{
int delay_ms = delay_sec * 1000;
if (delay_ms <= 0)
{
delay_ms = 100;
}
Scheduler.Run("Check for update", () =>
{
try
{
string updateLocation = PropertiesService.Instance.UpdateUrl;
string rawLocation = this.GetType().Assembly.Location;
string directory = Path.GetDirectoryName(rawLocation);
string installManifestFile = Path.Combine(directory, "appconfig.xml");
try
{
LoggingService.Instance.Info("Loading install manifest: " + installManifestFile);
XDocument installManifest = XDocument.Load(installManifestFile);
foreach (XElement element in installManifest.Root.Descendants())
{
if ("application".Equals(element.Name.LocalName))
{
XAttribute versionAttr = element.Attribute("providerUrl");
if (null != versionAttr)
{
updateLocation = versionAttr.Value;
}
break;
}
}
}
catch (Exception ex)
{
LoggingService.Instance.Error("Error loading install manifect: " + installManifestFile, ex);
}
LoggingService.Instance.Info("Checking for update: " + updateLocation);
XDocument manifest = XDocument.Load(updateLocation);
foreach (XElement element in manifest.Root.Descendants())
{
if ("assemblyIdentity".Equals(element.Name.LocalName))
{
XAttribute versionAttr = element.Attribute("version");
if (null != versionAttr)
{
string versionStr = versionAttr.Value;
Version serverVersion = new Version(versionStr);
if (this.GetType().Assembly.GetName().Version < serverVersion)
{
LoggingService.Instance.Info("Update available: " + serverVersion);
IsUpdateAvailable = true;
OnUpdateAvailable();
}
}
break;
}
}
}
catch (Exception ex)
{
LoggingService.Instance.Error("Exception: " + ex);
}
finally
{
if (!IsUpdateAvailable)
{
SilentCheck(60 * 60);
}
}
}, delay_ms);
}
}
public void CheckAndUpdate()
{
AddinModule.CurrentInstance.CheckForUpdates();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment