secrets of .net assemblies and memory management
DESCRIPTION
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 CollectionTRANSCRIPT
Building Blocks of .NET Framework
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
Agenda
KolkataGeeks 2
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.
Agenda (Contd..)
KolkataGeeks 3
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.
Agenda (Contd..)
KolkataGeeks 4
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.
Assembly
KolkataGeeks 5
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
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
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.
Features of Assembly
KolkataGeeks 8
Assemblies are designed to simplify application deployment and to solve versioning problems that can occur with component-based applications.
Benefits
KolkataGeeks 9
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.
Solve Versioning Problems
KolkataGeeks 10
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.
Simplify Application Deployment
KolkataGeeks 11
Generally, a static assembly consists of 4 elements:
1. Assembly Manifest 2. Type Metadata 3. MSIL 4.Resources
Contents Of Assemblies
KolkataGeeks 12
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.
Assembly Manifest
KolkataGeeks 13
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
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.
Assembly Metadata
KolkataGeeks 15
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 (FrameworkSDK\Bin\ildasm.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.
MSIL
KolkataGeeks 16
The ILAsm program (Intermediate Language Assembler) generates an executable file from the MSIL language. We can find this program in the C:\WINDOWS\Microsoft.NET\Framework\v4.0.30319.
KolkataGeeks 17
Creation of MSIL
MSIL
Source Code
CompilerCompiles
JIT DebuggerConverts Into
Native Code
KolkataGeeks 18
Includes the resource files for the application.
Resource
KolkataGeeks 19
Strong names are cryptographic signature, which can be assigned to assemblies. It makes the assembly name unique.
Strong Name
KolkataGeeks 20
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.
Strong-Name
KolkataGeeks 21
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
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.
Use of Strong Name
KolkataGeeks 23
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
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
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
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
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’.
Delay Signing
KolkataGeeks 28
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
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.
Delay Sign
KolkataGeeks 30
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
Steps for Delay Signing
KolkataGeeks 31
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
Assembly
Grouping Of Elements Way Of Accessibility
Categories of Assemblies
KolkataGeeks 33
Grouping of Elements
Single File Assembly Multi-File Assembly
KolkataGeeks 34
We can group all elements of an assembly in a single physical file.
Single-File Assembly
KolkataGeeks 35
SingleFileAssembly.dll
Assembly Manifest
Type Metadata
MSIL
Resources
Single File Assembly
KolkataGeeks 36
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.
Modules
KolkataGeeks 37
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.
MultiFile Assembly
KolkataGeeks 38
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
Multi-File Assembly
• Assembly Manifest
• Type Metadata• MSIL Code
• Type Metadata• MSIL Code
• Resources
MultiFile.dll
Util.netmodule
Graphic.bmp
KolkataGeeks 40
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
How to create mulifile assembly
KolkataGeeks 42
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
Now we have to compile modules with respect to other modules. So let’s write the code first:
KolkataGeeks 44
KolkataGeeks 45
/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
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
Multifile Assembly Created
KolkataGeeks 48
Way of Accessibility
Private Shared
KolkataGeeks 49
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.
Private Assemblies
KolkataGeeks 50
Let us create a ClassLibrary. Then we are going to build it. After building, .dll is
created.
KolkataGeeks 51
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
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
This is the output. And it shows that the use of private assembly.
KolkataGeeks 54
Separate copy of ClassLibrary1.dll has been copied here. So this is Private Assembly.
KolkataGeeks 55
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.
Shared Assembly
KolkataGeeks 56
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.
Shared Assembly
KolkataGeeks 57
GAC(shared
assembly)
Application1
Application2
Application3
Application4
Pictorial representaion of Shared Assembly
KolkataGeeks 58
Each assembly is assigned a strong name.
Assigning Strong NameKolkataGeeks 59
How to install .dll into GAC
Adding .dll in the GAC
KolkataGeeks 60
Here our shared assembly is located.
Location of Shared Assembly
KolkataGeeks 61
Using Shared Assembly
KolkataGeeks 62
Here no separate copy of assembly is pasted in the ConsoleApplication3\bin\debug folder. Instead here the shared assembly from the GAC has been used.
Common Copy of Shared Assembly has been used here.
KolkataGeeks 63
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.
Remove an assembly from GAC
KolkataGeeks 64
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.
View the contents of assembly
KolkataGeeks 65
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
KolkataGeeks 67
Type ildasm
KolkataGeeks 68
Ildasm window opens. File->Open->Browse for our .dll file.
KolkataGeeks 69
KolkataGeeks 70