Matthew Haugen
Matthew Haugen

Reputation: 13286

Restart and Update ClickOnce Application In Production

The Short Version

Is there a way to automatically update a ClickOnce application while it is running?

The Long Version...

Setup

I have an API and a console agent for an application I'm developing. I needed some work offloaded and this was the easiest and quickest way to handle that. I'm new to ClickOnce (this is the only time I've ever used it, in fact), but it seems perfect for me. I've set it up so that the application runs online with the "no installation" option set.

At a high scale, the agent calls the API to ask what it should do, and the API returns down a task and whatever parameters are required.

Question

Because of the "quiet' nature of my console runner, I'm trying to make it require as little user interaction as is possible. However, I'm now facing the issue of needing to update it. Ideally, it'd be running on a server in my closet, so I'd really rather not have to connect a monitor and all that every time I need to restart the app.

Is there a good way to update a ClickOnce application while it's running?

Possible Solutions

I haven't found anything relating to this in my searches. I've found a few places talking about doing updates, but they're all giving examples of how to check for updates when the application starts. That's not even relevant for me, since I'm not installing it on the client machine.

As I said, the server is pushing commands down to the application, so it'd be no problem for me to implement one that says "perform an update." My initial thought was to do that, then do a Process.Start(string) to pull up the .application file from my web server, which will of course have the latest version. I could do that, then stop the running instance, and theoretically that would work.

But that raises issues with signing things. I'd rather not deal with signing, although admittedly I've never done anything with it so maybe it isn't as bad as it sounds. But right now, when I start the runner from my website, it requires that I click a Windows dialog to ensure that I recognize the risks. I don't care if I have to do that for the first run, but clearly that negates the benefits of being able to do an in-production update like this.

Maybe signing is easier than I think? Is there a good way to, for free, sign the application and have it trustworthy enough that I can avoid that dialog, at least after I've accepted it once?

I'd love a standard way to do this, but since it's not a particularly standard problem I'm also open to less-than-standard ways. For instance if I could start another console app, stop the main one, swap out some application files, then start it up, that could work. But that sounds super prone to failure and just generally more complicated than I'd like for this to be.

It doesn't have to be a 100% solution. Obviously that would be nice, but just something that works most of the time would suffice, since this is really more a personal project of mine than anything that goes on client's computers.

Upvotes: 1

Views: 739

Answers (1)

Lucas
Lucas

Reputation: 3502

You need ApplicationDeployment

There are the methods CheckForUpdate() and CheckForUpdateAsync()

When an update is available use Update() or UpdateAsync()

And that is a way to restart ClickOnce applications.

using System.Diagnostics;
using System.Windows;

public class Foo
{
    public void Restart()
    {
        Process.Start(Application.ResourceAssembly.Location);
        Application.Current.Shutdown();
    }
}

Upvotes: 3

Related Questions