Instrumenting and Re-Signing Assemblies

If you need to gather code coverage information while you are testing code in an assembly, you must first instrument that assembly. The process of instrumentation adds code that generates code coverage information into the assembly. For information about instrumenting assemblies for code coverage, see How to: Obtain Code Coverage Data.

But if the assembly you are testing was signed with a strong name, the code modification caused by instrumentation invalidates its signing. So Visual Studio automatically tries to re-sign the assembly immediately after the instrumentation step.

Note

For more information about the steps that occur in a test run, see Test Deployment Overview.

If the re-signing succeeds, the test run will probably finish and produce results correctly. If the signing does not succeed, an error such as the following is generated:

Strong name verification failed for the instrumented assembly 'SignedLibrary, Version=1.0.0.0, Culture=neutral, PublicKeyToken=1696e096eba75082'. Please ensure that the right key file for re-signing after instrumentation is specified in the test run configuration.

To fix this error, try the following:

  • Re-sign the assembly. If you have the key file that was used for signing the assembly, you can supply it so that Visual Studio can automatically re-sign the assembly.

  • Disable Signing Verification. You can disable the verification of signing on the computer on which you are testing the assembly. This action applies only to assemblies you specify.

Re-sign the Assembly

On the Code Coverage page of the test run configuration editor, you can specify a key file for re-signing binaries after instrumentation. If you are testing multiple assemblies that have been signed, Visual Studio will try to re-sign all the strong-named assemblies that were signed with the key file that you specify.

On the test run configuration editor, you can specify a key file that Visual Studio will use to re-sign strong-named assemblies. For more information, see the "Re-Signing Assemblies" procedure in the topic How to: Obtain Code Coverage Data. Specifying a key file adds a re-signing step to the test run process. Visual Studio will automatically determine which assemblies must be re-signed by using the key file that you specified. All the assemblies that can be re-signed by using that key file will be re-signed.

Note

Assemblies can be re-signed only by using a key file, not a key name.

Re-signing assemblies occurs incompletely or not at all in the following cases:

  • If the key file is not available, Visual Studio cannot re-sign the assembly. You can try to re-generate the key file and then try again to re-sign the assembly.

  • If the key file is password protected, re-signing will fail.

  • Only one key file is supported per test run. Consider, for example, a test run that references several assemblies. Some of these assemblies were signed using one key file, and others were signed using a different key file. Because you can choose only one key file, the assemblies that need the other key file will not be re-signed, and would have to be re-signed manually. If some assemblies are left unsigned, an error at the test-run level is generated. In this case, you might choose to disable signing verification, as described in Disable Signing Verification.

Disable Signing Verification

If re-signing fails—for example if the correct key file is not available—you can choose to disable signing verification, for this assembly, for the duration of your test run. To do this, use the Strong Name Tool (Sn.exe) with the –Vr option, as shown here:

SN -Vr <assembly name>

This disables strong-name verification, for the specified assembly only, on the computer on which you run the command.

Note

If you are running tests remotely and you need to disable signing verification, you must include the SN.exe command in the setup and cleanup scripts that will be run on the remote computer.

You can do this only if you have sufficient permissions.

After the test run has completed, re-enable signing verification. To do this, use the SN.exe command with the –Vu option, as shown here:

SN -Vu <assembly name>

A recommended way to disable and re-enable signing verification is to use the SN.exe commands in scripts. You can disable verification in a setup script and re-enable verification in a cleanup script.

Note

It is important that you re-enable signing verification. If signing verification remains disabled, it can cause security problems.

After you have placed the SN commands in separate script files, specify the script files on the Setup and Cleanup Scripts page of the test run configuration editor. For more information, see How to: Specify a Test Run Configuration. For information about the order in which these and other steps occur in a test run, see Test Deployment Overview.

See Also

Tasks

How to: Specify a Test Run Configuration
How to: Obtain Code Coverage Data
How to: Sign an Assembly with a Strong Name

Reference

Strong Name Tool (Sn.exe)

Concepts

Test Deployment Overview
Strong-Named Assemblies