You are on page 1of 14

Common Language Runtime (CLR)

The .NET Framework includes a set of classes that run on top of an execution
environment called the CLR. The CLR enables the following key benefits:
 Multilanguage support. True to its name, the CLR is a runtime- execution
environment that provides a common set of features across many different
languages. Coupled with the .NET Framework classes, developers can write
code in the language of their choice and not have to worry about using a different
framework of classes. This arrangement is especially useful in a mixed-language
environment in which some developers are coding in one language, such as C+
+, and others are coding in a different language, such as Microsoft Visual Basic.
For example, you don’t have to learn a different way to open a file just because a
project is better suited to a different programming language.
 Code access security. The CLR provides a means of protecting important
resources on the system such as the hard disk, the network, or the user
interface. Code access security in the CLR protects these resources by
inspecting code as it’s running and only allowing the code to access a resource if
it has permission. Administrators and users can decide what permissions they’re
willing to grant to an application based on information associated with that
application called evidence. For example, an application that is downloaded off
an Internet site and runs in a browser might be able to communicate only with the
host site from which it came, whereas an application installed on the local
machine and run by a user might be able to send messages to other nodes on
the network. In this example, the network represents the protected resource, and
the location from which the application runs is used to determine the manner in
which the application can access the network.
 Memory management. Applications written on top of the CLR do not need to
explicitly allocate and free system memory. Handling your own memory
management can be complex and time consuming. In fact, memory
management is one of the most common sources of bugs and security
vulnerabilities in today’s applications. Without having to worry about memory
management, developers are able to be more productive, write more robust
code, and focus on solving their own problems.
 Managed code. Managed code is code that executes on top of the CLR. It’s
called managed code because the CLR takes care of, or manages, the code’s
execution, such as requests for system memory and the application of code
access security policies.
Why the .NET Framework?

The .NET Framework was designed with one key goal in mind: to increase the
productivity of software developers. This goal is accomplished through a number of
features that are built into the framework. Understanding these features will help you
maximize your development efforts when using the .NET Framework.

Class-Based Model

The APIs in the .NET Framework are encapsulated in classes. A class in the
framework usually contains members such as properties and methods. Developers
can create an instance of a class in the framework and use it to perform tasks that are
required by the application. Developers can also extend classes to include additional
behaviors or combine them to define new classes. This object-oriented approach
offers a familiar model that you can quickly identify with and relate to as you explore the
functionality exposed by the framework.

Layered Architecture

The classes that make up the .NET Framework are layered, meaning that at the base of
the framework are simple types, which are built on and reused by more complex
types. The more complex types often provide the ability to perform more significant
operations. For example, the socket class provides raw access to the network. The
amount of code that you might write when using the socket class to download a file
from the Internet might be 50 lines. The HTTP classes in the framework build on top of
the socket class and make it easier to perform the same task. For example, the
example that downloads a file from the Internet could now be accomplished in 15 lines
of code. More generic URI resolution classes make it possible to download the same
file in one or two lines of code. This layered approach makes it possible to use the
classes that are most applicable to the task your application needs to accomplish.
Because one layer builds on top of the other, it also makes it possible for an application
to use the higher levels to do most of the work without blocking the ability to access the
lower levels for maximum control.
.NET Framework Classes
Now that we’ve covered a bit of the motivation behind the framework, let’s take a
deeper look at the classes. Classes in the .NET Framework are divided into logical
groups called namespaces. Most classes in the .NET Framework exist under a top-level
namespace known as System. The root System namespace contains the basic data
types in the framework, such as numbers, Booleans, Uniform Resource Identifiers
(URI), strings, and objects, the base type from which all other classes in the framework
derive. System also contains a broad array of second-level namespaces that represent
the core functionality in the framework, such as eventing, infrastructure, interfaces,
attributes, and exceptions.

System.* Namespaces Overview

There are two general groups of classes in the .NET Framework:

1. General-purpose base class libraries and


2. Application model-specific classes.

It’s useful to understand the difference between the two so that you’ll better know when
to use classes from each group as you build your application.

General-Purpose Class Libraries

General-purpose class libraries can be useful in almost any context. For example,
System.String represents an immutable fixed-length string of Unicode characters. String
manipulation is useful in a Web-based application that returns HTML content to the
browser, a GUI client application that runs on the end user’s computer, or a long-
running service that has no graphical representation at all.
Table 1-1 contains System namespaces along with a brief description and an indication
of whether the namespace represents a general-purpose base class library or is part of
a particular application model. Each namespace contains anywhere from 10 to more
than 100 classes, depending on the number of lower-level namespaces it contains.

Table 1-1: Second-Level System Namespaces*

Base
Class or
Namespace Description
Applicatio
n Model
Classes that can be used to reference
CodeDom Base
the structure of a code document.
Contains interfaces and classes that
define various collections of objects,
Collections Base
such as lists, queues, bit arrays, hash
tables, and dictionaries.
Provides classes that are used to
ComponentModel implement the run- time behavior of Base
components and controls.
Provides classes and interfaces that
allow you to programmatically access
Configuration .NET Framework configuration settings Base
and handle errors in configuration files
(.config files).
Contains the classes that represent
ADO.NET, which enables you to build
Data Base
components that efficiently manage
data.
Provides classes that enable you to
Diagnostics interact with system processes, event Base
logs, and performance counters
Provides easy access to Active
DirectoryServices Directory directory services from Base
managed code.
Provides access to GDI+ basic graphics
Drawing Base
functionality
Provides an important infrastructure for
EnterpriseService
enterprise applications, including access Base
s
to COM+.
Globalization Contains classes that define culture- Base
related information, including the
language; the country/region; the
calendars in use; the format patterns for
dates, currency, and numbers; and the
sort order for strings.
Contains types that allow synchronous
IO and asynchronous reading and writing Base
on data streams and files.
Provides access to a rich set of
management information and
management events about the system,
Management Base
devices, and applications designed for
the Windows Management
Instrumentation (WMI) infrastructure.
Provides classes that allow you to
connect to, monitor, and administer
Messaging Base
message queues on the network and
send, receive, or peek messages.
Provides access to network resources
over protocols such as Transmission
Net Control Protocol (TCP), User Datagram Base
Protocol (UDP), and Hypertext Transfer
Protocol (HTTP).
Contains classes and interfaces that
provide a managed view of loaded
Reflection types, methods, and fields, with the Base
ability to dynamically create and invoke
types
Provides classes and interfaces that
allow developers to create, store, and
Resources Base
manage various culture-specific
resources used in an application.
Includes a broad set of third-level
namespaces such as a Win32
Runtime Base
interoperability layer, remoting, and
object serialization.
Provides the underlying structure of the
Security CLR security system, including base Base
classes for permissions.
Provides classes that allow you to
Application
ServiceProcess implement, install, and control Windows
model
service applications.
Contains classes representing ASCII,
Unicode, UTF-7, and UTF-8 character
encodings; abstract base classes for
converting blocks of characters to and
Text Base
from blocks of bytes; and a helper class
that manipulates and formats String
objects without creating intermediate
instances of String.
Provides classes and interfaces that
Threading Base
enable multithreaded programming.
Provides classes that allow you to raise
Timers Base
an event on a specified interval.
Supplies classes and interfaces that
Application
Web enable the browser/ server
model
communication used by ASP.NET.
Contains classes for creating Windows-
based applications that take full
Application
Windows.Forms advantage of the rich user interface
model
features available in the Microsoft
Windows operating system.
Provides standards-based support for
Xml Base
processing XML.
... Many more in newer version. ...

* Based on the .NET Framework Class Library overview at msdn.microsoft.com. As you


can tell from Table 1-1, the .NET Framework contains a broad set of functionality.

Application Model–Specific Classes

Think of an application model as a set of classes that define a manner in which an


application operates. They define the general structure of the application. The
following sections provide a brief look at the application models in the .NET Framework.

ASP.NET

ASP.NET, contained in the System.Web namespace, is designed to make it easy for


developers to build real-world Web applications. It offers a rich application environment
for building server-side HTTP applications that dynamically create content and send it to
a client, such as a browser or a mobile device.
Using ASP.NET and the base class libraries in the framework designed for distributed
application development, you can build applications that gather data from multiple
back-end or remote sources and present it to the browser in an aggregated format. The
following example demonstrates an ASP.NET page that calls an XML-based Web
service using the .NET Framework.
This weather service sample application is extremely simple. It takes a postal code as
an input parameter and returns the forecast. In this case, it always returns sunny unless
the zip code supplied is 11111, in which case it will say rainy, as shown in Figure 1-2.
The following code listing shows the code for this application:

Visual Basic .NET: Weather Service Sample Application

The steps:

Create a new ASP.NET Web Service Application project.


Add the following code.

Imports System.Web.Services
Imports System.Web.Services.Protocols
Imports System.ComponentModel
' To allow this Web Service to be called from script, using
ASP.NET AJAX, uncomment the following line.
' <System.Web.Script.Services.ScriptService()> _
<System.Web.Services.WebService(Namespace:="http://temp
uri.org/")> _
<System.Web.Services.WebServiceBinding(ConformsTo:=Wsi
Profiles.BasicProfile1_1)> _
<ToolboxItem(False)> _
Public Class Service1 ' This class exposes a Web Service
method that
' takes a zip code and returns the forecast
' for that area.
Inherits System.Web.Services.WebService
' The logic for GetTodaysForecast is limited for the
' purposes of this example to check for
' zip = 11111 and return "rainy" if it matches,
' otherwise it will return "sunny".
<WebMethod()> _
Public Function GetTodaysForecast(ByVal zip As
System.String) As System.String
Dim forecast As String = "sunny"
If zip = "11111" Then
forecast = "rainy"
End If
Return forecast
End Function
End Class
Build the project.

Make sure there is no error.


Next, we add a web form.
Add the following code.

<%@ Page Language="vb" AutoEventWireup="false"


CodeBehind="WebForm1.aspx.vb" Inherits="WeatherServiceVB.WebForm1"
%>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"


"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >


<head runat="server">
<title>A Dummy Weather Service</title>
</head>
<script language="VB" runat="server">

Dim zip as String = "11111"

Sub Submit_Click(sender As Object, e as EventArgs)


Try
zip = ZipCode.Text
Catch ex As Exception
' exception ignored
End Try
Dim myService As New WeatherServiceVB.Service1()
Result.Text = "Today's forecast is: " +
myService.GetTodaysForecast(zip)

End Sub
</script>
<body style="font: 10pt verdana">
<h4>Weather Report </h4>
<form id="Form1" runat="server">
<div style="padding:15,15,15,15;background-color:Gray;width:300;border-
color:black;border-width:1;border-style:solid">
Zip Code: <br /><asp:TextBox id="ZipCode" Text="11111"
runat="server"/><br />
<input type="submit" id="Add" value="Get Weather Report"
OnServerClick="Submit_Click" runat="server" />
<p />
<asp:Label id="Result" runat="server"/>
</div>
</form>
</body>
</html>

Run the project.

The following is a sample output.


Try key in different postcode and click the Get Weather Report button.
Figure 1-2: ASP.NET weather display page

The ASP.NET weather display page provides a mechanism for entering a postal code
into a form and calling the weather service to retrieve and display the forecast, as
shown in the following code:

You might also like