.NET Assemblies - III
This is a three series article.
-
In the first part we discussed the 3-tier architecture.
-
In part-II of this article we had an introduction to what the .NET assemblies
are? and
-
In the last part we are practically going to be implementing a simple
.NET assembly.
Part-III :: Implementing a simple .NET Assembly
This part of the article is going to be the last of the series. In the last part
I introduced you to what the .NET Assemblies are and for what reasons are they
used? You also were informed about the advantages of using the .NET Assemblies
as compared to the other scripting techniques that we have available in the ASP
.NET.
Before implementing an assembly I would like to add,
that we implement many pre-built assemblies while we are working on ASP .NET
based applications. For example; the System namespace and other
namespaces like it i.e. System.Data, System.Web are also pre-built
assemblies that have been provided in the .NET framwork to us by Microsoft. In
addition to this they have also provided us the capability of making our own
assemblies. As compared to the classic ASP making components in form of
assemblies in ASP .NET is much more easier and flexible.
Now let's go ahead with creating our first component. This is
going to be a simple component that would just provide us with a few options to
display a message on the screen. Write the following code in Notepad
or any other editor that you have.
|
Imports System
Namespace HRM.Display
Public Class DisplayConsole
Public Function Welcome() As String
Return "Welcome to your first
component!"
End Function
Public Function Output(strParam As String) As String
Return strParam
End Function
End Class
End Namespace |
Save this file by the name "hrmDisplay.vb".
Now for the process of converting this file into a compiled
DLL file, we require to make a batch file. Again open the
Notepad and add the following to it. Save the following file by name "MakeComponent.bat"
|
set indir=c:\inetpub\wwwroot\hrmDisplay.vb
set outdir=c:\inetpub\wwwroot\bin\hrmDisplay.dll
set assemblies=System.dll
vbc /t:library /out:%outdir% %indir% /r:%assemblies% |
Before running the file you require to create a directory by
the name of bin. Create a directory in your current directory by
name of bin and then run the file. Checkout the bin
directory your DLL file would be placed there after its creation.
Before using the component we require to do one more thing!
and that is to make a web.config file. Web.config is an XML
based file that specifies important configuration for an ASP .NET application.
Again open up the Notepad and write the following code.
|
<configuration>
<system.web>
<sessionState timeout="10" />
<compilation>
<assemblies>
<add assembly="hrmDisplay"
/>
</assemblies>
</compilation>
</system.web>
</configuration> |
Save this file as "web.config". The reason that we made the
bin directory and then placed the DLL file within it
was that the web.config would always search for the file in the
bin directory, not only this assembly, but other user defined and
third party assemblies also require there references to be added into the
web.config.
Now we are ready to use our component within our ASP .NET
page. Open up Notepad or any other editor and add the following
code into the file and save it as "Test.aspx".
|
<%@ Import Namespace = "HRM.Display" %>
<%@ Page Language = "VB" %> <Script Language="VB" Runat="Server">
Sub Page_Load(Source As Object, Sender As EventArgs)
Dim objComponent As New DisplayConsole
Response.Write (objComponent.Welcome())
<br>
Response.Write (objComponent.Output("This is my message!"))
End Sub </Script> |
Now open your page in Internet Explorer or any
other browser through your server and see your component in action.
|