Monday, February 19, 2007

ClickOnce Deployment and Expired Temporary (Test) Certificates

I have recently been helping on a maintenance project for a new client, and we ran into an issue for our first deployment.

Basically, a test certificate had been used when the application was first deployed using ClickOnce deployment and had expired in December last year. Publishing through Visual Studio would not work due to the expired certificate and you get the following error:

The deployment identity does not match the subscription.

Crap! What now?

I found a Microsoft KB article related to the issue with nice concise title of "You receive an error message when you try to update a Visual Studio 2005 ClickOnce application after the certificate that was used to sign the installation expires". This gives some code that helps you renew a test certificate.

Great! Then I found someone who'd actually gone to the effort of making the code a little bit more robust and works with more certificates. You can find the project source and executable on Cliff Stanford's site RenewCert - Working Version.

This allows you to create a new certificate with the same key and extended expiry date. Publishing of subsequent updates should now work like a charm.

Wednesday, February 14, 2007

Getting the Version of a .NET Assembly at Runtime

The versions of all .NET assemblies are commonly located in the AssemblyInfo.cs file in each project. This can be extracted via code for use during execution and can easily be used to display version information on an About Box, for example.

Getting the version as a string is as straightfoward as the following code:

string version = Assembly.GetExecutingAssembly().GetName().Version.ToString();

The GetExecutingAssembly() method returns the Assembly of the executable that the code is running in. Other methods allow you to get the calling assembly or the Assembly containing a particular Type.

The GetName() method call returns an AssemblyName object, which provides information the assembly's unique identity, including the version, version compatibility, culture information and full name.

The Version property returns a Version object, providing access to the individual elements of the version (major, minor, revision, etc). It also provides overloads for comparison with other Version objects.