8. Assemblies and Application Domains
S ect i on Owner : Akila Manian MVP
C ontent C ontr ibut o r s : Nara yana Ra o Surapaneni MVP
8.1 Introduction
In Microsoft .NET, when an application is compiled, the output of the compilation produces what is known as an Assembly. Two types of assemblies can be produced by the
compilation procedure. One is the executable file .exe and the other is a dynamic link library file .dll. Basically the assembly is the unit of deployment in Microsoft .NET
and it can be thought of as a collection of types and resources that form a logical unit of functionality.
An assembly is a self-describing entity. It contains all information about the types classes contained in the assembly, all external references needed for executing the
assembly and so on. This is possible with an assembly manifest. The manifest contains assembly’s identity and version information, a file table containing all files that make up
the assembly and the assembly reference list for all external dependencies. Thus assemblies do not need to depend on the registry values for compilation or execution.
An assembly contains manifest data and one or more modules. Manifest data contains information about the assembly and other list of assemblies that it depends on. It also
contains all the publicly exposed types and resources. An assembly contains various modules. Each module contains metadata and IL.
Assemblies can be viewed by application developers with the help of a tool called ildasm IL Disassembler provided by the .NET Framework.
8.2 Assembly Types
Assemblies can be single file assemblies or multi file assemblies. In multi file assemblies one of the files must contain the assembly’s manifest data. Multi file assemblies can have
only one entry point even though the assembly can contain multiple code modules. A multi file assembly is created primarily for combining modules written in different
programming languages. Once the assembly is created, the file that contains the assembly manifest and hence the assembly can be signed, or one can give the file and
the assembly a strong name and put it in the global assembly cache.
Main uses of multi file assembly are for combining modules written in different programming languages. They enable optimization of downloading an application by
putting seldom-used types in a module that is downloaded only when needed. The .NET Framework downloads a file only when it is referenced; keeping infrequently referenced
code in a separate file from the application optimizes code download.
Let us look at an example of how to create multi file assembly.
AddModule.cs
Copy and Paste the following code into Notepad and save it as or AddModule.vb, depending on the language that you are using
Code Listing in C using System;
public class AddClass {
public int Addint Operand1, int Operand2 {
return Operand1 + Operand2; }
} Compiling in C
Compilation csc r:System.dll t:Module AddModule.cs Code Listing in VB.NET
Imports System Public Module AddModule
Public Class AddClass Function AddByVal Operand1 As Integer, ByVal Operand2 As Integer As Integer
Add = Operand1 + Operand2 End Function
End Class End Module
Compiling in VB.NET vbc r:System.dll t:Module AddModule.vb
This file is compiled with the target option as module. Hence an assembly is not created. The output is a file with an extension of .netmodule.
Similarly create SubtractModule.cs as shown below
SubtractModule.vb Code Listing in C
using System; public class SubtractClass
{ public int Subtractint Operand1 , int Operand2
{ return Operand1 - Operand2;
} }
Compiling in C csc r:System.dll t:Module SubtractModule.cs
Code Listing in VB.NET
Imports System Public Module SubtractModule
Public Class SubtractClass Function SubtractByVal Operand1 As Integer, ByVal Operand2 As Integer As
Integer Subtract = Operand1 - Operand2
End Function End Class
End Module
Compiling in VB.NET vbc r:System.dll t:Module SubtractModule.vb
Now create the main module, which references the above modules. The code is as shown below for MainModule.csMainModule.vb
MainModule.cs Code Listing in C
using System; public class MainModule
{ public static void Main
{
int iOperand1, iOperand2, iResult ; iOperand1 = 22;
iOperand2 = 11; iResult = 0;
AddClass objAddClass = New AddClass; SubtractClass objSubtractClass = New SubtractClass;
iResult = objAddClass.AddiOperand1, iOperand2; Console.WriteLineiResult.ToString;
iResult = objSubtractClass.SubtractiOperand1, iOperand2; Console.WriteLineiResult.ToString;
Console.ReadLine; }
} Compiling in C
Compilation csc r:System.dll MainModule.cs Code Listing in VB.NET
Imports System Public Module MainModule
Sub Main
Dim iOperand1, iOperand2, iResult As Integer iOperand1 = 22
iOperand2 = 11 iResult = 0
Dim objAddClass As New AddClass Dim objSubtractClass As New SubtractClass
iResult = objAddClass.AddiOperand1, iOperand2 Console.WriteLineiResult.ToString
iResult = objSubtractClass.SubtractiOperand1, iOperand2 Console.WriteLineiResult.ToString
Console.ReadLine End Sub
End Module Compiling in VB.NET
vbc r:System.dll MainModule.vb The code is compiled as follows
To create a multi file assemble, which contains the two modules created previously namely AddModule and SubtractModule, compile using the following command at the
command prompt Compiling in C
csc System.dll addModule:AddModule.netmodule addModule:SubtractModule.netmodule MainModule.cs
Compiling in VB.NET Vbc System.dll addModule:AddModule.netmodule
addModule:SubtractModule.netmodule MainModule.vb
This process creates a multi file assembly. This assembly contains the two modules created previously namely AddModule and SubtractModule. Thus this assembly contains
multiple modules. If ildasm utility is executed on the MainModule assembly, it shows that the manifest information in the MainModule contains references to the AddModule
and the SubtractModule modules. That is the modules are linked to the main assembly by the information contained in the main assembly’s manifest information.
8.3 Private Assemblies