다음을 통해 공유


Strong Names

The solution to the problems just described is to more strongly associate a distinct build of a component assembly with the client application. This distinct build is indicated by the combination of a version number and a special value, called the originator. The system can then isolate these component assemblies so that different versions can be running at the same time for different client applications — something that was not possible in the past. This system of protection is sometimes called side-by-side execution (in contrast to backward compatible execution), because applications can run alongside other versions of the same applications without affecting their respective execution environments.

The code to demonstrate adding these additional build attributes, and creating a protected shareable component, can be found in the 4_Shared subdirectory. Building on the code in 3_SimplePath, this step adds a second, shareable component that reverses an array of strings.

First, you will build this new component assembly without specifying any of the options that would make it shareable. If you simply compile the new Reverser.dll component using, for example, BuildNoShare.bat (located in the \4_Shared\ subdirectory), you can then examine the metadata using Ildasm.exe, which shows that the assembly is lacking an originator and does not have an established version number:

.assembly Reverser
{  ...
  .hash algorithm 0x00008004
  .ver 0:0:0:0
}

To mark an assembly as shareable, you have to compile it using a private key. (Public keys are used for verification.) So before compiling, you have to generate a public-private key pair. You use the Strong Name tool (Sn.exe) to generate a new key pair and place it in a file (found in the \4_Shared\Reverser subdirectory):

sn –k orgKey.snk

Now that you have a private key, you are ready to compile the component, specifying the key file and the version number to be assigned. You can do this by specifying AssemblyVersion and AssemblyKeyFile attributes in the Reverser.cs file using the STRONG conditional compilation symbol:

#if STRONG
[assembly: System.Reflection.AssemblyVersion("1.0.0.0")]
[assembly: System.Reflection.AssemblyKeyFile("orgKey.snk")]
#endif

You must then define STRONG in the compile process:

csc.exe /define:STRONG ... Reverser.cs

If you run Ildasm.exe again on Reverser.dll, you can verify that the assembly is now shareable, as indicated by the presence of a .publickey property and a .ver property that does not contain the default value of 1.0.0.0:

.assembly Reverser
{  ...
  .publickey = (00 ... FC 4A DC 9B 9C)
  .hash algorithm 0x00008004
  .ver 1:0:0:0
}

For more information on the Strong Name tool (SN.exe), see the section on that utility in Appendix B: Packaging And Deployment Tools in this tutorial.

See Also

Deploying Shared Components | (5) Component Versioning | Packaging and Deployment Summary | Appendix A: Additional Packaging and Deployment Information | Appendix B: Packaging and Deployment Tools