chapter a .net framework 1 overview

20
Chapter 1 A .NET Framework Overview COPYRIGHTED MATERIAL

Upload: others

Post on 24-Mar-2022

3 views

Category:

Documents


0 download

TRANSCRIPT

Chapter

1

A .NET Framework Overview

COPYRIG

HTED M

ATERIAL

.NET represents Microsoft’s vision and technologies that connect information, people, devices, and systems to form applications. This is a type of distributed computing where software applications

will provide services to one another. Microsoft has attached the .NET term to various products that fulfill certain roles in accomplishing Microsoft’s vision of distributed computing.

There are the .NET Servers, the server products that provide the necessary infrastructure to support applications that will be delivered over the Internet or over company intranets. These products include Windows .NET Server, SQL Server 2000, Biztalk Server 2002, Exchange Ser-ver 2000, Host Integration Server, Application Center Server 2000, SharePoint Portal Server 2002, and Content Management Server. These products provide stand-alone services such as e-mail or connectivity to legacy systems, and you can use them in the applications that you write using the .NET Framework to provide services for data or hosting Web applications.

There is the .NET Framework SDK, which comprises development tools, infrastructure, and libraries that make it easy to build independent software applications, which can communicate with each other and link the computing devices of the world together.

The .NET Framework also provides the Common Language Runtime, which is the successor to COM and overcomes the weaknesses of the COM architecture by providing mechanisms for increased portability, better versioning (through components called assemblies), enhanced security, and ease of programming. It manages your code to make it easier to provide long-running, fault-tolerant, and scalable services. For example, it uses a garbage collection mechanism to prevent memory leaks and can isolate code into application domains, which act as lightweight processes, to provide better performance and increased stability. Microsoft goes one step further and provides the Base Class libraries. These are code libraries, organized into logical groupings called namespaces, that provide the foundations to work with the standards on the Internet, such as XML and HTTP, to build scalable Web or Windows applications. It makes it easy to provide software as a service over the Internet or inside a corporation through the use of Web services. Microsoft is vying to have the best tools and technologies to implement their vision of how the Internet and computers will evolve in the coming years.

This chapter provides an overview of .NET, but it is by no means a comprehensive discussion of .NET. The chapter is designed to help you grasp how .NET works, understand the infor-mation presented in this book, and pass the exams for the MCAD and MCSD certifications (which is most likely your primary goal).

By the time you sit for exams 70-305 and 70-306, you should have a solid understanding of the .NET Framework. Such coverage is outside the scope of this book. If you are unfamiliar with the .NET Framework, there are a number of books out on the subject, including Visual Basic

®

.NET Programming by Harold Davis, ISBN: 0-7821-4038-6 and Mastering™ Visual Basic

®

.NET

by Evangelos Petroutsos, ISBN: 0-7821-2877-7, both by Sybex, Inc.

Common Language Runtime

5

Common Language Runtime

The

Common Language Runtime

or CLR is the mechanism by which all the code you will write or learn about in this book is executed. The CLR is implemented in a COM in-process server called

mscoree.dll

. It provides the necessary services to load, execute, and clean up code that is written to the .NET platform. The role of the CLR is similar to that of the Visual Basic runtime or the Java Virtual Machine in that it is responsible for executing the code you develop using the .NET Framework. However, the CLR is radically different in that it does not interpret p-code or byte code, but has been built from the beginning to run native (processor-specific) code. This performs much better than interpreted languages. When you compile your application, you compile to

Microsoft Intermediate Language

(MSIL) rather than to machine code initially. MSIL acts as a common language for all programming languages, hence the term Common Language Runtime. This is the case regardless of whether you use Visual Basic .NET, C#, C

++

with the managed extensions, or any other language with a compiler for .NET. The MSIL is then just-in-time compiled to native code on a method-by-method basis when it is needed. The native code is then cached to avoid having to compile it again if the method is called again. This enables applications to perform well, even though they are using a runtime.

The CLR provides services that manage the code that it executes, which is referred to as

managed code

(unmanaged code is any code run outside of the services of the CLR). These are core to maintaining various aspects of your code during its lifetime. Table 1.1 describes each of the services provided by the Common Language Runtime.

T A B L E 1 . 1

The Common Language Runtime Services

CLR Service Description

Class Loader Responsible for locating and loading assemblies into memory and then locating the MSIL for the necessary class to execute. This functionality is wrapped by the methods in the

System.Assembly

shared class, such as the

System.Assembly.Load

or

System.Assembly.LoadAssembly

methods, which load the assembly from the GAC or from file, respectively.

MSIL to NativeCompilers

Just-in-time compiles MSIL to machine code on a method-by-method basis. The machine code is then cached to speed up future calls.

Code Manager Manages the execution of the code.

Garbage Collection Automatically cleans up objects in the managed heap. This means that you no longer have to worry about memory leaks even if you have circular references to objects. The

System.GC

class allows you to control the garbage collector. This is a multithreaded garbage collector for maximum performance.

Security Engine Responsible for making sure that the code is safe to execute before it is executed. It performs a number of checks to ensure that there are no buffer overruns and that the privileges that the code needs has been granted to the code by the user or administrator.

6

Chapter 1 �

A .NET Framework Overview

For more information on the CLR, check out VS .NET help at

ms-help://MS.VSCC/

MS.MSDNVS/cpguide/html/cpconcommonlanguageruntimeoverview.htm

.

A .NET language compiler emits more than just MSIL. It also writes out metadata that describes the types and versions of library and executable files (DLLs and EXEs) used by your code. The metadata is stored in a part of the library or executable file and is called the

manifest

. The manifest replaces the necessity of the system registry, type libraries, COM

+

catalog, or other mechanism used by COM to store metadata. This is an improvement over COM, because these external locations are prone to corruption or deletion. This also solves one of the biggest problems with COM’s external metadata versioning: side-by-side execution and “DLL Hell”.

Side-by-side execution means that you can have multiple versions of the same shared COM library or executable installed on one computer. In COM you had problems with side-by-side execution of two versions of the same application, either on the same machine or even in the same process. (Microsoft allows for side-by-side execution of COM components in Windows 2000 and XP, though it is cumbersome.) .NET allows multiple versions of the same library or executable to exist, so you don’t have to upgrade your old application and all of the components and custom or third-party add-ons all at once. You can install the newer version of the application and later upgrade pieces of the old application to take advantage of the new application’s features. For example, say you are using the libraries in the .NET Framework to access data and do web programming for your application and a newer version of the .NET Framework is released by Microsoft. You can install it on your machine and your application will continue to use the old version of the .NET Framework it was compiled against until you decide to upgrade it to the new version by recompiling your DLLs to the new version.

Metadata also solves a very familiar problem to those who have installed a lot of software on Windows: “DLL Hell.” There is a big problem with versioning in COM applications. This problem tends to arise when components need to be created dynamically through late-binding.

Debug Engine Facilitates debugging of the code.

Type Checker Verifies that the correct type for the object is available and loaded.

Exception Manager Responsible for raising and managing exceptions in the system.

Thread Support The CLR supports free threading for all languages. This service is responsible for translating the way that .NET handles threads to the proper OS calls to create the threads and maintain thread context.

COM Marshaler Makes integrating .NET code and COM code easier by providing ser-vices to marshal data between .NET and COM. This makes interacting with COM components transparent and relatively painless.

T A B L E 1 . 1

The Common Language Runtime Services

(continued)

CLR Service Description

The Common Type System

7

The versions of COM libraries your application uses are not stored anywhere with the applica-tion and need to be resolved dynamically through the registry when the application is run. This is generally done by passing a ProgID (program identifier), which must be unique to the CreateObject function in VB. If the ProgID is overwritten to point to the newest version of a COM library (DLL) when it is installed on the machine, it could break applications that rely on the older version of the library if the newer version is not 100% compatible with the old program. If the correct version of a DLL is not available when the application runs, the application will not function correctly or may not even run at all. This means that simply installing another application on the system can break the first application. You may have experienced a situation where you installed an application and it updated some DLLs to the newer version—and broke an application you already had installed. After much effort, you probably found that they could not be installed on the same computer, at least not very easily.

.NET solves this problem by allowing developers to control the versions and dependencies between the different software components. These dependencies are then stored in the manifest associated with a .NET assembly. (We will discuss assemblies in greater detail later in this chapter, but for now it is enough to know that an assembly is usually one DLL or EXE file much like COM components.) .NET uses the information in the manifest to maintain application integrity by verifying dependencies and making sure that code has not been changed, and by raising an exception if a change has occurred. Because each assembly carries its own version information, multiple versions of assemblies can be loaded into a process at once and multiple versions of a shared assembly can be tracked, so you don’t have to make the choice between applications. Since the manifest is stored in the assembly and not in the system registry, this means that there is no need to register your components when you install them or worry about the registry becoming corrupted. This also reduces the chance of applications interfering with each other’s registry settings, and makes deploying applications easier because in most cases all you need to do is copy the DLL to the computer.

The Common Type System

A

type

is the generic way to refer to any object created in .NET. A type can be a class, structure, enumeration, interface, or array. Types can be used from any of the .NET languages directly. In fact, there is something called the

Common Type System

(CTS) that permits interoperability between all the .NET languages. This makes using multiple languages for a development project easier, because you don’t need to convert data from the format of one language to the other, which was a problem with COM. Each language may have its own specific keyword for creating various primitive types, but they are always compatible because they actually create the same type when compiled to MSIL. If the language you are using does not have a certain type, you can just specify the .NET type directly, as opposed to using a keyword in the language. For example, Visual Basic .NET supports only signed integer types (around –2 billion to 2 billion). Maybe you would like to use an unsigned version because you need a number up to about 4 billion but don’t want to use a long because it takes twice as much memory. You could declare a variable as a System.UInt32 even though Visual Basic .NET has no keyword to support this type. Information about types needed at runtime is stored in the assembly’s manifest.

8

Chapter 1 �

A .NET Framework Overview

Table 1.2 lists the types found in the

Common Language Specification

, which is a design document that states what you must do and avoid to guarantee that the code written in the different .NET languages are interoperable.

T A B L E 1 . 2

.NET Common Type System and the VB .NET Equivalent Keywords

Type Description Range

Keyword

in VB .NET

System.Boolean

A true or false value. Cannot be implicitly converted to other primitive types.

True or False Boolean

System.Byte An unsigned byte. An integer from 0 to 255. Byte

System.Char A Unicode character16-bit value.

Any valid Unicode character. Char

System.DateTime

A date and time value.

IEEE 64-bit long integers that represent a date from January 1, 0001 to December 31, 9999 and a time from 00:00:00 to 23:59:59.

Date (You cannot use a Double to store a DateTime as in VB6)

System.Decimal

A signed value with 28 significant digits. Not prone to the rounding errors of the Single or Double.

–79,228,162,514,264,337,593,543,950,335 to 79,228,162,514,264,337,593,543,950,335

Decimal

System.Double

A 64-bit, double precision floating point number.

–1.79769313486231570E+308 to1.79769313486231570E+308

Double

System.Int16 A signed 16-bit integer.

–32768 to 32767 Short (New Keyword)

System.Int32 A signed 32-bit integer.

–2,147,483,648 to 2,147,483,647 Integer (Now 32 bit)

System.Int64 A signed 64-bit integer.

–9,223,372,036,854,775,808 to 9,223,372,036,854,775,807

Long (Now 64 bit)

System.Sbyte A signed 8-bit integer. –128 to 127

System.Single

A 32-bit, single precision floating point number.

–3.402823E38 to 3.402823E38 Single

The Common Type System

9

All types in the .NET Framework derive from

System.Object

. This means that any type, whether it is an Integer or a Customer class, is an object. .NET only differentiates between two types of objects when they are a

value type

or a

reference type

.Value types are allocated on the stack just like primitive types in Visual Basic 6 or C

++

and will go out of scope when the block they were declared in exits. All value types derive from

System.ValueType

; you do not use the

new

keyword to create a value type because it is not allocated on the managed heap.

Actually, you can use the

new

keyword to declare a value type if you have a parameterized constructor on the value type. This is usually only the case when

you work with custom value types.

Reference types are allocated on the managed heap and will therefore be garbage-collected sometime after they go out of scope. They inherit from

System.ReferenceType

, and you use the

new

keyword to create an instance of a reference type on the managed heap. Here is an example:

‘Value types declared and initialized

Dim i As Integer = 8

Dim s As MyStruct ‘Structure called MyStruct

System.TimeSpan

A period of time con-taining a minimum value (starting time) and a maximum value (finishing time). This value can be positive or negative depending if the max-imum value is greater than or less than the minimum value.

Minimum value field is –10675199.02:48:05.4775808.Maximum value field is 10675199.02:48:05.4775807

System.String

A Unicode string. Zero or more Unicode characters. The default is a null reference.

String

System.Array Provides methods to manipulate, sort, or search arrays.

Depends on the type of the array. The index of an array is an unsigned integer, so you can have up to 4294967296 items in the array.

System.Object

The base type from which all other types in .NET derive.

Not Applicable to an Object. Object

T A B L E 1 . 2

.NET Common Type System and the VB .NET Equivalent Keywords

(continued)

Type Description Range

Keyword

in VB .NET

10

Chapter 1 �

A .NET Framework Overview

‘Reference types declared

Dim obj As New String(“Here is a string!) ‘String

Dim arr() As Integer = new Integer(9) {} ‘Array

You can convert from a value type to a reference type by casting the value type to an Object. This is referred to as

boxing

the type. You can later unbox the object to the original value type by casting it back to the value type. Boxing and

unboxing

is useful but incurs some overhead. For example, there is a class called

ArrayList

in the .NET Framework that acts as a dynamic array of any type that you want to give it. Instead of having the

Add

method take every type, Microsoft had it take an Object. This means that value types would need to be converted or boxed to the reference type Object before being passed to the method. This is an implicit conversion, so you might not even realize it is happening.

There are specialized versions of the data structure classes (such as

ArrayList

) to support String reference types instead of just generic objects. Since strings are a common item stored in data structures, this helps speed things up by not performing conversions from object to string. Whenever boxing or unboxing occurs, memory is copied from the stack to the managed heap or vice versa, respectively. This takes extra instructions, which translates to extra CPU cycles doing these conversions. If you were doing this in a long loop, you might have a problem with performance in your application. By converting it before going into the loop, you can save instructions and thus improve the performance of your application.

Boxing a value type occurs in the following code:

Dim i As Integer = 14

Dim obj As Object = i

You would unbox the type by using the following code:

Dim j As Integer = CType(obj, Integer)

CType

is a useful Visual Basic function to convert from one type to another. You pass it the Object reference you want to convert and the type you want to convert it to for the second parameter, and it will return that type or throw an exception

if the conversion is not possible.

For more information on the Common Type System in .NET, see the following in Visual Studio .NET help:

ms-help://MS.VSCC/MS.MSDNVS/cpguide/html/cpconthecommontypesystem.htm

.

Namespaces

The .NET Framework logically (not physically) organizes the types that you create into a namespace. A

namespace

is a logical organization of your classes, interfaces, and other types that make up your code. Namespaces also help prevent naming conflicts for the types you use. For example, if you are creating an application that integrates customer information from an accounting application and a sales incentive application, both applications might contain a class named

Customer

. Creating an instance of this class would be difficult because the

Namespaces

11

compiler would not know which customer you meant when you typed

new

Customer()

. A namespace would solve this problem by tacking on a unique name in front of the Customer object so you could say

new Accounting.Customer()

versus

new ABC.Customer()

. Namespaces are analogous to the COM use of GUIDs to uniquely identify code for ease of use and readability.

When you compile your code, the .NET Framework packages the manifest and MSIL into a DLL or EXE. Namespaces can be nested inside each other to create hierarchies of organization, and can also cross the physical boundaries of the library or executable files.

You create a namespace by using the namespace keyword as follows:

Namespace ABCCompany

'Classes in the ABCCompany namespace can go here

Namespace WebStuff

'Class in the ABCCompany.WebStuff namespace

Class Customer

‘Customer implementation

End Class

End Namespace

End Namespace

You must use the namespace to refer to a class you might want to use. For example, if you created a class called

Customer

in the

ABCCompany.WebStuff

, you would have to type the following code to create a new instance of the class:

Dim c As new ABCCompany.WebStuff.Customer()

If you don’t like to type this much, you can shorten it by importing the namespaces you will frequently use and, if there are no name conflicts, avoid typing the whole namespace:

Imports ABCCompany.WebStuff

Dim c As New Customer()

The

Imports

statement is used to tell the compiler in what namespaces to look for classes that aren’t found in the root namespace. Imports statements must precede any other declaration in the source file.

If you do have a conflict between classes in two namespaces, you can create an alias to a namespace so you don’t have to type as much, as seen here:

Imports abc = ABCCompany.WebStuff

Imports xyz = XYZCompany.Accounting

Dim c As New abc.Customer()

You can even set up an alias to a class by doing the following:

Imports a = ABCCompany.WebStuff.Customer

Dim c As New a()

12

Chapter 1 �

A .NET Framework Overview

Microsoft recommends that library developers use the following guideline when creating names for their namespaces:

CompanyName.TechnologyName

The namespace

Microsoft.Outlook would be an example of this rule.Namespaces play an important part in the .NET Framework Base Classes (discussed later in

this chapter) and, of course, on the exam.

Assemblies and Application DomainsAn assembly comprises one or more files (usually one file) of library or executable type (DLLs or EXEs), that contain MSIL, the manifest, and resources. An assembly is self-describing through its manifest, which contains metadata (properties of the assembly) that identify the types and versions of all components used by the assembly plus all the information on the components contained in the assembly. The manifest replaces much of the HKEY_CLASSES_ROOT portion of the registry used by COM, which is why you don’t have to register your .NET components.

An assembly is the entity used for identifying, sharing, versioning, and deploying your code in .NET. It also represents a security boundary for an application and can have code access permissions (see Chapter 10) applied to it. Security is controlled through the manifest, which defines the versioning and security requirements for the application. An assembly can be referenced at runtime by using the methods and properties in the System.Reflection.Assembly class.

An assembly called a multifile assembly can be made up of more than one DLL by using the .NET Framework command line tools. The additional DLLs that make up this assembly usually contain resources such as images or string text, but could also contain MSIL for various classes in the assembly. They do not contain the manifest for the assembly, which is contained in just one DLL in the assembly.

A .NET application must have at least one assembly, but any substantial application will usually have more. Assemblies are the smallest unit that is dynamically loaded into memory and executed. This means that resource assemblies or other pieces of an application can be downloaded from the disk, a website, or a network share when needed.

An assembly is loaded into an application domain (app domain) in the CLR. The best way to explain an app domain is to start by talking about how Windows manages memory. All Windows applications run inside of processes. A process is assigned memory, kernel resources, and at least one thread to execute code. Any memory or resource used by one process is protected by the operating system from other processes. This provides greater stability for applications on your machine, because one process cannot directly manipulate memory in another process. In essence, each process thinks it is the only one on the machine, because the OS does a good job of isolating them.

You may think that you would want every part or instance of your application to run in a separate process. For example, if you built a number of Web applications and hosted them on

.NET Framework Base Class Library 13

the same server, you could have them run in the same process for each application or in a separate process for each Web application. All applications running in one process would scale well, but they all share the same memory; thus, if one of the applications was written poorly or was infiltrated by malicious individuals, it could bring down one or all of the other applications running within the process.

Stability problems like these can be resolved by creating a separate process for each Web application. But there is overhead in creating and maintaining each process for each application, so this solution will not scale as well. With app domains, you get the best of both worlds. The code verification feature of the CLR guarantees that code is safe to run before it is allowed to execute. This means that all of your .NET applications can gain the scalability of running in the same process without the adverse stability issues, because the CLR ensures that the running code cannot manipulate other app domains memory inadvertently. You would have to create a proxy to marshal data from one app domain to another if you needed to share information. This is kind of like lightweight processes in a Windows process. For example, by default ASP.NET launches each Web application in a separate app domain in the aspnet_wp.exe process.

You can interact with an app domain in managed code through the System.AppDomain class. You can spawn new app domains for your application’s assemblies to be loaded into by calling the CreateDomain method of the System.AppDomain class, as seen here:

Dim domain As AppDomain = AppDomain.CreateDomain("TheAppDomainName")

You should consider a separate app domain if you require the following tasks:� You would like to unload an assembly at a later time. Assemblies can be unloaded from

memory only if the entire app domain is shut down.� You are running several tasks that need to be isolated from each other and don’t want to

incur the overhead of separate processes.� You need to provide fewer rights to less-trusted assemblies than they would receive in the

default policy for the app domain.

.NET Framework Base Class LibraryThe Base Class library represents a common framework that all .NET languages use. This replaces the multitude of choices for libraries with one consistent framework. You no longer need to know Microsoft Foundation Classes (MFC) if you write code in C++, then switch to Visual Basic Forms or ASP for Web applications. This makes your life easier as a programmer if you program in multiple languages, since you don’t have to learn two or three different ways to do a task such as creating a dialog box or writing a web-enabled program. Microsoft can also focus their efforts on improving one API rather than maintaining multiple APIs with similar functionality. This also means that all languages are on an equal footing and have equal func-tionality with regard to programming to the Web or Windows. They all expose the same functionality through the framework. It is now just a question of what language you prefer or know rather than what language supports the feature or library you need.

14 Chapter 1 � A .NET Framework Overview

Although the languages are functionally equivalent, there are still some perfor-mance differences between the languages. If performance is an issue, you might want to test the module in some of the various languages and see which one performs better. All languages should perform roughly equal to each other in the future, so you shouldn’t give too much weight to this.

The Base Class library is contained in the mscorlib.dll file. It is organized into namespaces to make it convenient to find the various classes and their properties, methods, and events, many of which we will use and discuss in the course of this book. Table 1.3 lists the namespaces in .NET and a description of what they contain.

T A B L E 1 . 3 Useful .NET Base Class Library Namespaces

Namespace Description

System The fundamental classes for handling value and reference types, exceptions, events, and other basic functionality in .NET code.

System.Collections Organizes the types needed for the various collections of objects, such as HashTable, ArrayList, SortedList, List, Queue, Stack, and Dictionary.

System.ComponentModel Interfaces and classes needed to create the design-time and run-time behavior for components to support every-thing from data binding to licensing.

System.Configuration Contains the classes and interfaces to programmatically work with the .NET configuration files (.config files).

System.Data Contains classes that implement the common function-ality for ADO.NET data access, the DataSet class being the centerpiece.

System.Data.OleDb Contains the collection of classes that implement the .NET data provider for OLEDB.

System.Data.SqlClient Contains the collection of classes that implement the .NET data provider for SQL Server.

System.Data.SqlTypes Implements native data types for SQL Server that are faster than using regular .NET data types in representing SQL Server data.

System.Diagnostics Provides the classes that let you do tracing and interact with performance counters, event logs, and system processes.

.NET Framework Base Class Library 15

System.DirectoryServices Provides the classes and interfaces to interact with the Active Directory and other directory services (such as LDAP, IIS metabase, NDS, Windows NT).

System.Drawing Provides the basic functionality of GDI+ in your .NET code.

System.Drawing.Drawing2D Provides the advanced GDI+ features to manage 2D and vector drawing like brushes and matrices.

System.Drawing.Imaging Provides advanced GDI+ functionality to manipulate images.

System.Drawing.Printing Provides the classes to monitor and implement printing.

System.Drawing.Text Provides advanced GDI+ functionality to manage typography (fonts).

System.EnterpriseServices Provides .NET objects a way to integrate with COM+ services.

System.IO Allows for synchronous and asynchronous writing to and reading from files and data streams.

System.Management Provides access information and events from applica-tions or devices written to the Windows Management Instrumentation (WMI) API.

System.Messaging Provides the classes and interfaces necessary to work with Message Queueing.

System.Net Provides an easy way to work with many Internet proto-cols without having to worry about the specifics of the protocol. HTTP is well supported in this namespace.

System.Net.Sockets Provides the Windows Sockets interface to .NET developers.

System.Reflection Contains the classes and interfaces to dynamically view the classes, methods, and properties of types loaded in managed code and to invoke or dynamically create types.

System.Security The basic structure for the .NET security system.

System.Text Contains class and the means to convert betweencharacter encodings like ASCII, UTF-8, UTF-7, Unicode.

System.Text.RegularExpressions Contains classes to use regular expressions in .NET code.

T A B L E 1 . 3 Useful .NET Base Class Library Namespaces (continued)

Namespace Description

16 Chapter 1 � A .NET Framework Overview

System.Threading Provides classes to implement multithreaded program-ming and manage thread scheduling, wait notification, and deadlock resolution.

System.Timers Provides a component that raises an event on a specified interval.

System.Web Provides the classes to communicate with a browser via HTTP. Base namespace for ASP.NET.

System.Web.Caching Provides the classes to control caching of objects on proxy servers and by the browser.

System.Web.Hosting Provides functionality for hosting ASP.NET applications outside of IIS.

System.Web.Mail Allows you to construct and send an e-mail message using Collaboration Data Objects for Windows 2000 (CDOSYS).

System.Web.Services Provides the classes to support creating XML Web services using ASP.NET.

System.Web.Services.Description Provides the classes to generate the Web Services Description Language (WSDL) that describes the public interface of the Web service.

System.Web.Services.Discovery Provides classes that allow clients to locate Web services on a server.

System.Web.Services.Protocols Contains classes that defines the protocols used to transmit information exchanged between the client and the Web service.

System.Web.UI Contains the classes and interfaces necessary to create ASP.NET HTML, web server controls, and pages.

System.Web.UI.HtmlControls Contains classes that allow you to create HTML controls.

System.Web.UI.WebControls Contains classes that allow you to create web server controls.

System.Windows.Forms Contains the classes to create Windows applications.

System.Xml Contains classes that provide standards-based support for processing XML.

T A B L E 1 . 3 Useful .NET Base Class Library Namespaces (continued)

Namespace Description

Summary 17

You can look up more detailed information on each of these namespaces by typing the namespace name in Help � Index.

SummaryThe .NET Framework provides the tools and services to enable you to build Windows and Web applications. It is based on Internet standards such as HTTP, SOAP, TCP/IP, etc. where appropriate. The code you create is compiled to an intermediate language, Microsoft Intermediate Language, that executes on the Common Language Runtime. The CLR provides services that manage garbage collection, threading, and loading code, to name a few. The code that is run on the CLR is stored in one or more library or executable files called an assembly. An assembly stores its metadata, called the manifest, in itself. This manifest contains information about the types contained in and used by the assembly, security settings for the code, how the code in the assembly should interact with COM and COM+, and many other useful items. The assembly is loaded into an application domain, which isolates the running code from other applications running in other app domains. This provides stability without sacrificing performance, because app domains exist in the same process.

The .NET framework contains a large collection of types that are divided into namespaces. Namespaces logically organize your types (such as classes, interfaces, etc.) and provide unique identifiers for types that you may use in your code. Many of these types are contained in the base class library of the .NET Framework. This is one of the powerful features of developing with .NET. Libraries are provided to make working with everything from data in a database to XML to Web services easier.

System.Xml.Schema Contains classes that provide standards-based support for Extensible Schema Definition (XSD) files.

System.Xml.XPath Contains classes that provide standards-based support for W3C XML Path Language (XPath) version 1.0.

System.Xml.Xsl Contains classes that provide standards-based support for Extensible Stylesheet Language Transformation (XSLT) transforms version 1.0.

Microsoft.CSharp Supports code compilation and generation of C# code.

Microsoft.VisualBasic Supports code compilation and generation of VB .NET code. This is where all of the familiar VB functions, such as Mid, Chr, and Str, reside.

T A B L E 1 . 3 Useful .NET Base Class Library Namespaces (continued)

Namespace Description

18 Chapter 1 � A .NET Framework Overview

Key TermsBefore taking the exam, be certain you are familiar with the following terms:

application domain (app domain) manifest

assembly Microsoft Intermediate Language

boxing namespace

Common Language Runtime reference type

Common Language Specification type

Common Type System unboxing

managed code value type

Review Questions 19

Review Questions1. Elliott is trying to use a .NET class called SqlConnection in his Web application. When he

tries to build the application, it fails with the following syntax error: Type ‘SqlConnection’ is not defined. What code should he add to his application to resolve the problem if the SqlConnection class is in the System.Data.SqlClient namespace?

A. Get System.Data.SqlClient

B. Imports System.Data.SqlClient

C. Using System.Data.SqlClient

D. Namespace System.Data.SqlClient

2. What is an assembly?

A. An assembly is just one DLL.

B. An assembly is what an application domain is loaded into.

C. An assembly is the entity in .NET for versioning, deploying, and securing your code. It is contained in one or more DLLs.

D. An assembly is the entity in .NET for deploying and securing your code. It is contained in one or more DLLs. It has nothing to do with the versioning of code.

3. You want to declare an integer type that will hold a value from 0 to 30000. What VB .NET type would you use, without using more memory than necessary?

A. Integer

B. Long

C. Short

D. Byte

4. Which of the following are reasons to use a separate application domain in a project? (Choose all that apply.)

A. You would like to unload an assembly at a later time. Assemblies can only be unloaded from memory if the entire app domain is shut down.

B. You are running several tasks that need to be isolated from each other and don’t want to incur the overhead of separate processes.

C. You need to provide fewer rights to less-trusted assemblies than they would receive in the default policy for the app domain.

D. You need to achieve the maximum performance of the system without regard to the stability of the system.

20 Chapter 1 � A .NET Framework Overview

5. Which of the following namespaces provides the classes to access data through ADO.NET?

A. System.Reflection

B. System.Web

C. System.Data

D. System.Windows

6. How would you create an alias of xyzib for XYZCo.Widgets.IconButton class?

A. Imports Alias xyzib = XYZCo.Widgets.IconButton

B. Imports XYZCo.Widgets.IconButton As xyzib

C. Imports xyzib = XYZCo.Widgets.IconButton

D. Alias xyzib = XYZCo.Widgets.IconButton

7. Which of the following namespaces provides the classes to dynamically get the types and methods in the executing assembly?

A. System.Data

B. System.Web

C. System.Windows

D. System.Reflection

8. What is the name of the CLR service that cleans up objects allocated on the heap?

A. heap cleaner

B. garbage collector

C. garbage clearer

D. trash compactor

Answers to Review Questions 21

Answers to Review Questions1. B. Importing the namespace is required if you use just the class name. You could also refer to the

class as System.Data.SqlClient.SqlConnection if you don’t want to import the namespace.

2. C. An assembly is not just one DLL, but can be spread across multiple files (although if you use Visual Studio .NET, it is compiled into one file). Assemblies are loaded into application domains and not the other way around as in answer B. It is a unit of versioning, so answer D is incorrect.

3. C. The type Short is new to Visual Basic .NET and represents a 16-bit Integer holding a value from –32768 to 32767; this is the type that will use the minimum amount of memory. An Integer would work but it is 32 bits long in Visual Basic .NET and would use more memory than a Short. In Visual Basic 6, this was a 16-bit value. A Long would work also, but it is 64 bits long and would consume more memory than necessary. A Byte is too small; being 8-bit unsigned, it can only hold a value from 0 to 255.

4. A, B, C. Application domains provide a boundary to isolate running processes from each other; assemblies can be unloaded only by unloading an app domain; and you can give an app domain fewer rights. All this comes with some performance overhead, especially since data needs to be marshaled from one app domain to another. Thus answer D is not correct, because everything running as threads in one app domain runs more quickly, but is potentially less stable.

5. C. The System.Data namespace contains the classes, interfaces, structures, and enumerations that implement ADO.NET. System.Reflection provides information about assemblies, classes, methods, and properties at runtime. System.Web contains the classes used by ASP.NET. System.Windows has the necessary classes to create and support Windows applications.

6. C. You use an equal sign in the Imports statement to create an alias. Remember that aliases can be created for namespaces and types. The other answers are incorrect syntax.

7. D. The System.Reflection namespace provides a dynamic view of the types, methods, and properties in the assembly and allows you to dynamically invoke them at runtime. The System.Data namespace contains the classes, interfaces, structures, and enumerations that implement ADO.NET. System.Web contains the classes used by ASP.NET for Web applica-tions. System.Windows has the necessary classes to create and support Windows applications.

8. B. The garbage collector is the mechanism by which the CLR reclaims memory and prevents memory leaks. The System.GC class provides methods to use the garbage collector.