Building Blocks of .NET      Framework
Agenda               1. Introduction.               2. What is Assembly?               3. Features of Assembly.           ...
Agenda (Contd..)               9 b. Multi-File Assembly                  c. Private Assembly                  d. Shared As...
Agenda (Contd..)               17. Phases of Garbage Collection.               18. Finalize Method.               19. Disp...
Assembly               Assemblies are the building blocks of .NET Framework               applications; they form the fund...
Versioning - Grouping of modules on the basis of same               version information.               Deployment - To gro...
Assembly is basically collection of exe or dll files               which are generated upon successful compilation of     ...
Features of Assembly      Assemblies are implemented as .exe or .dll      We can share an assembly by storing it in Global...
Benefits               Assemblies are designed to simplify application               deployment and to solve versioning pr...
Solve Versioning Problems               Every assembly has a 128-bit version number. This               version number is ...
Simplify Application Deployment               Assemblies are the natural unit of deployment. The               Windows Ins...
Contents Of Assemblies               Generally, a static assembly consists of 4 elements:               1. Assembly Manife...
Assembly Manifest               Assembly Manifest contains assembly metadata. It               contains all the metadata w...
It contains Assembly name, Version number, Culture,               Strong name information , list of all files in the      ...
Assembly Metadata               Metadata is data which contains a detailed               description of each type, attribu...
MSIL               MSIL is an intermediate language generated by the               compiler. All .NET assemblies are repre...
The ILAsm program (Intermediate Language               Assembler) generates an executable file from the               MSIL...
Creation of MSIL                    Source Code                          Compiles                                         ...
Resource               Includes the resource files for the application.KolkataGeeks                             19
Strong Name               Strong names are cryptographic signature, which can               be assigned to assemblies. It ...
Strong-Name               An assemblies identity – its name, version number               and public/private key are conta...
Strong name adds two bits of metadata to the               manifest:               First one is the unique number which be...
Use of Strong Name               1. Versioning               2. Authentication               Versioning solves known probl...
SN also help developers to uniquely identify versions               of their .NET assemblies.               Assemblies in ...
The second important feature of Strong Names is               authentication; a process where we want to ensure           ...
The same situation is when SNs are used. You can               remove SNs from assemblies , but this makes no             ...
While developing an application that has a strong               name, all libraries it uses must also have a strong       ...
Delay Signing               We might want to restrict access to the key pairs used               for signing assemblies.  ...
A delay signed assembly contains the public key and               not the private key. The developer instructs the CLR,   ...
Delay Sign               If we specify to delay sign the assembly, the compiler               in that case, does not sign ...
Steps for Delay Signing               1. Create a key pair               Sn –k mykey.snk               2. Separate the pub...
5. Just before shipping, the signing authority of the               company, signs with the actual strong name using –R   ...
Categories of AssembliesKolkataGeeks              33
KolkataGeeks   34
Single-File Assembly               We can group all elements of an assembly in a single               physical file.Kolkat...
Single File Assembly                  SingleFileAssembly.dll                                             Assembly         ...
Modules               Modules are the containers, which holds the contents               of an assembly. A module correspo...
MultiFile Assembly               We can create a multifile assembly from several code               modules and resource f...
In a multi-file assembly, the main module, always               contains the manifest; additional modules can contain     ...
Multi-File Assembly         MultiFile.dll                                 Util.netmodule                                 •...
Here all the three files belong to an assembly.               Util.netmodule is compiled as a separate module. And        ...
How to create mulifile assemblyKolkataGeeks           42
The module parameter with the /t compiler option               indicates that the file should be compiled as a            ...
Now we have to compile modules with respect to               other modules. So let’s write the code first:KolkataGeeks    ...
KolkataGeeks   45
/t:module is used to create a module, which can be               added to an assembly in future.               /addmodule:...
Creating a multifile assembly           Class1.netmodule and MainClient.netmodule are included           in the assembly. ...
Multifile Assembly CreatedKolkataGeeks               48
KolkataGeeks   49
Private Assemblies               Assemblies which are used for each application               separately. On creation of a...
Let us create a ClassLibrary. Then we are going to                     build it. After building, .dll is created.KolkataGe...
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 con...
Now I have created my application.    Added the .dll by References->Browse. And as we can see, I have    also used the Cla...
This is the output. And it shows that the use of private                                      assembly.KolkataGeeks       ...
Separate copy of ClassLibrary1.dll has been copied here. So                         this is Private Assembly.KolkataGeeks ...
Shared Assembly               Shared assembly can be used in situations where it is               not necessary to install...
Shared Assembly               To make a shared assembly, we require it to store in               Global Assembly Cache GAC...
Pictorial representaion of Shared Assembly                                     Application1                               ...
Each assembly is assigned a strong name.                Assigning Strong NameKolkataGeeks                           59
Adding .dll in the GAC               How to install .dll into GACKolkataGeeks                             60
Location of Shared Assembly               Here our shared assembly is located.KolkataGeeks                            61
Using Shared AssemblyKolkataGeeks             62
Here no separate copy of assembly is pasted in the               ConsoleApplication3bindebug folder. Instead here         ...
Remove an assembly from GAC               We have to use Global Assembly Cache               tool(gacutil.exe) to remove a...
View the contents of assembly               Let’s write some code and compile it.               Assembly will be created. ...
To view the contents of a manifest for a particular               assembly, we can use the ILDassembler(ILDasm.exe),      ...
KolkataGeeks   67
Type ildasmKolkataGeeks        68
Ildasm window opens. File->Open->Browse for our .dll               file.KolkataGeeks                           69
KolkataGeeks   70
Upcoming SlideShare
Loading in …5
×

Secrets of .NET Assemblies and Memory Management

2,668 views

Published on

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

Published in: Technology, Spiritual
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total views
2,668
On SlideShare
0
From Embeds
0
Number of Embeds
1
Actions
Shares
0
Downloads
196
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

Secrets of .NET Assemblies and Memory Management

  1. 1. Building Blocks of .NET Framework
  2. 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 AssemblyKolkataGeeks 2
  3. 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. 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. 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. 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. 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. 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. 9. Benefits Assemblies are designed to simplify application deployment and to solve versioning problems that can occur with component-based applications.KolkataGeeks 9
  10. 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. 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. 12. Contents Of Assemblies Generally, a static assembly consists of 4 elements: 1. Assembly Manifest 2. Type Metadata 3. MSIL 4.ResourcesKolkataGeeks 12
  13. 13. Assembly Manifest Assembly Manifest contains assembly metadata. It contains all the metadata which are required to specify the assemblys version requirements, security identity, scope of the assembly and references to resources and classes.KolkataGeeks 13
  14. 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. 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. 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. 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. 18. Creation of MSIL Source Code Compiles Compiler MSIL Converts Into JIT Debugger Native CodeKolkataGeeks 18
  19. 19. Resource Includes the resource files for the application.KolkataGeeks 19
  20. 20. Strong Name Strong names are cryptographic signature, which can be assigned to assemblies. It makes the assembly name unique.KolkataGeeks 20
  21. 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. 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. 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. 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 publishers namespace and to separate each version.KolkataGeeks 24
  25. 25. The second important feature of Strong Names is authentication; a process where we want to ensure ourselves about the codes 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. 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 cant be trusted when environment is set up to require those digital signatures or SNs.KolkataGeeks 26
  27. 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. 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. 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. 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. 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.dllKolkataGeeks 31
  32. 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.snkKolkataGeeks 32
  33. 33. Categories of AssembliesKolkataGeeks 33
  34. 34. KolkataGeeks 34
  35. 35. Single-File Assembly We can group all elements of an assembly in a single physical file.KolkataGeeks 35
  36. 36. Single File Assembly SingleFileAssembly.dll Assembly Manifest Type Metadata MSIL ResourcesKolkataGeeks 36
  37. 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. 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. 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
  40. 40. Multi-File Assembly MultiFile.dll Util.netmodule • Type Metadata • Assembly Manifest • MSIL Code • Type Metadata • MSIL Code Graphic.bmp • ResourcesKolkataGeeks 40
  41. 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
  42. 42. How to create mulifile assemblyKolkataGeeks 42
  43. 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. 44. Now we have to compile modules with respect to other modules. So let’s write the code first:KolkataGeeks 44
  45. 45. KolkataGeeks 45
  46. 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. 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
  48. 48. Multifile Assembly CreatedKolkataGeeks 48
  49. 49. KolkataGeeks 49
  50. 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. 51. Let us create a ClassLibrary. Then we are going to build it. After building, .dll is created.KolkataGeeks 51
  52. 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. 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. 54. This is the output. And it shows that the use of private assembly.KolkataGeeks 54
  55. 55. Separate copy of ClassLibrary1.dll has been copied here. So this is Private Assembly.KolkataGeeks 55
  56. 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. 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
  58. 58. Pictorial representaion of Shared Assembly Application1 GAC Application4 Application2 (shared assembly) Application3KolkataGeeks 58
  59. 59. Each assembly is assigned a strong name. Assigning Strong NameKolkataGeeks 59
  60. 60. Adding .dll in the GAC How to install .dll into GACKolkataGeeks 60
  61. 61. Location of Shared Assembly Here our shared assembly is located.KolkataGeeks 61
  62. 62. Using Shared AssemblyKolkataGeeks 62
  63. 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. 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. 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. 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
  67. 67. KolkataGeeks 67
  68. 68. Type ildasmKolkataGeeks 68
  69. 69. Ildasm window opens. File->Open->Browse for our .dll file.KolkataGeeks 69
  70. 70. KolkataGeeks 70

×