secrets of .net assemblies and memory management

70
Building Blocks of .NET Framework

Upload: abhishek-sur

Post on 10-May-2015

2.012 views

Category:

Technology


0 download

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 Collection

TRANSCRIPT

Page 1: Secrets of .NET Assemblies and Memory Management

Building Blocks of .NET Framework

Page 2: Secrets of .NET Assemblies and Memory Management

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

Page 3: Secrets of .NET Assemblies and Memory Management

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

Page 4: Secrets of .NET Assemblies and Memory Management

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

Page 5: Secrets of .NET Assemblies and Memory Management

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

Page 6: Secrets of .NET Assemblies and Memory Management

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

Page 7: Secrets of .NET Assemblies and Memory Management

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

Page 8: Secrets of .NET Assemblies and Memory Management

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

Page 9: Secrets of .NET Assemblies and Memory Management

Assemblies are designed to simplify application deployment and to solve versioning problems that can occur with component-based applications.

Benefits

KolkataGeeks 9

Page 10: Secrets of .NET Assemblies and Memory Management

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

Page 11: Secrets of .NET Assemblies and Memory Management

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

Page 12: Secrets of .NET Assemblies and Memory Management

Generally, a static assembly consists of 4 elements:

1. Assembly Manifest 2. Type Metadata 3. MSIL 4.Resources

Contents Of Assemblies

KolkataGeeks 12

Page 13: Secrets of .NET Assemblies and Memory Management

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

Page 14: Secrets of .NET Assemblies and Memory Management

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

Page 15: Secrets of .NET Assemblies and Memory Management

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

Page 16: Secrets of .NET Assemblies and Memory Management

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

Page 17: Secrets of .NET Assemblies and Memory Management

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

Page 18: Secrets of .NET Assemblies and Memory Management

Creation of MSIL

MSIL

Source Code

CompilerCompiles

JIT DebuggerConverts Into

Native Code

KolkataGeeks 18

Page 19: Secrets of .NET Assemblies and Memory Management

Includes the resource files for the application.

Resource

KolkataGeeks 19

Page 20: Secrets of .NET Assemblies and Memory Management

Strong names are cryptographic signature, which can be assigned to assemblies. It makes the assembly name unique.

Strong Name

KolkataGeeks 20

Page 21: Secrets of .NET Assemblies and Memory Management

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

Page 22: Secrets of .NET Assemblies and Memory Management

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

Page 23: Secrets of .NET Assemblies and Memory Management

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

Page 24: Secrets of .NET Assemblies and Memory Management

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

Page 25: Secrets of .NET Assemblies and Memory Management

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

Page 26: Secrets of .NET Assemblies and Memory Management

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

Page 27: Secrets of .NET Assemblies and Memory Management

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

Page 28: Secrets of .NET Assemblies and Memory Management

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

Page 29: Secrets of .NET Assemblies and Memory Management

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

Page 30: Secrets of .NET Assemblies and Memory Management

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

Page 31: Secrets of .NET Assemblies and Memory Management

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

Page 32: Secrets of .NET Assemblies and Memory Management

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

Page 33: Secrets of .NET Assemblies and Memory Management

Assembly

Grouping Of Elements Way Of Accessibility

Categories of Assemblies

KolkataGeeks 33

Page 34: Secrets of .NET Assemblies and Memory Management

Grouping of Elements

Single File Assembly Multi-File Assembly

KolkataGeeks 34

Page 35: Secrets of .NET Assemblies and Memory Management

We can group all elements of an assembly in a single physical file.

Single-File Assembly

KolkataGeeks 35

Page 36: Secrets of .NET Assemblies and Memory Management

SingleFileAssembly.dll

Assembly Manifest

Type Metadata

MSIL

Resources

Single File Assembly

KolkataGeeks 36

Page 37: Secrets of .NET Assemblies and Memory Management

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

Page 38: Secrets of .NET Assemblies and Memory Management

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

Page 39: Secrets of .NET Assemblies and Memory Management

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

Page 40: Secrets of .NET Assemblies and Memory Management

Multi-File Assembly

• Assembly Manifest

• Type Metadata• MSIL Code

• Type Metadata• MSIL Code

• Resources

MultiFile.dll

Util.netmodule

Graphic.bmp

KolkataGeeks 40

Page 41: Secrets of .NET Assemblies and Memory Management

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

Page 42: Secrets of .NET Assemblies and Memory Management

How to create mulifile assembly

KolkataGeeks 42

Page 43: Secrets of .NET Assemblies and Memory Management

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

Page 44: Secrets of .NET Assemblies and Memory Management

Now we have to compile modules with respect to other modules. So let’s write the code first:

KolkataGeeks 44

Page 45: Secrets of .NET Assemblies and Memory Management

KolkataGeeks 45

Page 46: Secrets of .NET Assemblies and Memory Management

/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

Page 47: Secrets of .NET Assemblies and Memory Management

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

Page 48: Secrets of .NET Assemblies and Memory Management

Multifile Assembly Created

KolkataGeeks 48

Page 49: Secrets of .NET Assemblies and Memory Management

Way of Accessibility

Private Shared

KolkataGeeks 49

Page 50: Secrets of .NET Assemblies and Memory Management

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

Page 51: Secrets of .NET Assemblies and Memory Management

Let us create a ClassLibrary. Then we are going to build it. After building, .dll is

created.

KolkataGeeks 51

Page 52: Secrets of .NET Assemblies and Memory Management

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

Page 53: Secrets of .NET Assemblies and Memory Management

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

Page 54: Secrets of .NET Assemblies and Memory Management

This is the output. And it shows that the use of private assembly.

KolkataGeeks 54

Page 55: Secrets of .NET Assemblies and Memory Management

Separate copy of ClassLibrary1.dll has been copied here. So this is Private Assembly.

KolkataGeeks 55

Page 56: Secrets of .NET Assemblies and Memory Management

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

Page 57: Secrets of .NET Assemblies and Memory Management

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

Page 58: Secrets of .NET Assemblies and Memory Management

GAC(shared

assembly)

Application1

Application2

Application3

Application4

Pictorial representaion of Shared Assembly

KolkataGeeks 58

Page 59: Secrets of .NET Assemblies and Memory Management

Each assembly is assigned a strong name.

Assigning Strong NameKolkataGeeks 59

Page 60: Secrets of .NET Assemblies and Memory Management

How to install .dll into GAC

Adding .dll in the GAC

KolkataGeeks 60

Page 61: Secrets of .NET Assemblies and Memory Management

Here our shared assembly is located.

Location of Shared Assembly

KolkataGeeks 61

Page 62: Secrets of .NET Assemblies and Memory Management

Using Shared Assembly

KolkataGeeks 62

Page 63: Secrets of .NET Assemblies and Memory Management

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

Page 64: Secrets of .NET Assemblies and Memory Management

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

Page 65: Secrets of .NET Assemblies and Memory Management

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

Page 66: Secrets of .NET Assemblies and Memory Management

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

Page 67: Secrets of .NET Assemblies and Memory Management

KolkataGeeks 67

Page 68: Secrets of .NET Assemblies and Memory Management

Type ildasm

KolkataGeeks 68

Page 69: Secrets of .NET Assemblies and Memory Management

Ildasm window opens. File->Open->Browse for our .dll file.

KolkataGeeks 69

Page 70: Secrets of .NET Assemblies and Memory Management

KolkataGeeks 70