Creating assemblies in asp.net

You can create a single-file assembly by using command-line compilers such as vbc and csc. A single-file assembly includes all the information about the component. You can use the following statement to create an assembly file with the .exe extension.

C#:

csc filename.cs

VB.NET:

vbc filename.vb

These statements create an assembly that has the same name as that of the VB or CS file and with an .exe extension. To create an assembly file with a different filename, you can use the /out: option of the compiler command, as follows:

C#:

csc /out:outputfile.exe sourcefile.cs

VB.NET:

vbc /out:outputfile.exe sourcefile.cs

In these examples, the source file must contain a single entry point, such as the Main() function. If you do not have an entry point, the compiler gives you an error message. If you do not want your source file to contain any entry point and want it to contain only other classes and methods, you must create a library assembly. A library assembly contains components that will be accessed by other assemblies. It is very similar to class libraries.

You can create a library assembly by typing the following command:

C#:

csc /t:library /out:outputfile.dll sourcefile.cs

VB.NET:

vbc /t:library /out:outputfile.dll sourcefile.cs

After you have created assemblies for all the files to be used in a project, you can create a deployment project.

Creating a multifile assembly

You might be required to create a multifile assembly if you want to use classes written in different languages. You might also be required to create a multifile assembly if you want to optimize the process of downloading components. For example, you might want to combine rarely used components into a single assembly.

When you create a multifile assembly, one of the files in the assembly must contain the assembly manifest. Let us look at the process of creating a multifile assembly with the help of an example. Consider the class ConnectDB:

'ConnectDB.VB
'-------------
Imports System.Data
Imports System.Data.SQL
Namespace DB
Public Class ConnectDB
:
Functions for establishing connection
:
End Class
End Namespace

Consider another class, Calculate, which uses the class ConnectDB to connect to the sales database and returns the price of a product after calculating the discount:

'Calculate.VB

'-------------
Imports System.Data
Imports System.Data.SQL
Imports DB
Public Class Calculate
:
Call functions from the ConnectDB class
Calculate the discounted price
:
End Class

To create an assembly with these two files, you need to follow these steps:

1. Compile all the classes that are created within a namespace, which is referenced by other modules. In this example, the class ConnectDB is created within the namespace DB. The DB namespace is accessed in the Calculate class. Therefore, you first need to build the ConnectDB
class into a module by using the following statement:

vbc /t:module ConnectDB.vb /r:system.dll /r:system.data.dll

When you want to create a module instead of a library assembly or an executable file, you need to specify the /t:module option, which instructs the compiler to create a standard DLL file that does not contain the assembly manifest. The /r option is used to specify references to other libraries. This statement creates a module called ConnectDB.mcm.

The default extension for a module is .mcm. You can change the default name of the output file generated by the compiler by using the /out: option of the compiler.

2. After compiling classes that are included inside namespaces, you need to compile classes that use other modules. In the example, the Calculate class references the ConnectDB module and makes calls to functions written in the ConnectDB class. Therefore, you must compile the Calculate class file by executing the following statement at the command prompt:

vbc /addmodule:ConnectDB.mcm /t:module Calculate.vb

In this statement, the /addmodule option is included to specify the name of the module, which is referenced by the file Calculate.vb. When you give this statement from the command prompt, the compiler creates a module called Calculate.mcm, which references another module, ConnectDB.mcm.

3. After compiling various classes into modules, you can create a multifile assembly by using the Al.exe utility. Type the following statement at the command prompt to create an assembly:
al /out:App.dll /t:lib ConnectDB.mcm Calculate.mcm

In this statement, the /out option specifies the name of the output file to be produced by Al.exe. The /t option specifies the file format of the output file.

You can set the option to lib (code library), exe (console application), or win (Windows-based application). Finally, you specify the names of the modules to be included in the assembly. This statement creates a library assembly called App.dll. This file contains the assembly manifest, which describes the types in other modules included in the assembly.

RELATED POST

CREATING ASSEMBLIES WITH ASP.NET

BUILDING HANDLERS IN ASP.NET

INTERFACES AND CLASSES CREATION IN ASP.NET

CACHING IN ASP.NET

CACHING IN ASP.NET PART TWO

WIRE LESS APPLICATION WITH ASP.NET

SECURITY IN ASP.NET PART ONE

SECURITY IN ASP.NET PART TWO

LOCALIZING ASP.NET APPLICATIONS

DEPLOYING ASP.NET APPLICATIONS

ASP.NET CONFIGURATION SYSTEM

WEB SERVICES IN ASP.NET

WEB SERVICES PART TWO

WEB SERVICE INFRASTRUCTURE

WEB SERVICE INFRASTRUCTURE PART TWO

EXCHANGING MESSAGES IN ASP.NET

MICROSOFT HAIL STORM

SOAP AND DOT NET PART ONE

SOAP AND DOT NET PART TWO


No comments:

Post a Comment