ClickOnce On-Demand Update Bug in Beta 2

In case you stumble and fall over this like I did, there is a bug in Beta 2 for configuring ClickOnce on-demand updates through the API. Normally if you wanted to support on-demand updates in a ClickOnce application, you would set it to not check for updates through the Updates settings in the Publish category of project properties in a Windows Forms project. You would then write code like the following in your application to support an on-demand update check (either programmatically or based on user request):


using

System.Deployment.Application;
publicpartialclassForm1 : Form


{


privatevoid OnDemandUpdate(object sender, EventArgs e)
{

if (!ApplicationDeployment.IsNetworkDeployed)
{

MessageBox.Show(“This application deployment does not support network updates.”);
return;
}

ApplicationDeployment deployment = ApplicationDeployment.CurrentDeployment;
Cursor currCursor = this.Cursor;
this.Cursor = Cursors.WaitCursor;
if (deployment.CheckForUpdate())
{

deployment.Update();

this.Cursor = currCursor;
DialogResult result = MessageBox.Show(“Update Complete. Restart?”, “ClickOnce On-Demand Update”,

MessageBoxButtons.YesNo);
if (result == DialogResult.Yes)
{

Application.Restart();
}

}

else
{

MessageBox.Show(“No updates available”);
}

this.Cursor = currCursor;
}

However, if you did this in Beta 2, you get an exception when this code runs with an IllegalOperationException saying this application cannot be updated programmatically. The workaround in Beta 2 is to tell the app to check for updates, set it to do so after the application starts, and set the subscription period (how often to check for updates) to a long value, such as 50 weeks. Be careful, because another bug is that VS will let you set an illegally large value for this subsciption, such as 512 weeks. If you do so though, you will get a deployment error because ClickOnce will see a schema violation, and if you try to edit the manifest with MaGe, it will crash with an unhandled exception. This should all be fixed by RTM, but are things to be aware of if you are playing with ClickOnce now or especially if you will be going to production with an application that uses this capability.



Saurabh Pant has a good post summarizing the specific settings that go in your manifest for this here.