.NET Framework Essentials, 2nd Edition by Hoang Lam and Thuan L. Thai


375766876d8f602.jpg Author Hoang Lam and Thuan L. Thai
Isbn 9780596003029
File size 3.6 MB
Year 2002
Pages 320
Language English
File format PDF
Category it ebooks


 

.NET Framework Essentials, 2nd Edition Thuan L. Thai Hoang Lam Publisher: O'Reilly Second Edition February 2002 ISBN: 0-596-00302-1, 320 pages .NET Framework Essentials, 2nd Edition is a concise and technical overview of the Microsoft .NET Framework. Covered here are all of the most important topics—from the underlying Common Language Runtime (CLR) to its specialized packages for ASP.NET, Web Forms, Windows Forms, XML and data access (ADO.NET). The authors survey each of the major .NET languages, including Visual Basic .NET, C# and Managed C++. www.it-ebooks.info Table of Contents Preface ..................................................... Audience ................................................... About This Book .............................................. Assumptions This Book Makes .................................... Conventions Used in This Book .................................... How to Contact Us ............................................. Acknowledgments ............................................. 1 1 1 2 3 3 4 1. .NET Overview .............................................. 1.1 Microsoft .NET ............................................ 1.2 The .NET Platform .......................................... 1.3 .NET Framework Design Goals .................................. 1.4 .NET Framework ........................................... 5 5 6 7 11 2. The Common Language Runtime ................................. 2.1 CLR Environment .......................................... 2.2 CLR Executables ........................................... 2.3 Metadata ................................................. 2.4 Assemblies and Manifests ..................................... 2.5 Intermediate Language (IL) .................................... 2.6 The CTS and CLS .......................................... 2.7 CLR Execution ............................................ 2.8 Summary ................................................ 14 14 14 19 25 30 32 37 41 3. .NET Programming ........................................... 3.1 Common Programming Model .................................. 3.2 Core Features and Languages ................................... 3.3 Language Integration ......................................... 3.4 Summary ................................................ 42 42 44 56 60 4. Working with .NET Components ................................. 4.1 Deployment Options ......................................... 4.2 Distributed Components ...................................... 4.3 COM+ Services in .NET ...................................... 4.4 Message Queuing ........................................... 4.5 Summary ................................................ 62 62 71 74 85 88 5. Data and XML .............................................. 89 5.1 ADO.NET Architecture ....................................... 89 5.2 ADO.NET Benefits .......................................... 90 5.3 Content Components ......................................... 93 5.4 Managed Providers .......................................... 105 5.5 DataSets and XML .......................................... 115 5.6 Summary ................................................ 125 6. Web Services ............................................... 6.1 Web Services in Practice ...................................... 6.2 Web Services Framework ...................................... 6.3 Web Services Provider ....................................... 6.4 Web Services Consumers ...................................... 6.5 Web Services and Security ..................................... 6.6 Summary ................................................ www.it-ebooks.info 126 126 127 137 142 159 160 7. Web Forms ................................................. 7.1 ASP .................................................... 7.2 ASP.NET ................................................ 7.3 The System.Web.UI Namespace ................................. 7.4 Web Form Syntax ........................................... 7.5 ASP.NET Application Development .............................. 7.6 ASP.NET and Web Services .................................... 7.7 Data Binding and the Use of Templates ............................ 7.8 State Management and Scalability ................................ 7.9 Summary ................................................ 162 162 163 163 171 177 190 193 198 203 8. Windows Forms ............................................. 8.1 Introducing Windows Forms .................................... 8.2 The System.Windows.Forms Namespace ........................... 8.3 Windows Forms Development .................................. 8.4 Windows Forms and Web Services ............................... 8.5 Conclusion ............................................... 204 204 205 210 232 232 A. .NET Languages ............................................. 234 A.1 Microsoft-Supported Languages for .NET .......................... 234 A.2 Third-Party Languages for .NET ................................. 234 B. Common Acronyms .......................................... 236 C. Common Data Types ......................................... 239 C.1 Usage .................................................. 240 D. Common Utilities ............................................ D.1 Assembly Generation Utility (al.exe) .............................. D.2 Assembly Registration Utility (gacutil.exe) .......................... D.3 MSIL Assembler (ilasm.exe) ................................... D.4 MSIL Disassembler (ildasm.exe) ................................ D.5 C++ Compiler (cl.exe) ....................................... D.6 C# Compiler (csc.exe) ....................................... D.7 Visual Basic Compiler (vbc.exe) ................................. D.8 PE File Format Viewer (dumpbin.exe) ............................. D.9 Type Library Exporter (tlbexp.exe) ............................... D.10 Type Library Importer (tlbimp.exe) .............................. D.11 XML Schema Definition Tool (xsd.exe) ........................... D.12 Shared Name Utility (sn.exe) .................................. D.13 Web Service Utility (wsdl.exe) ................................. 245 245 246 246 247 247 247 248 248 249 249 250 251 251 Colophon .................................................... 253 www.it-ebooks.info .NET Framework Essentials, 2nd Edition Preface A condensed introduction to the Microsoft .NET Framework, this book aims to help programmers make the transition from traditional Windows programming into the world of .NET programming. The Microsoft .NET Framework includes the Common Language Runtime (CLR) and a set of base classes that radically simplify the development of large-scale applications and services. This book examines the CLR in detail, so that you can put its new features to good use. The book also illustrates how language integration really works and guides you through component and enterprise development using the .NET Framework. In addition, it introduces you to four key .NET technologies: Data (ADO.NET) and XML, Web Services, Web Forms (ASP.NET), and Windows Forms. We used the latest release of Microsoft Visual Studio .NET and the .NET Framework SDK to prepare this manuscript and to develop all the examples and figures in this book. While we have done our best to ensure that the technical content of this book is up-to-date, it is possible that some items have changed slightly from the time of writing. To stay up-to-date, regularly check http://msdn.microsoft.com/net, http://www.gotdotnet.com/, and this book's O'Reilly page, http://www.oreilly.com/catalog/dotnetfrmess2/. Audience While this book is for any person interested in learning about the Microsoft .NET Framework, it targets seasoned developers with experience in building Windows applications with Visual Studio 6 and the Visual Basic and Visual C++ languages. Java™ and C/C++ developers will also be well prepared for the material presented here. To gain the most from this book, you should have experience in object-oriented, component, enterprise, and web application development. COM programming experience is a plus. About This Book Based on a short course that Thuan has delivered to numerous companies since August 2000, this book is designed so that each chapter builds on knowledge from the previous one for those unfamiliar with each technology. To give you a heads-up, here are brief summaries for the chapters and appendixes covered in this book. Chapter 1 takes a brief look at Microsoft .NET and the Microsoft .NET Platform. It then describes the .NET Framework design goals and introduces you to the components of the .NET Framework. Chapter 2 lifts the hood and peers into the CLR. This chapter surveys the rich runtime of the CLR, as well as other features. Chapter 3 introduces you to .NET programming. You'll examine a simple program that uses object-oriented and component-based concepts in four different languages: Managed C++, VB.NET, C#, and IL. You'll also experience the benefits of language integration. Chapter 4 demonstrates the simplicity of component and enterprise development in .NET. Besides seeing component-deployment features, you'll also examine complete programs that 1 www.it-ebooks.info .NET Framework Essentials, 2nd Edition take advantage of transaction, object pooling, role-base security, and message queuing—all in one chapter. Chapter 5 describes the architecture of ADO.NET and its benefits. Besides being disconnected to promote scalability, the ADO.NET dataset is also tightly integrated with XML to enhance interoperability. This chapter introduces you to the .NET data-access objects, as well as the XML namespace. Chapter 6 describes the next generation of software components that can be accessed through the Internet. In this chapter, we discuss the protocols that support Web Services, as well as how to publish and discover them. You will see how XML, used in conjunction with HTTP, breaks the proprietary nature of current component-oriented software development and enables greater interoperability. Chapter 7 introduces you to ASP.NET, which now supports object-oriented and event-driven programming, as opposed to conventional ASP development. In this chapter, Web Forms and server controls take the center stage. In addition, we examine how to build custom server controls, perform data binding to various .NET controls, and survey state management features in ASP.NET. Chapter 8 takes conventional form-based programming a step into the future with the classes in the System.Windows.Forms namespace. Similar to Win32-based applications, Windows Forms are best used for to build so-called rich or "fat" clients; however, with the new zeroeffort installation procedure of .NET and the advent of Web Services, Windows Forms are appropriate for a host of applications. Appendix A contains a list of links to web sites with information regarding languages that targets the CLR, including some burgeoning open source projects. Appendix B contains a list of commonly used acronyms that are used in .NET literature and presentations. Appendix C contains several lists of commonly used datatypes in .NET. This appendix also illustrates the use of several of its collection classes. Appendix D surveys the important tools that the .NET SDK provides to ease the tasks of .NET development. Now that you know what this book is about, we should explain what it is not about. This book does not focus on the marketing aspects of .NET or on other components of the .NET Platforms, including .NET Enterprise Servers, .NET Building Block Services, or .NET Operating Systems. Likewise, we do not cover the recently announced HailStorm service or the work Microsoft is doing to make the .NET Framework available on a host of devices. Assumptions This Book Makes This book assumes that you are a Windows and web application developer fluent in objectoriented and component-based programming. It also assumes that you have some basic knowledge of XML. While COM is not a crucial prerequisite, if you have COM programming experience, you will appreciate this book and the .NET Framework all the more. 2 www.it-ebooks.info .NET Framework Essentials, 2nd Edition Conventions Used in This Book We use the following font conventions in this book. Italic is used for: • • • Pathnames, filenames, and program names Internet addresses, such as domain names and URLs New terms where they are defined Constant width is used for: • • • Command lines and options that should be typed verbatim Direct quotes and specific method names from code examples, as well as specific values for attributes and settings within code XML element tags Constant width bold is used for: • • User input in code that should be typed verbatim Items in code to which we'd like to draw the reader's attention Constant width italic is used for replaceable items in code, which should be replaced with the appropriate terms. In code syntax examples, we sometimes use [value]+ to represent one or more instances of a value and [value]* to mean zero or more instances of a value. How to Contact Us We have tested and verified the information in this book to the best of our ability, but you may find that features have changed (or even that we have made mistakes!). Please let us know about any errors you find, as well as your suggestions for future editions, by writing to: O'Reilly & Associates, Inc. 1005 Gravenstein Highway North Sebastopol, CA 95472 (800) 998-9938 (in the United States or Canada) (707) 829-0515 (international/local) (707) 829-0104 (FAX) You can also send us messages electronically. To be put on the mailing list or request a catalog, send email to: [email protected] To ask technical questions or comment on the book, send email to: [email protected] 3 www.it-ebooks.info .NET Framework Essentials, 2nd Edition We have a web site for the book, where we'll list examples, errata, and any plans for future editions. You can access this page at: http://www.oreilly.com/catalog/dotnetfrmess2/ For more information about this book and others, see the O'Reilly web site: http://www.oreilly.com/ For more information on .NET in general, visit the O'Reilly .NET Center at http://dotnet.oreilly.com/ and the .NET DevCenter at http://www.oreillynet.com/dotnet/. Acknowledgments The folks at O'Reilly never cease to amaze us with the support that they provide. We'd like to thank John Osborn for extending us the contract to write this book and for his continuous support throughout the project. We'd also like to thank Nancy Kotary for the hard work that she went through to get the book out under a rigorous schedule. Nancy did a great job reviewing our materials and coordinating the project. Without John and Nancy, this book would not have been possible. Thanks to the production and design folks at O'Reilly for making this book a reality: Emma Colby, Tatiana Diaz, David Futato, Colleen Gorman, Robert Romano, Mike Sierra, Ellie Volckhausen, and Joe Wizda. Thanks to Brian Jepson who has contributed significantly to this book since the beginning of this project. Brian did an unquestionably outstanding job reading, testing, and ensuring that the technical content in every chapter lines up with the latest release. He also gave us invaluable guidance and support throughout this project. We'd also like to thank Dennis Angeline and Brad Merrill at Microsoft for answering technical questions on the CLR and languages. Hoang would like to thank his parents and family for their support and understanding of his being missing-in-action for several months. Mom and Dad, your ongoing efforts to put your children where they are today can never be repaid. Hoang would like to thank his wife, VanDu, the source of his inspiration. Don't underestimate your contribution to this book. And last, but not least, a personal thank you to Thuan, who has always pushed me toward the bleeding edge. 4 www.it-ebooks.info .NET Framework Essentials, 2nd Edition Chapter 1. .NET Overview Microsoft announced the .NET intitiative in July 2000. The .NET platform is a new development framework with a new programming interface to Windows services and APIs, integrating a number of technologies that emerged from Microsoft during the late 1990s. Incorporated into .NET are COM+ component services; the ASP web development framework; a commitment to XML and object-oriented design; support for new web services protocols such as SOAP, WSDL, and UDDI; and a focus on the Internet. The platform consists of four separate product groups: Development tools A set of languages, including C# and VB.NET; a set of development tools, including Visual Studio.NET; a comprehensive class library for building web services and web and Windows applications; as well as the Common Language Runtime to execute objects built within this framework. Specialized servers A set of .NET Enterprise Servers, formerly known as SQL Server 2000, Exchange 2000, BizTalk 2000, and so on, that provide specialized functionality for relational data storage, email, and B2B commerce. Web services An offering of commercial web services, specifically the .NET My Services initiative (formerly called HailStorm); for a fee, developers can use these services in building applications that require knowledge of user identity. Devices New .NET-enabled non-PC devices, from cell phones to game boxes. Microsoft is devoting considerable resources to the development and success of .NET and related technologies: their bets are on .NET as the next big thing in computing. 1.1 Microsoft .NET Microsoft has spent the last four years creating Microsoft .NET, which was publicly launched at PDC 2000 in Orlando, Florida. While the main strategy of .NET is to enable software as a service, .NET is much more than that. Aside from embracing the Web, Microsoft .NET acknowledges and responds to the following trends within the software industry today: Distributed computing Simplifies the development of robust client/server applications. Current distributed technologies require high vendor-affinity and lack interoperation with the Web. Microsoft .NET provides a remoting architecture that exploits open Internet standards, 5 www.it-ebooks.info .NET Framework Essentials, 2nd Edition including the Hypertext Transfer Protocol (HTTP), Extensible Markup Language (XML), and Simple Object Access Protocol (SOAP). Componentization Simplifies the integration of software components developed by different vendors. The Component Object Model (COM) has brought reality to software plug-and-play, but COM component development and deployment are too complex. Microsoft .NET provides a simpler way to build and deploy components. Enterprise services Allow the development of scalable enterprise applications without writing code to manage transactions, security, or pooling. Microsoft .NET continues to support enterprise services, since these services greatly reduce the development time and effort involved in building large-scale applications. Web paradigm shifts Represents changes in web technologies to simplify the development of web applications. Over the last few years, web application development has shifted from connectivity (TCP/IP), to presentation (HTML), to programmability (XML and SOAP). A key goal of Microsoft .NET is to enable software to be sold and distributed as a service. Maturity factors Represents lessons that the software industry has learned from developing large-scale enterprise and web applications. A commercial web application must support interoperability, scalability, availability, and manageability. Microsoft .NET facilitates all these goals. Although these are the main concepts that Microsoft .NET incorporates, what's more notable is that Microsoft .NET uses open Internet standards (HTTP, XML, and SOAP) at its core to transmit an object from one machine to another across the Internet. In fact, there is bidirectional mapping between XML and objects in .NET. For example, a class can be expressed as an XML Schema Definition (XSD); an object can be converted to and from an XML buffer; a method can be specified using an XML format called Web Services Description Language (WSDL); and an invocation (method call) can be expressed using an XML format called SOAP. 1.2 The .NET Platform The Microsoft .NET Platform consists of five main components, as shown in Figure 1-1. At the lowest layer lies the operating system (OS), which can be one of a variety of Windows platforms, including Windows XP, Windows 2000, Windows Me, and Windows CE. As part of the .NET strategy, Microsoft has promised to deliver more .NET device software to facilitate a new generation of smart devices. 6 www.it-ebooks.info .NET Framework Essentials, 2nd Edition Figure 1-1. The Microsoft .NET platform On top of the operating system is a series of .NET Enterprise Server products that shortens the time required to develop large-scale business systems. These server products include Application Center 2000, BizTalk Server 2000, Commerce Server 2000, Exchange Server 2000, Host Integration Server 2000, Internet Security and Acceleration Server 2000, and SQL Server 2000. Since Web Services are highly reusable across the Web, Microsoft plans to provide a number of building-block services that applications developers can use, for a fee. An example of building-block service is Microsoft Passport, which allows you to use a single username and password at all web sites that support Passport authentication. In March 2001, Microsoft announced another set of Web Services with the codename HailStorm, now called .NET My Services. This product encompasses a set of building-block services that support personalization, centered entirely on consistent user experiences.1 Microsoft plans to add newer services, such as calendar, directory, and search services. Third-party vendors are also creating new Web Services of their own. At the top layer of the .NET architecture is a brand new development tool called Visual Studio.NET (VS.NET), which makes possible the rapid development of Web Services and other applications. A successor of Microsoft Visual Studio 6.0, VS.NET is an Integrated Development Environment (IDE) that supports four different languages and features such as cross-language debugging and the XML Schema Editor. And at the center of .NET is the Microsoft .NET Framework—the main focus of this book. The .NET Framework is a new development and runtime infrastructure that will change the development of business applications on the Windows platform. It includes the Common Language Runtime (CLR) and a common framework of classes that can be used by all .NET languages. 1.3 .NET Framework Design Goals Inherent within the Microsoft .NET Framework are many design goals that are practical yet extremely ambitious. In this section, we discuss the main design goals of the Microsoft .NET Framework, including better support for components, language integration, application interoperation across cyberspace, simple development and deployment, better reliability, and greater security. 1 For more information on .NET My Services, see the forthcoming .NET My Services Essentials by Culbert and Murphy (O'Reilly). 7 www.it-ebooks.info .NET Framework Essentials, 2nd Edition 1.3.1 Component Infrastructure Prior to the existence of COM technology, Microsoft developers had no simple way to integrate binary libraries without referring to or altering their source code. With the advent of COM, programmers were able to integrate binary components into their applications, similar to the way we plug-and-play hardware components into our desktop PCs. Although COM was great, the grungy details of COM gave developers and administrators many headaches. While COM permits you to integrate binary components developed using any language, it does require you to obey the COM identity, lifetime, and binary layout rules. You must also write the plumbing code that is required to create a COM component, such as DllGetClassObject, CoRegisterClassObject, and others. Realizing that these requirements result in frequent rewrites of similar code, .NET sets out to remove them. In the .NET world, all classes are ready to be reused at the binary level. You don't have to write extra plumbing code to support componentization in the .NET Framework. You simply write a .NET class, which then becomes a part of an assembly (to be discussed in Chapter 2), and supports plug-and-play.2 In addition to providing a framework to make development easier, .NET removes the pain of developing COM components. Specifically, .NET removes the use of the registry for component registration and eliminates the requirements for extraneous plumbing code found in all COM components, including code to support IUnknown, class factories, component lifetime, registration, dynamic binding, and others. "Component" is a nasty word because one person may use it to refer to an object and another may use it to refer to a binary module. To be consistent, this book uses the term "COM component" (or simply "component") to refer to a binary module, such as a DLL or an EXE. 1.3.2 Language Integration COM supports language independence, which means that you can develop a COM component in any language you want. As long as your component meets all the rules spelled out in the COM specification, it can be instantiated and used by your applications. While this supports binary reuse, it doesn't support language integration. In other words, you can't reuse the code in the COM components written by someone else; you can't extend a class hosted in the COM component; you can't catch exceptions thrown by code in the COM component; and so forth. Microsoft .NET supports not only language independence, but also language integration. This means that you can inherit from classes, catch exceptions, and take advantage of polymorphism across different languages. The .NET Framework makes this possible with a specification called the Common Type System (CTS), which all .NET components must support. For example, everything in .NET is an object of a specific class that derives from the root class called System.Object. The CTS supports the general concepts of classes, interfaces, 2 COM still plays a role in the .NET Framework. In fact, if you use dumpbin.exe to dump a Portable Executable (PE) file created by the compilers available in the prerelease or Beta l version of the .NET SDK, you will see some COM residues, specifically a mention of something called the COM+Header. See Section 2.2.4 in Chapter 2 for more information. 8 www.it-ebooks.info .NET Framework Essentials, 2nd Edition delegates (which support callbacks), reference types, and value types. The .NET base classes provide most of the base system types, such as ones that support integer, string, and file manipulation. Because every language compiler must meet a minimum set of rules stipulated by the Common Language Specification (CLS) and generate code to conform to the CTS, different .NET languages can intermingle with one another. We will examine the CTS and CLS in Chapter 2. 1.3.3 Internet Interoperation COM supports distributed computing through its Distributed COM (DCOM) wire protocol. A problem with DCOM is that it embeds the host TCP/IP address inside the Network Data Representation (NDR) buffer, such that it will not work through firewalls and Network Address Translation (NAT) software. In addition, the DCOM dynamic activation, protocol negotiation, and garbage-collection facilities are proprietary, complex, and expensive. The solution is an open, simple, and lightweight protocol for distributed computing. The .NET Framework uses the new industry-supported SOAP protocol, which is based on the widely accepted XML and HTTP standards. 1.3.4 Simple Development If you have developed software for the Windows platforms since their appearance, you have seen everything from the Windows APIs to the Microsoft Foundation Classes (MFC), the Active Template Library (ATL), the system COM interfaces, and the countless other environments, such as Visual Interdev, Visual Basic, JScript, and other scripting languages. Each time you set out to develop something in a different compiler, you had to learn a new API or a class library, because there is no consistency or commonality among these different libraries or interfaces. The .NET solution provides a set of framework classes and lets every language use it. Such a framework removes the need for learning a new API each time you switch languages. Put differently, it's certainly easier to go through ten methods of a particular class than to go through a thousand API functions. 1.3.5 Simple Deployment Imagine this scenario: your Windows application, which uses three shared DLLs, works just fine for months, but stops working one day after you've installed another software package that overwrites the first DLL, does nothing to the second DLL, and adds an additional copy of the third DLL into a different directory. If you have ever encountered such a brutal—yet entirely possible—problem, you have entered DLL Hell. And if you ask a group of seasoned developers whether they have experienced DLL Hell, they will grimace at you in disgust, not because of the question you've posed, but because they have indeed experienced the pain and suffering. To avoid DLL Hell on Windows 2000 (at least for system DLLs), Windows 2000 stores system DLLs in a cache. If you install an application that overwrites system DLLs, Windows 2000 will overwrite the added system DLLs with the original versions from the cache. Microsoft .NET further diminishes DLL Hell. In the .NET environment, your executable will use the shared DLL with which it was built. This is guaranteed, because a shared DLL must 9 www.it-ebooks.info .NET Framework Essentials, 2nd Edition be registered against something similar to the Windows 2000 cache, called the Global Assembly Cache (GAC). In addition to this requirement, a shared DLL must have a unique hash value, public key, locale, and version number. Once you've met these requirements and registered your shared DLL in the GAC, its physical filename is no longer important. In other words, if you have two versions of a DLL that are both called MyDll.dll, both of them can live and execute on the same system without causing DLL Hell. Again, this is possible because the executable that uses one of these DLLs is tightly bound to the DLL during compilation. In addition to eradicating DLL Hell, .NET also removes the need for component-related registry settings. A COM developer will tell you that half the challenge of learning COM is understanding the COM-specific registry entries for which the developer is responsible. Microsoft .NET stores all references and dependencies of .NET assemblies within a special section called a manifest (see Chapter 2). In addition, assemblies can be either private or shared. Private assemblies are found using logical paths or XML-based application configuration files, and public assemblies are registered in the GAC; in both cases the system will find your dependencies at runtime. If they are missing, you get an exception telling you exactly what happened. Finally, .NET brings back the concept of zero-impact installation and removal. This concept is the opposite of what you have to deal with in the world of COM. To set up a COM application, you have to register all your components after you have copied them over to your machine. If you fail to perform this step correctly, nothing will work and you'll end up pulling your hair out. Likewise, to uninstall the application, you should unregister your components (to remove the registry entries) prior to deleting your files. Again, if you fail to perform this step correctly, you will leave remnants in the registry that will be forever extant. Unlike COM, but like DOS, to set up an application in .NET, you simply xcopy your files from one directory on a CD to another directory on your machine, and the application will run automatically.3 Similarly, you can just delete the directory to uninstall the application from your machine. 1.3.6 Reliability There are many programming languages and platforms in the commercial software industry, but few of them attempt to provide both a reliable language and a robust runtime or infrastructure. The most successful language that we have seen in the commercial software industry is the Java™ language and the Java Virtual Machine™, which have brought the software-development community much satisfaction. Microsoft is positioning .NET as the next big thing. Microsoft .NET requires type safety. Unlike C++, every class in .NET is derived from the mother of all classes, Object, which supports runtime type-identification features, contentdumping features, and so on. The CLR must recognize and verify types before they can be loaded and executed. This decreases the chances for rudimentary programming errors and prevents buffer overruns, which can be a security weakness. Traditional programming languages don't provide a common error-handling mechanism. C++ and Java support exception handling, but many others leave you in the dust, forcing to invent 3 This is true for private assemblies, but not for shared assemblies. See Chapter 4 for more details. 10 www.it-ebooks.info .NET Framework Essentials, 2nd Edition your own error-handling facilities. Microsoft .NET supports exceptions in the CLR, providing a consistent error-handling mechanism. Put another way: exceptions work across all .NETcompatible languages. When you program in C++, you must deallocate all heap-based objects that you have previously allocated. If you fail to do this, the allocated resources on your system will never be reclaimed even though they are no longer needed. And if this is a server application, it won't be robust because the accumulation of unused resources in memory will eventually bring down the system. Similar to Java, the .NET runtime tracks and garbage-collects all allocated objects that are no longer needed. 1.3.7 Security When developing applications in the old days of DOS, Microsoft developers cared little about security because their applications ran on a single desktop with a single thread of execution. As soon as developers started developing client and server applications, things got a bit complicated: multiple users might then have accessed the servers, and sensitive data might be exchanged between the client and the server. The problem became even more complex in the web environment, since you could unknowingly download and execute malicious applets on your machine. To mitigate these problems, .NET provides a number of security features. Windows NT and Windows 2000 protect resources using access-control lists and security identities, but don't provide a security infrastructure to verify access to parts of an executable's code. Unlike traditional security support in which only access to the executable is protected, .NET goes further to protect access to specific parts of the executable code. For example, to take advantage of declarative security checks, you can prefix your method implementations with security attributes without having to write any code. To take advantage of imperative security checks, you write the code in your method to explicitly cause a security check. There are many other security facilities that .NET provides in an attempt to make it harder to penetrate your applications and system. 1.4 .NET Framework Now that you are familiar with the major goals of the .NET Framework, let's briefly examine its architecture. As you can see in Figure 1-2, the .NET Framework sits on top of the operating system, which can be a few different flavors of Windows,4 and consists of a number of components. (Each of these components is discussed in greater detail starting with Chapter 4, as described in the Preface.) .NET is essentially a system application that runs on Windows. 4 In fact, the operating system can be—potentially—any flavor of Unix or other operating systems. This is possible due to the architecture of the CLR, which is discussed in Chapter 2. 11 www.it-ebooks.info .NET Framework Essentials, 2nd Edition Figure 1-2. The .NET Framework The most important component of the Framework is something called the CLR. If you are a Java programmer, think of the CLR as the .NET equivalent of the Java Virtual Machine (JVM). If you don't know Java, think of the CLR as the heart and soul of the .NET architecture. At a high level, the CLR activates objects, performs security checks on them, lays them out in memory, executes them, and garbage-collects them. Conceptually, the CLR and the JVM are similar in that they are both runtime infrastructures that abstract the underlying platform differences. However, while the JVM currently supports only the Java language, the CLR supports all languages that can be represented in the Common Intermediate Language (CIL). The JVM executes bytecode, so it could technically support many different languages, too. Unlike Java's bytecode, though, IL is never interpreted. Another conceptual difference between the two infrastructures is that Java code runs on multiple platforms with a JVM, whereas .NET code runs only on the Windows platforms with the CLR (at the time of this writing). Microsoft has submitted the Common Language Infrastructure (CLI), which is functional a subset of the CLR, to ECMA, so a thirdparty vendor could theoretically implement a CLR for a platform other than Windows. For more information on third-party vendors, see Appendix A. In Figure 1-2, the layer on top of the CLR is a set of framework base classes. This set of classes is similar to the set of classes in STL, MFC, ATL, or Java. These classes support rudimentary input and output functionality, string manipulation, security management, network communications, thread management, text management, reflection functionality, and collections functionality, as well as other functions. On top of the framework base classes is a set of classes that extend the base classes to support data management and XML manipulation. The data classes support persistent data management—data that is stored on backend databases. These classes include the Structured Query Language (SQL) classes to let you manipulate persistent data stores through a standard SQL interface. Similar to the SQL classes, the set of classes called ADO.NET allow you to manipulate persistent data. Alongside of the data classes, the .NET Framework supports a number of classes to let you manipulate XML data, perform XML searching, and perform XML translations. 12 www.it-ebooks.info .NET Framework Essentials, 2nd Edition Classes in three different technologies (including Web Services, Web Forms, and Windows Forms) extend the framework base classes and the data and XML classes. Web Services include a number of classes that support the development of lightweight distributed components, which will work even in the face of firewalls and NAT software. These components support plug-and-play across cyberspace, because Web Services employ standard HTTP and SOAP. Web Forms include a number of classes that allow you to rapidly develop web Graphical User Interface (GUI) applications. If you're currently developing web applications with Visual Interdev, you can think of Web Forms as a facility that allows you to develop web GUIs using the same drag-and-drop approach as if you were developing the GUIs in Visual Basic. Simply drag and drop controls onto your Web Form, double-click on a control, and write the code to respond to the associated event. Windows Forms support a set of classes that allow you to develop native- Windows GUI applications. You can think of these classes collectively as a much better version of MFC because they support easier GUI development and provide a common, consistent interface that can be used in all languages. In the next chapter, we examine the internals of the CLR and how it supports and executes .NET components, formally called assemblies in .NET. 13 www.it-ebooks.info .NET Framework Essentials, 2nd Edition Chapter 2. The Common Language Runtime The most important component of the .NET Framework is the Common Language Runtime (CLR). The CLR manages and executes code written in .NET languages and is the basis of the .NET architecture, similar to the Java Virtual Machine. The CLR activates objects, performs security checks on them, lays them out in memory, executes them, and garbagecollects them. In this chapter, we describe the CLR environment, executables (with examples in several languages), metadata, assemblies, manifests, the CTS, and the CLS. 2.1 CLR Environment The CLR is the underlying .NET infrastructure. Its facilities cover all the goals that we spelled out in Chapter 1. Unlike software libraries such as MFC or ATL, the CLR is built from a clean slate. The CLR manages the execution of code in the .NET Framework. An assembly is the basic unit of deployment and versioning, consisting of a manifest, a set of one or more modules, and an optional set of resources. Figure 2-1 shows the two portions of the .NET environment, with the bottom portion representing the CLR and the top portion representing the CLR executables or Portable Executable (PE) files, which are .NET assemblies or units of deployment. The CLR is the runtime engine that loads required classes, performs just-in-time compilation on needed methods, enforces security checks, and accomplishes a bunch of other runtime functionalities. The CLR executables shown in Figure 2-1 are either EXE or DLL files that consist mostly of metadata and code. Figure 2-1. The CLR environment 2.2 CLR Executables Microsoft .NET executables are different from typical Windows executables in that they carry not only code and data, but also metadata (see Section 2.3 and Section 2.5 later in this chapter). In this section, we start off with the code for several .NET applications, and discuss the .NET PE format. 2.2.1 Hello, World: Managed C++ Let's start off by examining a simple Hello, World application written in Managed C++, a Microsoft .NET extension to the C++ language. Managed C++ includes a number of new .NET-specific keywords that permit C++ programs to take advantage of .NET's new features, including garbage collection. Here's the Managed C++ version of our program: 14 www.it-ebooks.info .NET Framework Essentials, 2nd Edition #using using namespace System; void main( ) { Console::WriteLine(L"C++ Hello, World!"); } As you can see, this is a simple C++ program with an additional directive, #using (shown in bold). If you have worked with the Microsoft Visual C++ compiler support features for COM, you may be familiar with the #import directive. While #import reverse-engineers type information to generate wrapper classes for COM interfaces, #using makes all types accessible from the specified DLL, similar to a #include directive in C or C++. However, unlike #include, which imports C or C++ types, #using imports types for any .NET assembly, written in any .NET language. The one and only statement within the main( ) method is self-explanatory—it means that we are invoking a static or class-level method, WriteLine( ), on the Console class. The L that prefixes the literal string tells the C++ compiler to convert the literal into a Unicode string. You may have already guessed that the Console class is a type hosted by mscorlib.dll, and it takes one string parameter. One thing that you should also notice is that this code signals to the compiler that we're using the types in the System namespace, as indicated by the using namespace statement. This allows us to refer to Console instead of having to fully qualify this class as System::Console. Given this simple program, compile it using the new C++ command-line compiler shipped with the .NET SDK: cl hello.cpp /CLR /link /entry:main The /CLR command-line option is extremely important, because it tells the C++ compiler to generate a .NET PE file instead of a normal Windows PE file. When this statement is executed, the C++ compiler generates an executable called hello.exe. When you run hello.exe, the CLR loads, verifies, and executes it. 2.2.2 Hello, World: C# Because .NET is serious about language integration, we'll illustrate this same program using Microsoft's new C# language specially designed for .NET. Borrowing from Java and C++ syntax, C# is a simple and object-oriented language that Microsoft has used to write the bulk of the .NET base classes and tools. If you are a Java (or C++) programmer, you should have no problem understanding C# code. Here's Hello, World in C#: 15 www.it-ebooks.info .NET Framework Essentials, 2nd Edition using System; class MainApp { public static void Main( ) { Console.WriteLine("C# Hello, World!"); } } C# is similar to Java in that it doesn't have the concept of a header file: class definitions and implementations are stored in the same .cs file. Another similarity to Java is that Main( ) is a public, static function of a particular class, as you can see from the code. This is different from C++, where the main( ) method itself is a global function. The using keyword here functions similar to using namespace in the previous example, in that it signals to the C# compiler that we want to use types within the System namespace. Here's how to compile this C# program: csc hello.cs In this command, csc is the C# compiler that comes with the .NET SDK. Again, the result of executing this command is an executable called hello.exe, which you can execute like a normal EXE but is managed by the CLR. 2.2.3 Hello, World: VB.NET And since we're on a roll, here is the same program in Visual Basic.NET (VB.NET): Imports System Public Module modmain Sub Main( ) Console.WriteLine ("VB Hello, World!") End Sub End Module If you are a VB programmer, you may be in for a surprise. The syntax of the language has changed quite a bit, but luckily these changes make the language mirror other object-oriented languages, such as C# and C++. Look carefully at this code snippet, and you will see that you can translate each line of code here into an equivalent in C#. Whereas C# uses the keywords using and class, VB.NET uses the keywords Import and Module, respectively. Here's how to compile this program: vbc /t:exe /out:Hello.exe Hello.vb Microsoft now provides a command-line compiler, vbc, for VB.NET. The /t option specifies the type of PE file to be created. In this case, since we have specified an EXE, hello.exe will be the output of this command. 16 www.it-ebooks.info

Author Hoang Lam and Thuan L. Thai Isbn 9780596003029 File size 3.6 MB Year 2002 Pages 320 Language English File format PDF Category IT ebooks Book Description: FacebookTwitterGoogle+TumblrDiggMySpaceShare .NET Framework Essentials, 2nd Edition is an objective, concise, and technical overview of the new Microsoft .NET Framework for developing web applications and services. Specifically written for intermediate to advanced VB, C/C++, Java, and Delphi developers, .NET Framework Essentials, 2nd Edition is also useful to system architects and leaders who are assessing tools for future projects. The authors devote special attention to the writing of .NET components plus web applications and services.     Download (3.6 MB) Code-First Development with Entity Framework C# 2008 Programmer’s Reference C# Essentials, 2nd Edition Beginning ASP.NET 4 in C# and VB Applied XML Programming for Microsoft .NET Load more posts

Leave a Reply

Your email address will not be published. Required fields are marked *