Understanding strong assembly names

The output of Listing 31-1 may seem a bit confusing at first, as it lists more than just the assembly name, which in this case is named mscorlib. This output actually defines four pieces of information for the assembly name:

• The name itself (mscorlib)

• Culture information (neutral)

• A public key token (b77a5c561934e089)

At a minimum, all assemblies contain a name, a version, and a culture. .However, assemblies can contain a public key. An assembly containing all four pieces of information is said to be strongly named.

Only assemblies that contain strong names can be stored in the global assembly cache. The global assembly cache (GAC) is a disk-based collection of .NET assemblies that can be accessed by any piece of .NET code on the machine containing the GAC. The .NET Framework looks for an assembly in the entry assembly directory when an assembly needs to be loaded. This deployment scheme is simple; however, it can create several copies of an assembly on a disk volume for heavily used assemblies, as each assembly needs to be copied to each entry assembly that needs the referenced assembly. The .NET Framework includes the GAC to simplify things, so that heavily used assemblies, such as the assemblies that ship with the .NET Framework, can be placed on a machine once and referenced many times. The .NET Framework checks the GAC when searching for an assembly.

When the .NET Framework is installed on a machine, the setup process installs a Windows Explorer shell extension that makes the GAC appear as a standard Windows folder. The base Windows directory, which is C:\WINDOWS on most machines, contains a folder called assembly on machines with the .NET Framework installed. This folder shows the contents of the GAC, with strong name information in columns, as shown in Figure 31-2.

QCiMWW«iii iii

QCiMWW«iii iii

îfrj —nrf ■>>k>-v-\ ci lÎBI-l •fK' j^yrrr K

îfrj —nrf ■>>k>-v-\ ci afewW f 4P. ■-•-■■■■ -

ifcir^

T.«« W ctfkr*

i,E.U»d

?C MWO

knfWiiiniM

LEuU

a r.'&Msa.: ■ ! JL."

* ! Üöiö

M WIi^'»! I 4

« in»rt

« i.UHd

«HUCtlUH

? 1 ?jcwa

♦M^rtdîiiijr«

-S.i.U»d

1ÄM

UuU

>3 SI ! JL"

14* J

MW^'V! 1.4

IDJPJ

•mniWlPl M

INJ

«p.^hiü-uL?:-«

wiViiiKM*

'HM iMpi 3 £.U»d

bM^iT-iurKfcS*

K.3QW0

«V^ll fVM*

7£.U»d

burarijjiuit

wjfy^unawt

ir.li» a

wiwrnsiCftii

if irad

EdtftfT-nvtiaU

!cn»a

bJ^^llflCMf

ïC.UCttd

FC M»d

WJi-y^lliWelt

7£.U»d

wanra'udha«

ri.m a

Figure 31-2: Viewing the GAC as a Windows folder

You can place assemblies with different strong names side by side in the global assembly cache, even if the segment names match. For example, version 1.0.0.0 of an assembly named assembly.dll can be installed in the global assembly cache along with version 2.0.0.0 of an assembly also named assembly.dll. Code that references a strongly named assembly has the assembly's strong name listed in the manifest and always binds to the assembly with that strong name, even if other assemblies with some components of the strong name are the same. If an entry assembly references version 1.0.0.0 of assembly.dll, for example, the .NET Framework always loads version 1.0.0.0 of assembly.dll into the process space of the entry assembly, even if other versions of assembly.dll exist in the GAC.

Setting the information that comprises a strong name for an assembly is as easy as adding some attributes to one of the source code files for a project. These attributes can be added to a source file containing C# code or can be added to a separate file containing only the attributes themselves.

Note Visual Studio .NET adds a source file called AssemblyInfo.cs to new C# projects and places attributes needed for strongly named assemblies into that source file. The filename can be changed once it is created and will still work, as long as the renamed file remains a part of the project build.

0 0

Post a comment