Creating .NET Applications Using C

 
Chapter 1 - C# and .NET Architecture
bySimon Robinsonet al.
Wrox Press 2002
  

Creating .NET Applications Using C#

C# can be used to create console applications: text-only applications that run in a DOS window. You'll probably use console applications when unit testing class libraries, and for creating Unix/Linux daemon processes. However, more often you'll use C# to create applications that use many of the technologies associated with .NET. In this section, we'll give you an overview of the different types of application that you can write in C#.

Creating ASP.NET Applications

ASP was a Microsoft technology for creating web pages with dynamic content. An ASP page is basically an HTML file with embedded chunks of server-side VBScript or JavaScript. When a client browser requested an ASP page, the web server would deliver the HTML portions of the page, processing the server-side scripts as it came to them. Often these scripts would query a database for data, and mark up that data in HTML. ASP was an easy way for clients to build browser-based applications.

ASP was not, however, without its shortcomings. First, ASP pages sometimes rendered slowly because the server-side code was interpreted instead of compiled. Second, ASP files could be difficult to maintain because they were unstructured; the server-side ASP code and plain HTML were all jumbled up together. Third, ASP sometimes made development difficult because there was little support for error handling and type-checking. Specifically, if you were using VBScript and wanted to implement error handling in your pages, you had to use the On Error Resume Next statement, and follow every component call with a check to Err.Number to make sure that the call had gone well.

ASP.NET is a revision of ASP that fixes many of its problems. It does not replace ASP; rather, ASP.NET pages can live side by side on the same server with legacy ASP applications. Of course, you can also program ASP.NET with C#!

Although subsequent chapters (14-16) discuss ASP.NET in greater detail, let's take a moment to explore some of its more significant features.

Features of ASP.NET

First, and perhaps most importantly, ASP.NET pages are structured . That is, each page is effectively a class that inherits from the .NET System.Web.UI.Page class, and can override a set of methods that are evoked during the Page object's lifetime. (You can think of these events as page-specific cousins of the OnApplication_Start and OnSession_Start events that went in the global.asa files of plain old ASP.) Because you can factor a page's functionality into event handlers with explicit meanings, ASP.NET pages are easier to understand.

Another nice thing about ASP.NET pages is that you can create them in VS.NET, the same environment in which you create the business logic and data access components that those ASP pages use. A VS.NET project group , or solution , contains all of the files associated with an application. Moreover, you can debug your ASP pages in the editor as well; in the old days of Visual InterDev, it was often a vexing challenge to configure InterDev and the project's web server to turn debugging on.

For maximum clarity, ASP.NET's code-behind feature lets you take the structured approach even further. ASP.NET allows you to isolate the server-side functionality of a page to a class, compile that class into a DLL, and place that DLL into a directory below the HTML portion. A code-behind directive at the top of the page associates the file with its DLL. When a browser requests the page, the web server fires the events in the class in the page's code-behind DLL.

Last but not least, ASP.NET is remarkable for its increased performance. Whereas ASP pages are interpreted with each page request, the web server caches ASP.NET pages after compilation. This means that subsequent requests of an ASP.NET page execute more quickly than the first.

ASP.NET also makes it easy to write pages that cause forms to be displayed by the browser, which you might use in an intranet environment. The traditional wisdom is that form-based applications offer a richer user interface, but are harder to maintain because they run on so many different machines. For this reason, people have relied on form-based applications when rich user interfaces were a necessity and extensive support could be provided to the users.

With the advent of Internet Explorer 5 and the lackluster performance of Navigator 6, however, the advantages of form-based applications are clouded. IE 5's consistent and robust support for DHTML allows the programmer to create web-based applications that are every bit as pretty as their fat client equivalents. Of course, such applications necessitate standardizing on IE and not supporting Navigator. In many industrial situations, this standardization is now common.

Web Forms

To make web page construction even easier, Visual Studio .NET supplies Web Forms . They allow you to build ASP.NET pages graphically in the same way that VB 6 or C++ Builder windows are created; in other words, by dragging controls from a toolbox onto a form, then flipping over to the code aspect of that form, and writing event handlers for the controls. When you use C# to create a Web Form, you are creating a C# class that inherits from the Page base class, and an ASP page that designates that class as its code-behind. Of course, you don't have to use C# to create a Web Form; you can use VB.NET or another .NET language just as well.

In the past, the difficulty of web development has discouraged some teams from attempting it. To succeed in web development, you had to know so many different technologies, such as VBScript, ASP, DHTML, JavaScript, and so on. By applying the Form concepts to web pages, Web Forms promise to make web development easier. Only time will tell, however, how successful Web Forms and Web Controls (which we'll look at next) will be at insulating the developer from the complexities of web design.

Web Controls

The controls used to populate a Web Form are not controls in the same sense as ActiveX controls. Rather, they are XML tags in the ASP namespace that the web browser dynamically transforms into HTML and client-side script when a page is requested. Amazingly, the web server is able to render the same server-side control in different ways, producing a transformation that is appropriate to the requestor 's particular web browser. This means that it is now easy to write fairly sophisticated user interfaces for web pages, without having to worry about how to ensure that your page will run on any of the available browsers because Web Forms will take care of that for you.

You can use C# or VB.NET to expand the Web Form toolbox. Creating a new server-side control is simply a matter of implementing .NET's System.Web.UI.WebControls.WebControl class.

Web Services

Today, HTML pages account for most of the traffic on the World Wide Web. With XML, however, computers have a device-independent format to use for communicating with each other on the Web. In the future, computers may use the Web and XML to communicate information rather than dedicated lines and proprietary formats such as EDI ( Electronic Data Interchange ). Web Services are designed for a service-oriented web, in which remote computers provide each other with dynamic information that can be analyzed and re-formatted, before final presentation to a user. A Web Service is an easy way for a computer to expose information to other computers on the Web in the form of XML.

In technical terms, a Web Service on .NET is an ASP.NET page that returns XML instead of HTML to requesting clients. Such pages have a code-behind DLL containing a class that derives from the WebService class. The VS.NET IDE provides an engine that facilitates Web Service development.

There are two main reasons that an organization might choose to use Web Services. The first reason is that because they rely on HTTP, Web Services can use existing networks (the Web) as a medium for conveying information. The other is that because Web Services use XML, the data format is self-describing , non-proprietary, and platform-independent.

Creating Windows Forms

Although C# and .NET are particularly suited to web development, they still offer splendid support for so-called "fat client" apps, applications that have to be installed on the end-user's machine where most of the processing takes place. This support is from Windows Forms .

A Windows Form is the .NET answer to a VB 6 Form. To design a graphical window interface, you just drag controls from a toolbox onto a Windows Form. To determine the window's behavior, you write event-handling routines for the form's controls. A Windows Form project compiles to an EXE that must be installed alongside the .NET runtime on the end user's computer. Like other .NET project types, Windows Form projects are supported by both VB.NET and C#. We will be examining Windows Forms more closely in Chapter 7.

Windows Controls

Although Web Forms and Windows Forms are developed in much the same way, you use different kinds of controls to populate them. Web Forms use Web Controls, and Windows Forms use Windows Controls .

A Windows Control is a lot like an ActiveX control. After a Window control is implemented, it compiles to a DLL that must be installed on the client's machine. In fact, the .NET SDK provides a utility that creates a wrapper for ActiveX controls, so that they can be placed on Windows Forms. As is the case with Web Controls, Windows Control creation involves deriving from a particular class, System.Windows.Forms.Control .

Windows Services

A Windows Service (originally called an NT Service) is a program that is designed to run in the background in Windows NT/2000/XP (but not Windows 9x). Services are useful where you want a program to be running continuously and ready to respond to events without having been explicitly started by the user. A good example would be the World Wide Web Service on web servers, which listens out for web requests from clients.

It is very easy to write services in C#. There are .NET Framework base classes available in the System.ServiceProcess namespace that handle many of the boilerplate tasks associated with services, and in addition, Visual Studio .NET allows you to create a C# Windows Service project, which starts you out with the Framework C# sourcecode for a basic Windows service. We'll explore how to write C# Windows Services in Chapter 22.

  


Professional C#. 2nd Edition
Performance Consulting: A Practical Guide for HR and Learning Professionals
ISBN: 1576754359
EAN: 2147483647
Year: 2002
Pages: 244

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net