The session demonstrates how .NET assemblies are laid out in the system. It introduces some of its secrets which you might not know and also finishes with Memory Management and Garbage Collection
2. Agenda
1. Introduction.
2. What is Assembly?
3. Features of Assembly.
4. Benefits of Assembly.
5. Contents of Assembly.
6. Strong Name.
7. Use Of Strong Name.
8. Delay Signing.
9. Categories of Assemblies.
a. Single-File Assembly
KolkataGeeks 2
3. Agenda (Contd..)
9 b. Multi-File Assembly
c. Private Assembly
d. Shared Assembly
10. How to install assembly in the GAC.
11. How to remove assembly from the GAC.
12. How to view the contents of assembly?
13. Memory Management.
14. Conditions for a Garbage Collection.
15. Managed Heap.
16. Functions Used By Garbage Collector.
KolkataGeeks 3
4. Agenda (Contd..)
17. Phases of Garbage Collection.
18. Finalize Method.
19. Dispose Method.
20. Assembly Manifest.
21. Contents of Manifest.
22. Functions of Manifest.
23. Entry Point
24. Friend Assembly.
25. InternalsVisibleTo
26. GAC & Versioning.
KolkataGeeks 4
5. Assembly
Assemblies are the building blocks of .NET Framework
applications; they form the fundamental unit of
deployment, version control, reuse, activation
scoping, and security permissions.
Assembly is a packet and it contains program and
libraries for .NET . It can be used by any program that
requires the functionality provided by the assembly.
KolkataGeeks 5
6. Versioning - Grouping of modules on the basis of same
version information.
Deployment - To group code modules and resources that
support our model of deployment.
Reuse - Modules can be grouped if they can be logically
used together for some purpose. For example, if we intend
to share our assembly with multiple application, then such
assemblies should be grouped together and signed with a
strong name.
Security - Group modules on the basis of same security
permissions.
Scoping - Group modules containing types whose visibility
should be restricted to the same assembly.
KolkataGeeks 6
7. Assembly is basically collection of exe or dll files
which are generated upon successful compilation of
the .Net application.
Assemblies can contain one or more modules.
KolkataGeeks 7
8. Features of Assembly
Assemblies are implemented as .exe or .dll
We can share an assembly by storing it in Global Assembly
Cache.
Assembly must be assigned strong named, before placing in
the Global Assembly Cache.
On requirement only, assembly are loaded into memory.
Using reflection, we can obtain information about assembly.
KolkataGeeks 8
9. Benefits
Assemblies are designed to simplify application
deployment and to solve versioning problems that
can occur with component-based applications.
KolkataGeeks 9
10. Solve Versioning Problems
Every assembly has a 128-bit version number. This
version number is presented as a set of four decimal
pieces: Major.Minor.Build.Revision
An assembly will use types from the same
assembly(name and version) from which it was built
and tested. This use of both name and version to
identify referenced assemblies avoids the "DLL Hell"
problem.
KolkataGeeks 10
11. Simplify Application Deployment
Assemblies are the natural unit of deployment. The
Windows Installer Service 2.0 can install individual
assemblies as part of a larger setup program. You can
also deploy assemblies in other ways, including by a
simple xcopy to the target system.
KolkataGeeks 11
12. Contents Of Assemblies
Generally, a static assembly consists of 4 elements:
1. Assembly Manifest
2. Type Metadata
3. MSIL
4.Resources
KolkataGeeks 12
13. Assembly Manifest
Assembly Manifest contains assembly metadata. It
contains all the metadata which are required to
specify the assembly's version requirements, security
identity, scope of the assembly and references to
resources and classes.
KolkataGeeks 13
14. It contains Assembly name, Version number, Culture,
Strong name information , list of all files in the
assembly, type reference information , information on
referenced assemblies.
KolkataGeeks 14
15. Assembly Metadata
Metadata is data which contains a detailed
description of each type, attribute within the
assembly. We can say, it is a collection of data that
describes how the elements in the assembly relate to
each other. Assembly manifest contains this assembly
metadata.
KolkataGeeks 15
16. MSIL
MSIL is an intermediate language generated by the
compiler. All .NET assemblies are represented in MSIL.
Microsoft Intermediate Language (MSIL) is a language
used as the output of a number of compilers (C#, VB, .NET,
and so forth).
The ILDasm (Intermediate Language Disassembler)
program that ships with the .NET Framework SDK
(FrameworkSDKBinildasm.exe) allows the user to see
MSIL code in human-readable format. By using this utility,
we can open any .NET executable file (EXE or DLL) and see
MSIL code.
KolkataGeeks 16
17. The ILAsm program (Intermediate Language
Assembler) generates an executable file from the
MSIL language. We can find this program in the
C:WINDOWSMicrosoft.NETFrameworkv4.0.30319.
KolkataGeeks 17
18. Creation of MSIL
Source Code
Compiles
Compiler
MSIL
Converts Into
JIT Debugger
Native Code
KolkataGeeks 18
19. Resource
Includes the resource files for the application.
KolkataGeeks 19
20. Strong Name
Strong names are cryptographic signature, which can
be assigned to assemblies. It makes the assembly
name unique.
KolkataGeeks 20
21. Strong-Name
An assemblies identity – its name, version number
and public/private key are contained in the strong
name.
Assemblies name is unique because of its strong
name. To sign an assembly with a strong name, we
have to use sn.exe utility.
Here sn.exe is the strong name generator.
KolkataGeeks 21
22. Strong name adds two bits of metadata to the
manifest:
First one is the unique number which belongs to the
authors of the assembly.
And the second one is a signed hash of the assembly,
which proves that the unique number holder has
produced the assembly. This requires a private/public
key pair. The public key provides the unique
identifying number, and the private key helps in
signing.
KolkataGeeks 22
23. Use of Strong Name
1. Versioning
2. Authentication
Versioning solves known problem called as "DLL hell".
Signed assemblies are unique and SN solves problem
with namespace collisions .(assembly with the same
file name can be distributed by the developers).
Assemblies signed with SNs are uniquely identified
and are protected.
KolkataGeeks 23
24. SN also help developers to uniquely identify versions
of their .NET assemblies.
Assemblies in the GAC (Global Assembly Cache) must
be signed to separate each publisher's namespace
and to separate each version.
KolkataGeeks 24
25. The second important feature of Strong Names is
authentication; a process where we want to ensure
ourselves about the code's origin.
On adding a strong name, the CLR is allowed to
perform verification when the assembly is loaded to
determine that it has not been tampered with since it
was compiled.
KolkataGeeks 25
26. The same situation is when SNs are used. You can
remove SNs from assemblies , but this makes no
sense because just as in the case of emails, assemblies
without SNs can't be trusted when environment is set
up to require those digital signatures or SNs.
KolkataGeeks 26
27. While developing an application that has a strong
name, all libraries it uses must also have a strong
name. We cannot refer to an assembly without a
strong name from an assembly with a strong name.
KolkataGeeks 27
28. Delay Signing
We might want to restrict access to the key pairs used
for signing assemblies.
The reasons is, if a key-pair is leaked, assembly is
tamperable.
Again if key pairs are not given to the
developers, they cannot compile and test assemblies
with their correct identity. This critical problem can be
resolved using ‘Delay Signing’.
KolkataGeeks 28
29. A delay signed assembly contains the public key and
not the private key. The developer instructs the CLR,
to bypass the validation for the delay-sign assemblies.
At the time of final deployment, the private key
holder re-signs the assembly with the real key pair.
KolkataGeeks 29
30. Delay Sign
If we specify to delay sign the assembly, the compiler
in that case, does not sign the assembly. We will also
discover that the runtime considers the assembly to
be invalid and will not load it or allow us to debug and
run it.
KolkataGeeks 30
31. Steps for Delay Signing
1. Create a key pair
Sn –k mykey.snk
2. Separate the public and the private key. Extract the
public key and keep it in its own file.
Sn –k mykey.snk myPublicKey.snk
3. Place the details in the AssemblyInfo.cs
<assembly:AssemblyKeyFileAttribute("myPublicKey.snk")>
<assembly:AssemblyDelaySignAttribute(true)>
4. To skip the signature verification for the assembly
sn -Vr mydll.dll
KolkataGeeks 31
32. 5. Just before shipping, the signing authority of the
company, signs with the actual strong name using –R
option of the Strong Name tool.
sn -R mydll.dll Testkey.snk
KolkataGeeks 32
35. Single-File Assembly
We can group all elements of an assembly in a single
physical file.
KolkataGeeks 35
36. Single File Assembly
SingleFileAssembly.dll
Assembly
Manifest
Type Metadata
MSIL
Resources
KolkataGeeks 36
37. Modules
Modules are the containers, which holds the contents
of an assembly. A module corresponds to a file,
containing the contents of an assembly.
This extra containership is helpful in case where
assembly contains multiple files.
KolkataGeeks 37
38. MultiFile Assembly
We can create a multifile assembly from several code
modules and resource files.
The elements of an assembly can be contained in
several files.
Modules of compiled code(.netmodule) and
resources(.bmp), or other files required by the
application, can be put together to make a multifile
assembly.
KolkataGeeks 38
39. In a multi-file assembly, the main module, always
contains the manifest; additional modules can contain
IL and /or resources. The manifest describes the
relative location of all the other modules that make
up the assembly.
KolkataGeeks 39
41. Here all the three files belong to an assembly.
Util.netmodule is compiled as a separate module. And
it contains no manifest. When the assembly was
created, assembly manifest was added to the
multifile.dll. This manifest indicates its relationship
with util.netmodule and graphic.bmp.
KolkataGeeks 41
43. The module parameter with the /t compiler option
indicates that the file should be compiled as a
module. With the following command, a module
named Class1.netmodule is created, which can be
added to an assembly.
KolkataGeeks 43
44. Now we have to compile modules with respect to
other modules. So let’s write the code first:
KolkataGeeks 44
46. /t:module is used to create a module, which can be
added to an assembly in future.
/addmodule: is used to reference a namespace
created in Class1.netmodule.
The compiler produces a module called
MainClient.netmodule that contains a reference to
another module, Class1.netmodule.
KolkataGeeks 46
47. Creating a multifile assembly
Class1.netmodule and MainClient.netmodule are included
in the assembly.
The /main: option specifies the assembly’s entry point.
The /out: option specifies the name of the output file,
which will contain the assembly metadata.
/target: option specifies the assembly file type.
KolkataGeeks 47
50. Private Assemblies
Assemblies which are used for each application
separately. On creation of an assembly, it is private by
default. This type of assembly cannot be used by any
other application, until and unless this assembly is
copied separately in the respective application’s
folder.
KolkataGeeks 50
51. Let us create a ClassLibrary. Then we are going to
build it. After building, .dll is created.
KolkataGeeks 51
52. Now we can find that really, .dll file has been created in the bin
folder. Now we want to use this .dll in our new console application.
KolkataGeeks 52
53. Now I have created my application.
Added the .dll by References->Browse. And as we can see, I have
also used the ClassLibrary1 on the top.
KolkataGeeks 53
54. This is the output. And it shows that the use of private
assembly.
KolkataGeeks 54
55. Separate copy of ClassLibrary1.dll has been copied here. So
this is Private Assembly.
KolkataGeeks 55
56. Shared Assembly
Shared assembly can be used in situations where it is
not necessary to install a version of an assembly for
each application that uses it.
KolkataGeeks 56
57. Shared Assembly
To make a shared assembly, we require it to store in
Global Assembly Cache GAC. GAC is reserved for
storing assemblies and for sharing them in between
different applications.
Here every assembly must have an unique name. So
we need to assign strong name to assemblies before
storing it in the GAC.
KolkataGeeks 57
63. Here no separate copy of assembly is pasted in the
ConsoleApplication3bindebug folder. Instead here
the shared assembly from the GAC has been used.
Common Copy of Shared Assembly has been used
here.
KolkataGeeks 63
64. Remove an assembly from GAC
We have to use Global Assembly Cache
tool(gacutil.exe) to remove an assembly from the
GAC.
Syntax:
At the Command prompt:
Gacutil /u assemblyname
Here /u means uninstall.
And assemblyname is the name of the assembly,
which we want to remove from the GAC.
KolkataGeeks 64
65. View the contents of assembly
Let’s write some code and compile it.
Assembly will be created.
Type a command ildasm from the command prompt.
An application will be open, name Intermediate
Language Disassembler.
This application opens our assembly and shows all
classes, methods , data fields, global and local
application data.
KolkataGeeks 65
66. To view the contents of a manifest for a particular
assembly, we can use the ILDassembler(ILDasm.exe),
which is part of the .NET Framework SDK.
KolkataGeeks 66