Assemblies
Objectives
•
Introduce assemblies – concept – creation – naming
• Discuss deployment
– main .exe – dependent assemblies
2
Assembly
• An assembly is a logical group of types
– unit of deployment, versioning, and security in the CLR
Invest
assembly of financial types
Stock
Bond
Advisor
3
Assembly contents
• Assembly contains several parts
– IL – description of contained types in the metadata – description of assembly in the manifest
IL
code for Invest, Stock, Bond, Advisor classes
metadata
description of Invest, Stock, Bond, Advisor classes
manifest
.ver 1.2.3.4 .publickey ...
4
Type access
• Two access levels available for types
– internal allows access only within same assembly (default) – public allows access from all assemblies
available only in assembly
internal class Advisor {
...
}
available in all assemblies
public class Stock {
...
}
5
Member access
• Can make type member internal
– allows access only within same assembly
public class Stock {
available only in assembly
internal string name; ...
}
6
Single file assembly
• Assembly can be single dll or exe file
– simple to create – simple to deploy – most common case
Finance.dll
single file assembly
Invest
Stock
Bond
Advisor
7
Assembly creation
• Compiler outputs assembly when target type is:
– exe – winexe – library
Invest.cs
Stock.cs Finance.dll
compiler
Bond.cs
Advisor.cs
source files
assembly
csc /target:library
/out:Finance.dll Invest.cs Stock.cs Bond.cs Advisor.cs
8
Module
• Can create module with compiler /target:module switch
– does not create assembly – typically has .netmodule extension
Stock.cs
Assets.netmodule
compiler
Bond.cs
source files
module
csc /target:module
/out:Assets.netmodule Stock.cs Bond.cs
9
Multi-file assembly
• Can create multi-file assembly
– use /addmodule compiler switch to add module to assembly – can also use more powerful assembly linker al.exe
Invest.cs Assets.netmodule
compiler
Advisor.cs Finance.dll
additional source files
two files, one assembly
csc /target:library
/out:Finance.dll /addmodule:Assets.netmodule Invest.cs Advisor.cs
10
Assembly identity
• Assembly identified by four attributes
– name – culture – version – public key token
11
Name
• Assembly name based on name of file containing manifest
– set automatically when assembly created
Finance.dll
compiler
assembly name is Finance
csc /target:library
/out:Finance.dll ...
12
Culture
• Assembly can specify optional target culture
– typical to have separate assembly for each supported culture – assembly will be culture neutral if culture not specified
• Culture typically specified using AssemblyCulture attribute
– in System.Reflection namespace
[assembly: AssemblyCulture("fr-CA")]
French-Canada
13
Version
• Assembly can be given a four part version number
– major version – minor version – build number – revision
• Version typically specified using AssemblyVersion attribute
– in System.Reflection namespace
[assembly: AssemblyVersion("1.2.3.4")]
major.minor.build.revision
14
Public key token
• Public/private key pair used to give assembly a strong name
– used in deployment to provide unique identity – hash of public key called token used since public key is long
• Procedure:
– generate public/private key pair with strong name tool sn.exe – apply to assembly with AssemblyKeyFile attribute – attribute is in System.Reflection namespace
sn -k Lib.snk
generate keys, specify output file
[assembly: AssemblyKeyFile("Lib.snk")]
apply keys to assembly
15
Dependent assemblies
• Typical for application to consist of multiple assemblies
– main .exe and supporting .dlls
App.exe
Finance.dll
Invest
Stock Main
Bond
main .exe
supporting .dll
Advisor
16
Compile time assembly reference
• Can use type defined in other assembly
– must compile with option /r[eference]:
• Compiler searches for referenced assemblies
– current directory – CLR system directory – directories specified by /lib:dir1[,dir2...] switch – directories specified by LIB environment variable
App.cs App.exe
compiler
csc /target:exe
/out:App.exe /reference:Finance.dll App.cs
17
Dependent assembly reference
•
Identity of dependent assemblies recorded in manifest – information used during runtime assembly binding – ensures binding to same assembly at runtime
App.exe
.assembly extern Finance {
dependent assembly recorded
.publickeytoken ... .ver 1.2.3.4
}
...
18
Deployment
• Several actors involved in deployment
– main .exe – dependent .dlls – main deployment directory – configuration file
19
AppBase
• Directory containing main .exe called AppBase
C:\
Program Files
AppBase
MyApplication App.exe
20
Application configuration file
• Application can have configuration file – used to help manage deployment – file name is application name with .config added – placed in AppBase
App.exe.config file in AppBase directory
...
21
Deploying dependent assemblies
• Several options for deployment of dependent assemblies
– AppBase – directory below AppBase specified in config file – local or remote location specified by URI in config file – global assembly cache
22
AppBase deployment
• AppBase automatically searched for needed assemblies
C:\
Program Files
AppBase
MyApplication
dependent assembly
App.exe Finance.dll
23
Private path
• Can specify subdirectories of AppBase be searched
– use probing privatePath in config file
C:\
Program Files
AppBase
MyApplication
App.exe App.exe.config
MyAssemblies
Finance.dll
dependent assembly
24
Codebase
• Can specify arbitrary URI for strongly named assembly
– use codeBase element in config file – useful to redirect to new version of needed assembly
C:\
Program Files
MyAssemblies MyApplication
not under AppBase dependent assembly
Finance.dll App.exe App.exe.config
25
Global assembly cache (gac)
• Strongly named assembly can go in global assembly cache
– typically stored under C:\Windows\assembly – need admin privileges to install – automatically searched for needed assemblies – use gacutil tool to manipulate cache • Convenient way to store shared assembly
– but complicates deployment, backup, and uninstall
gacutil /i Finance.dll install
gacutil /u Finance,Version=1.2.3.4,PublicKeyToken=362cfdd636584143 uninstall
26
Summary
• Assembly is the component of the CLR – unit of deployment and versioning
• Assembly identity made of four components
– name – culture – version – public key token
• Application typically made of several assemblies
– must reference dependent assemblies at compile time – several options for runtime assembly deployment
27