Documente Academic
Documente Profesional
Documente Cultură
Peter Drayton
peter@develop.com
1
Assemblies and the .NET Framework
2
Assemblies defined
3
Figure 1: Access modifiers
C# VB.NET Meaning
4
Assemblies and modules
5
Figure 2: Modules and Assemblies
/t:module /t:library
.module pete.netmodule .module pete.dll
.assembly extern mscorlib .assembly extern mscorlib
.assembly extern paul .assembly extern paul
.class public PP { .class public PP {
.field [paul]Bassist m .field [paul]Bassist m
} }
.assembly pete Manifest
6
Figure 3: A multi-module assembly
/t:library /addmodule:pete.netmodule,george.netmodule
.module band.dll /t:module
.assembly extern mscorlib .module pete.netmodule
.assembly extern paul
.assembly extern john .assembly extern mscorlib
.assembly extern paul
.class public Manager {
} .class public PP {
.field [paul]Bassist m
.assembly band }
.file pete.netmodule /t:module
.file george.netmodule
.module george.netmodule
Manifest
7
Figure 4: Multi-module assemblies using CSC.EXE
Component Assembly
csc.exe /t:library
component.cs component.dll
/addmodule:code.netmodule
Application Assembly
8
Figure 5: Multi-module assemblies using CSC.EXE and NMAKE
9
Public Keys and Assemblies
10
Figure 6: Managing public/private keys using SN.EXE
sn.exe -k publicprivate.snk
Public Key
Private Key
publicprivate.snk (128 bytes + 32
(436 bytes)
byte header)
Public Key
public.snk (128 bytes + 32
byte header)
sn.exe -t public.snk
11
Figure 7: Strong assembly references
mylib.cs
publicprivate.snk
using System.Reflection;
Public Key
[assembly: AssemblyKeyFile("publicprivate.snk")]
(128 bytes + 32
[assembly: AssemblyDelaySign(false)] byte header)
myapp.exe
mylib
Public Key Token (8 bytes)
Assembly Reference
PE/COFF
Header CLR Header Code
12
Figure 8: Delay signing an assembly
mylib.cs
using System.Reflection;
[assembly: AssemblyKeyFile("public.snk")]
[assembly: AssemblyDelaySign(true)]
public.snk
Public Key
csc /t:library mylib.cs (128 bytes + 32
byte header)
mylib.dll
Public Key Space
(128 bytes + 32 For publicprivate.snk
byte header) Signature Public Key
PE/COFF (128 bytes + 32
Header CLR Header Code byte header)
Private Key
sn -R mylib.dll publicprivate.snk (436 bytes)
mylib.dll
Public Key
(128 bytes + 32 Signature
byte header)
PE/COFF
Header CLR Header Code
13
Assembly names
• All assemblies have a four-part name that uniquely identifies the locale
and developer of the component
– The simple Name typically corresponds to the file name (no
extension)
– The Version identifies the major/minor/build/revision numbers
– The (optional) CultureInfo corresponds to language and region
– The (optional) Originator/PublicKey identifies the developer
– Display names are stringified assembly names suitable for human
entry
– Note: Namespace prefixes of types may or may not match the
Name of the assembly
14
Figure 9: Inside the AssemblyVersion attribute
Attribute Actual
Parameter Value
1 1.0.0.0
1.2 1.2.0.0 * where d is the
number of days
1.2.3 1.2.3.0 since Feb. 1, 2000
1.2.3.4 1.2.3.4 and s is the
number of seconds
1.2.* 1.2.d.s since midnight /2
1.2.3.* 1.2.3.s
<absent> 0.0.0.0
15
Figure 10: Fully specified assembly names
using System.Reflection;
[assembly: AssemblyVersion("1.2.3.4") ]
[assembly: AssemblyCulture("en-US") ] // resource-only assm
[assembly: AssemblyKeyFile("acmecorp.snk") ]
16
Loading and resolving assemblies
17
Figure 11: Loading an assembly with an explicit CODEBASE
using System;
using System.Reflection;
18
Figure 12: Assembly resolution and loading
Assembly.Load(name,culture,version,token) AppDomain.BaseDirectory
APPBASE <bindingRedirect>
POLICY <codeBase>
Assembly
Resolver
CODEBASE <probing>
PRIVATE_BINPATH
Assembly
Loader
Loaded Assembly
19
Figure 13: Loading an assembly using the assembly resolver
using System;
using System.Reflection;
20
Version policy
21
Figure 14: Assembly resolver configuration file format
configuration
0..1 Element (xmlns="") Attribute
assemblyBinding
privatePath apply
name
assemblyIdentity publicKeyToken
0..1
culture
0..N version
codeBase
href
0..N oldVersion
bindingRedirect
newVersion
0..1
publisherPolicy apply
22
Figure 15: Setting the version policy
23
Figure 16: Setting the application to safe-mode
24
The assembly cache
25
Figure 17: Global Assembly Cache
26
Assembly resolving via CODEBASE or probing
27
Figure 18: Specifying the codebase using configuration files
28
Figure 19: APPBASE and the relative search path
m
Main.EXE
(APPBASE)
C:\
n o q
Eligible Ineligable
Directories Directories p
C:\m C:\
C:\m\o C:\n
C:\m\o\q
C:\m\p
29
Figure 20: Setting the relative search path
30
Figure 21: Culture-neutral probing
31
Figure 22: Culture-dependent probing
32
Figure 23: Assembly resolution
Apply Version Policy
(if reference fully specified)
Is file found
<codeBase> N
N via probing?
hint provided?
Y
Y
33
Versioning hazards
34
Summary
35
Questions?
36