Strong name signing for Media Center application assemblies

I have run into an issue a few times recently while helping folks create installers for their Windows Vista Media Center applications that are based on the project templates that ship in the Windows Vista Media Center SDK.  As a result, even though this is covered in the SDK documentation and on the Media Center Sandbox blog, I wanted to specifically describe this scenario, the underlying design of Media Center that introduces this issue and how to fix it in your application.

Description of the problem

When creating a new Windows Media Center Presentation Layer application using the project template included in the Windows Vista Media Center SDK, an XML file is generated that can be used with RegisterMceApp.exe to register the application with Media Center.  However, this XML file does not contain a public key token value (because it is not possible to know ahead of time what the public key token will be for each developer/company/etc).  If you do not add a strong name key file to your Media Center application assembly and also add the PublicKeyToken value to the XML file, you will be able to register the application and it will appear in the Media Center UI after registering it, but it will fail to launch correctly.

Why does this happen?

Media Center currently only supports loading application assemblies from the global assembly cache (GAC) or from the %windir%\ehome directory.  As a best practice, application developers should install their assemblies to the GAC on users' systems and not fill up the %windir%\ehome directory with files that are not a part of Media Center itself.

If you are creating a Media Center application that consists of a code-based assembly, and that assembly is installed to the GAC (which it should be if you follow the best practice I listed above), you must register the application using the RegisterApplication API or the RegisterMceApp.exe utility.  When the assembly is located in the GAC, you must specify the full strong name identity for the assembly or else Media Center will not be able to locate the assembly to load it correctly.  The strong name identity for .NET Framework 2.0 assemblies that can be loaded by Windows Vista Media Center consists of the assembly name, the assembly version, the culture, and the public key token (and the processor architecture if you need to create and ship non-MSIL assemblies for some reason).

How to fix this issue

In order to fix this issue, you need to do all of the following:

  1. Add a strong name key file to your Visual Studio project
  2. Rebuild your Media Center application assembly
  3. Add the correct PublicKeyToken value to the XML file used to register your application with Media Center

Charlie wrote up excellent instructions for developing Windows Vista Media Center applications that include steps to do all three of the above things, and they can be found in the Media Center application step-by-step instructions on the Media Center Sandbox blog.  I will also summarize the steps here to hopefully make them easier to discover in web searches.

To add a strong name key file to the project assembly:

  1. Right-click on the project in the Solution Explorer and choose Properties.
  2. Select the Signing tab.
  3. Check the box labeled Sign the assembly.
  4. Click on the drop-down labeled Choose a strong name key file and select <New...>.
  5. Enter a key file name, and optionally provide a password for the key file.
  6. Click OK to add the key file to the project.
  7. Click on the File menu and choose Save All.
  8. Click on the Build menu and select Build Solution to build the project assembly with the strong name key file included.

To add a strong name key to App.xml that is included when you create a new Windows Media Center Presentation Layer application using the Visual Studio 2005 project template:

  1. In Visual Studio 2005, click on the Tools menu and select External Tools.
  2. Click the Add button to create a new external tool item.
  3. Enter Get Public Key in the Title text box.
  4. If you are running on an x86 version of Windows, enter C:\Program Files\Microsoft Visual Studio 8\SDK\v2.0\Bin\sn.exe in the Command text box. If you are running on an x86 version of Windows, enter C:\Program Files (x86)\Microsoft Visual Studio 8\SDK\v2.0\Bin\sn.exe in the Command text box.
  5. Enter -Tp "$(TargetPath)" in the Arguments text box.
  6. Uncheck all options except Use Output window.
  7. Click the OK button.
  8. Click on the Tools menu and select Get Public Key.
  9. Copy the public key token value.
  10. Open the file App.xml and locate the addin value in the entrypoint element.
  11. After the text Version=6.0.6000.0, add a comma and the text PublicKeyToken= and then add the public key token that was reported by the Get Public Key tool.
  12. Save and close App.xml.

After using the above steps, you can proceed to create an MSI-based installer for your application using the WiX installer instructions in the step-by-step guide.  When installing the MSI, your application assembly should install correctly to the GAC and Media Center will know how to load it because the registration XML file contains the full strong name identity information for your assembly.