1.9 Give an Assembly a Strong Name


1.9 Give an Assembly a Strong Name

Problem

You need to give an assembly a strong name so that it

  • Has a unique identity, which allows people to assign specific permissions to the assembly when configuring code access security policy.

  • Can't be modified and passed off as your original assembly.

  • Supports versioning and version policy.

  • Can be shared across multiple applications, and installed in the global assembly cache (GAC).

Solution

Use assembly-level attributes to specify the location of your strong name key pair and optionally a version number and culture for your assembly. The compiler will strong name your assembly as part of the build process.

Discussion

To strong name an assembly using the C# compiler, you need the following:

  • A strong name key pair contained either in a file or in a CSP key container. (Recipe 1.8 discusses the creation of strong name key pairs.)

  • To use assembly-level attributes to specify the location where the compiler can obtain your strong name key pair.

    • If your key pair is in a file, apply the attribute System.Reflection.AssemblyKeyFileAttribute to your assembly and specify the name of the file that contains the keys.

    • If your key pair is in a CSP container, apply the attribute System.Reflection.AssemblyKeyNameAttribute to your assembly and specify the name of the container in which the keys are stored.

Optionally, you can also

  • Specify the culture that your assembly supports by applying the attribute System.Reflection.AssemblyCultureAttribute to the assembly. (You can't specify a culture for executable assemblies because executable assemblies support only the neutral culture.)

  • Specify the version of your assembly by applying the attribute System.Reflection.AssemblyVersionAttribute to the assembly.

The code that follows (from a file named HelloWorld.cs) shows the use of attributes (shown in boldface text) to specify the keys, the culture, and the version for the assembly.

 using System; using System.Reflection;  [assembly:AssemblyKeyName("MyKeys")]   [assembly:AssemblyCulture("")]   [assembly:AssemblyVersion("1.0.0.0")]  public class HelloWorld {     public static void Main() {              Console.WriteLine("Hello, world");     } } 

To create a strong-named assembly from the example code, create the strong name keys and store them in a file named MyKeyFile using the command sn -k MyKeyFile.snk . Then install the keys into the CSP container named MyKeys using the command sn -i MyKeyFile.snk MyKeys . You can now compile the HelloWorld.cs file into a strong-named assembly using the command csc HelloWorld.cs .

Note  

You can also build strong-named assemblies using the Assembly Linker (al.exe), which allows you to specify the strong name information on the command line instead of using attributes in your code. This is useful when you don't want to embed the strong name attributes in your source file and when you use scripts to build large source trees. Refer to the Assembly Linker information in the .NET Framework SDK documentation for more details.




C# Programmer[ap]s Cookbook
C# Programmer[ap]s Cookbook
ISBN: 735619301
EAN: N/A
Year: 2006
Pages: 266

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net