- •About the Authors
- •Contents at a Glance
- •Contents
- •Introduction
- •Goal of the Book
- •How to Use this Book
- •Introduction to the .NET Framework
- •Common Language Runtime (CLR)
- •Class Library
- •Assembly
- •Versioning
- •Exceptions
- •Threads
- •Delegates
- •Summary
- •Introduction to C#
- •Variables
- •Initializing Variables
- •Variable Modifiers
- •Variable Data Types
- •Types of Variables
- •Variable Scope
- •Types of Data Type Casting
- •Arrays
- •Strings
- •Initializing Strings
- •Working with Strings
- •Statements and Expressions
- •Types of Statements
- •Expressions
- •Summary
- •Classes
- •Declaring Classes
- •Inheritance
- •Constructors
- •Destructors
- •Methods
- •Declaring a Method
- •Calling a Method
- •Passing Parameters to Methods
- •Method Modifiers
- •Overloading a Method
- •Namespaces
- •Declaring Namespaces
- •Aliases
- •Structs
- •Enumerations
- •Interfaces
- •Writing, Compiling, and Executing
- •Writing a C# Program
- •Compiling a C# Program
- •Executing a C# Program
- •Summary
- •Arrays
- •Single-Dimensional Arrays
- •Multidimensional Arrays
- •Methods in Arrays
- •Collections
- •Creating Collections
- •Working with Collections
- •Indexers
- •Boxing and Unboxing
- •Preprocessor Directives
- •Summary
- •Attributes
- •Declaring Attributes
- •Attribute Class
- •Attribute Parameters
- •Default Attributes
- •Properties
- •Declaring Properties
- •Accessors
- •Types of Properties
- •Summary
- •Introduction to Threads
- •Creating Threads
- •Aborting Threads
- •Joining Threads
- •Suspending Threads
- •Making Threads Sleep
- •Thread States
- •Thread Priorities
- •Synchronization
- •Summary
- •Case Study
- •Project Life Cycle
- •Analyzing Requirements
- •High-Level Design
- •Primary and Foreign Keys
- •Referential Integrity
- •Normalization
- •Designing a Database
- •Low-Level Design
- •Construction
- •Integration and Testing
- •User Acceptance Testing
- •Implementation
- •Operations and Maintenance
- •Summary
- •Creating a New Project
- •Console Application
- •Windows Applications
- •Creating a Windows Application for the Customer Maintenance Project
- •Creating an Interface for Form1
- •Creating an Interface for WorkerForm
- •Creating an Interface for CustomerForm
- •Creating an Interface for ReportsForm
- •Creating an Interface for JobDetailsForm
- •Summary
- •Performing Validations
- •Identifying the Validation Mechanism
- •Using the ErrorProvider Control
- •Handling Exceptions
- •Using the try and catch Statements
- •Using the Debug and Trace Classes
- •Using the Debugging Features of Visual Studio .NET
- •Using the Task List
- •Summary
- •Creating Form1
- •Connecting WorkerForm to the Workers Table
- •Connecting CustomerForm to the tblCustomer Table
- •Connecting the JobDetails Form
- •to the tblJobDetails Table
- •Summary
- •Introduction to the Crystal Reports Designer Tool
- •Creating the Reports Form
- •Creating Crystal Reports
- •Creating the Windows Forms Viewer Control
- •Creating the Monthly Worker Report
- •Summary
- •Introduction to Deploying a Windows Application
- •Deployment Projects Available in Visual Studio .NET
- •Deployment Project Editors
- •Summary
- •Case Study
- •Project Life Cycle
- •Analyzing Requirements
- •High-Level Design
- •Low-Level Design
- •Summary
- •Populating the TreeView Control
- •Displaying Employee Codes in the TreeView Control
- •Event Handling
- •Displaying Employee Details in the ListView Control
- •Summary
- •Case Study
- •Project Life Cycle
- •Analyzing Requirements
- •High-Level Design
- •Low-Level Design
- •Summary
- •Adding the Programming Logic to the Application
- •Adding Code to the Form Load() Method
- •Adding Code to the OK Button
- •Adding Code to the Exit Button
- •Summary
- •The Created Event
- •Adding Code to the Created Event
- •Overview of XML
- •The XmlReader Class
- •The XmlWriter Class
- •Displaying Data in an XML Document
- •Displaying an Error Message in the Event Log
- •Displaying Event Entries from Event Viewer
- •Displaying Data from the Summary.xml Document in a Message Box
- •Summary
- •Airline Profile
- •Role of a Business Manager
- •Role of a Network Administrator
- •Role of a Line-of-Business Executive
- •Project Requirements
- •Creation and Deletion of User Accounts
- •Addition of Flight Details
- •Reservations
- •Cancellations
- •Query of Status
- •Confirmation of Tickets
- •Creation of Reports
- •Launch of Frequent Flier Programs
- •Summarizing the Tasks
- •Project Design
- •Database Design
- •Web Forms Design
- •Enabling Security with the Directory Structure
- •Summary
- •Getting Started with ASP.NET
- •Prerequisites for ASP.NET Applications
- •New Features in ASP.NET
- •Types of ASP.NET Applications
- •Exploring ASP.NET Web Applications
- •Introducing Web Forms
- •Web Form Server Controls
- •Configuring ASP.NET Applications
- •Configuring Security for ASP.NET Applications
- •Deploying ASP.NET Applications
- •Creating a Sample ASP.NET Application
- •Creating a New Project
- •Adding Controls to the Project
- •Coding the Application
- •Summary
- •Creating the Database Schema
- •Creating Database Tables
- •Managing Primary Keys and Relationships
- •Viewing the Database Schema
- •Designing Application Forms
- •Standardizing the Interface of the Application
- •Common Forms in the Application
- •Forms for Network Administrators
- •Forms for Business Managers
- •Forms for Line-of-Business Executives
- •Summary
- •The Default.aspx Form
- •The Logoff.aspx Form
- •The ManageUsers.aspx Form
- •The ManageDatabases.aspx Form
- •The ChangePassword.aspx Form
- •Restricting Access to Web Forms
- •The AddFl.aspx Form
- •The RequestID.aspx Form
- •The Reports.aspx Form
- •The FreqFl.aspx Form
- •Coding the Forms for LOB Executives
- •The CreateRes.aspx Form
- •The CancelRes.aspx Form
- •The QueryStat.aspx Form
- •The ConfirmRes.aspx Form
- •Summary
- •Designing the Form
- •The View New Flights Option
- •The View Ticket Status Option
- •The View Flight Status Option
- •The Confirm Reservation Option
- •Testing the Application
- •Summary
- •Locating Errors in Programs
- •Watch Window
- •Locals Window
- •Call Stack Window
- •Autos Window
- •Command Window
- •Testing the Application
- •Summary
- •Managing the Databases
- •Backing Up the SkyShark Airlines Databases
- •Exporting Data from Databases
- •Examining Database Logs
- •Scheduling Database Maintenance Tasks
- •Managing Internet Information Server
- •Configuring IIS Error Pages
- •Managing Web Server Log Files
- •Summary
- •Authentication Mechanisms
- •Securing a Web Site with IIS and ASP.NET
- •Configuring IIS Authentication
- •Configuring Authentication in ASP.NET
- •Securing SQL Server
- •Summary
- •Deployment Scenarios
- •Deployment Editors
- •Creating a Deployment Project
- •Adding the Output of SkySharkDeploy to the Deployment Project
- •Deploying the Project to a Web Server on Another Computer
- •Summary
- •Organization Profile
- •Project Requirements
- •Querying for Information about All Books
- •Querying for Information about Books Based on Criteria
- •Ordering a Book on the Web Site
- •Project Design
- •Database Design
- •Database Schema
- •Web Forms Design
- •Flowcharts for the Web Forms Modules
- •Summary
- •Introduction to ASP.NET Web Services
- •Web Service Architecture
- •Working of a Web Service
- •Technologies Used in Web Services
- •XML in a Web Service
- •WSDL in a Web Service
- •SOAP in a Web Service
- •UDDI in a Web Service
- •Web Services in the .NET Framework
- •The Default Code Generated for a Web Service
- •Testing the SampleWebService Web Service
- •Summary
- •Creating the SearchAll() Web Method
- •Creating the SrchISBN() Web Method
- •Creating the AcceptDetails() Web Method
- •Creating the GenerateOrder() Web Method
- •Testing the Web Service
- •Securing a Web Service
- •Summary
- •Creating the Web Forms for the Bookers Paradise Web Site
- •Adding Code to the Web Forms
- •Summary
- •Case Study
- •Project Life Cycle
- •Analyzing Requirements
- •High-Level Design
- •Low-Level Design
- •Summary
- •Overview of Mobile Applications
- •The Microsoft Mobile Internet Toolkit
- •Overview of WAP
- •The WAP Architecture
- •Overview of WML
- •The Mobile Web Form
- •The Design of the MobileTimeRetriever Application
- •Creating the Interface for the Mobile Web Forms
- •Adding Code to the MobileTimeRetriever Application
- •Summary
- •Creating the Forms Required for the MobileCallStatus Application
- •Creating the frmLogon Form
- •Creating the frmSelectOption Form
- •Creating the frmPending Form
- •Creating the frmUnattended Form
- •Adding Code to the Submit Button in the frmLogon Form
- •Adding Code to the Query Button in the frmSelectOption Form
- •Adding Code to the Mark checked as complete Button in the frmPending Form
- •Adding Code to the Back Button in the frmPending Form
- •Adding Code to the Accept checked call(s) Button in the frmUnattended Form
- •Adding Code to the Back Button in the frmUnattended Form
- •Summary
- •What Is COM?
- •Windows DNA
- •Microsoft Transaction Server (MTS)
- •.NET Interoperability
- •COM Interoperability
- •Messaging
- •Benefits of Message Queues
- •Limitations
- •Key Messaging Terms
- •Summary
- •Pointers
- •Declaring Pointers
- •Types of Code
- •Implementing Pointers
- •Using Pointers with Managed Code
- •Working with Pointers
- •Compiling Unsafe Code
- •Summary
- •Introduction to the Languages of Visual Studio .NET
- •Visual C# .NET
- •Visual Basic .NET
- •Visual C++ .NET
- •Overview of Visual Basic .NET
- •Abstraction
- •Encapsulation
- •Inheritance
- •Polymorphism
- •Components of Visual Basic .NET
- •Variables
- •Constants
- •Operators
- •Arrays
- •Collections
- •Procedures
- •Arguments
- •Functions
- •Adding Code to the Submit Button
- •Adding Code to the Exit Button
- •Summary
- •Introduction to Visual Studio .NET IDE
- •Menu Bar
- •Toolbars
- •Visual Studio .NET IDE Windows
- •Toolbox
- •The Task List Window
- •Managing Windows
- •Customizing Visual Studio .NET IDE
- •The Options Dialog Box
- •The Customize Dialog Box
- •Summary
- •Index
796 |
Part IX |
BEYOND THE LABS |
|
|
|
In this chapter, you will learn about COM+, System.Messaging namespace, and asynchronous message queuing.
COM+
Before I introduce you to COM+, you need to understand the concepts of COM, MTS, and Windows DNA.
What Is COM?
As you might be aware, COM stands for component object model, and it is a model for construction of binary-compatible software components introduced by Microsoft. In simple terms, COM is a specification and implementation framework that allows you to create modular, object-oriented, customizable, and upgradeable distributed applications using a number of programming languages. COM enables you to develop components that can communicate with other components irrespective of the programming language or tool you choose to develop it.Therefore, COM allows you to concentrate on developing your application and not bother about the internals of the components.
COM allows clients to invoke services provided by COM-compliant components (COM objects). Services implemented by COM objects are exposed through a set of interfaces that represent the point of contact between clients and the object.
Why COM?
In order to understand the features provided by COM, you first need to understand the rationale for its existence. Binary code has been in use for a long time now. Libraries have been used in C and C++ from the very beginning. Windows programmers have been reusing DLLs (dynamically linked libraries). But there were other issues.
Libraries created using C/C++ compilers were often incompatible with executables created using a different C/C++ compiler. Secondly, DLLs were language
ADVANCED C# CONCEPTS |
Chapter 34 |
797 |
|
|
|
|
|
dependent. Moreover, updating DLLs posed greater problems. Compatibility between versions was not achieved because of memory allocation reasons. Changed DLLs often broke executables designed for an earlier version.
Benefits of COM
COM was an alternative developed by Microsoft to overcome these problems. The major benefits of COM are:
COM components can provide functionality in a standard way.
COM provides for component interoperability.
COM provides a good versioning mechanism that allows one system component to be updated without requiring updates to other components in the system.
COM components can be implemented in a number of programming languages.
Before we proceed further, I will take you through some commonly used terms in COM.
Interfaces
An interface is a specification that defines the type of behavior a class must implement. An interface provides a group of related methods that are pure virtual functions. These methods are not implemented in the interface but are implemented in the class that implements the interface.The class that implements the interface methods is referred to as coclass. When the coclass is instantiated,the instance that is created is referred to as component object.
A class that inherits an interface must implement every member of the interface. An interface enables other programs to access the functionality provided by your component.
Consider the following example, where a COM object behaves like a clock. The clock object supports two interfaces, IClock and IAlarm. The IClock interface provides the methods to set and read current time. The IAlarm interface provides the alarm and methods.
798 |
Part IX |
BEYOND THE LABS |
|
|
|
GUID
COM objects and interfaces are specified using Microsoft IDL (interface defini - tion language). In order to avoid problems arising out of two components with same names, every COM component and interface has a GUID (globally unique identifier). A GUID is a 128-bit integer that is generated and assigned to every COM component and interface built. It uniquely identifies the component to the operating environment and other sof tware.
CLSID |
Y |
|
Class ID (CLSID) is a unique identifier that is associated with an OLE (Object |
||
Linking and Embedding) object. If a classLis used to create more than one instance |
||
|
A |
|
of an object, the associated applicationFneeds to register its CLSID in the system |
||
|
E |
|
registry. This enables clients to locate and load the executable code associated with |
||
the objects. |
T |
M |
Marshaling |
|
|
|
|
Marshaling is the process of packaging and sending interface method calls across thread or process boundaries.
Type Library
Type libraries are binary files (.tlb files) that include information about types and objects exposed by an ActiveX application. A type library can contain any of the following:
Information about data types
Description about one or more objects
Reference to type descriptions from other type libraries
Including the type library with the product ensures that information about objects in the library is made available to users of the application. Type libraries can be shipped as a standalone binary file or a resource in a DLL.
Team-Fly®
ADVANCED C# CONCEPTS |
Chapter 34 |
799 |
|
|
|
|
|
Stub
Stub is an interface-specific object that unpackages the parameters for that interface after they are marshaled across the process boundary and makes the requested method call.The stub runs in the address space of the receiver and communicates with a corresponding proxy in the sender’s address space.
Proxy
A proxy is an interface-specific object that packages parameters for that interface in preparation for a remote method call. A proxy runs in the address space of the sender and communicates with a corresponding stub in the receiver’s address space.
Standard Interfaces
Every component must implement at least two interfaces, the IUnknown and the IDispatch interfaces. The IUnknown interface has three methods, as follows:
AddRef
Release
QueryInterface
COM objects keep track of the number of interface pointers to the object that are in use. When the reference count reaches zero, it can be freed.However, the object is not explicitly freed;instead, all the objects interface pointers and the object frees itself after an appropriate time.
AddRef increments the reference count and Release documents it. The QueryInterface is the most important method. Because all interfaces inherit from IUnknown, all interfaces must implement QueryInterface. The QueryInterface method provides client access to other interfaces on an object.
IDispatch interface exposes objects, methods, and properties to other programs that support automation. COM components implement the IDispatch interface to enable access by automation clients.
800 |
Part IX |
BEYOND THE LABS |
|
|
|
Every COM object runs inside of a server. A single server can support multiple COM objects. A client can access COM objects provided by a server in one the following three ways:
In-process server. Clients can link directly to a library containing the server. The client and ser ver execute in the same process. Communication is accomplished through function calls.
RPC (remote procedure call ). The client can access a server running in a different process but on the same machine through an interprocess communication mechanism.
Remote Object Proxy. The client can access a remote server running on another machine. The network communication between client and server is accomplished through RPC. The mechanism supporting access to remote servers is called DCOM (Distributed COM).
Functioning of COM Objects
If the client and the server are in the same process, the sharing of data between the two is simple. However, when the server process is separate from the client process, as in a local server or remote server, COM must format and bundle the data in order to share it. This process of preparing the data is called marshaling. Marshaling is accomplished through a proxy object and a stub object that handle the cross-process communication for any particular interface. COM creates the stub in the object’s server process and has the stub manage the real interface pointer. COM then creates the proxy in the client’s process and connects it to the stub. The proxy then supplies the interface pointer to the client.
The client calls the interfaces of the server through the proxy, which marshals the parameters and passes them to the server stub. The stub unmarshals the parameters and makes the actual call inside the server object. When the call completes, the stub marshals return values and passes them to the proxy, which in turn returns them to the client. The same proxy/stub mechanism is used when the client and server are on different machines.
The internal implementation of marshaling and unmarshaling differs depending on whether the client and server operate on the same machine (COM) or on dif-
ADVANCED C# CONCEPTS |
Chapter 34 |
801 |
|
|
|
|
|
ferent machines (DCOM). When a client wishes to create and use a COM object, the client performs the following steps:
1.It invokes the COM API to instantiate a new COM object.
2.COM locates the object implementation and initiates a server process for the object.
3.The server process creates the object and returns an interface pointer at the object.
4.The client can then interact with the newly instantiated COM object through the interface pointer.
COM Threading Model
Programming multithreading applications is quite a tedious task irrespective of the tool or the language being used. In an application that is single threaded, everything runs synchronously, that is, one after the other, whereas in case of multithreaded applications, the execution sequence is not so straightforward. All requests for a component method are queued up in the message pump, causing all requests to be executed simultaneously. Applications in which multiple threads are active at the same time can be asynchronous.
It is possible that methods in a component execute simultaneously. Data in a component can be changed or accessed by more than one thread, thus losing its co n- currency. Therefore, it becomes the responsibility of the component or the application to prevent such simultaneous access by multiple threads or to make the code thread savvy.
Because this is a likely scenario for COM components, the COM architecture provides various threading models, which allows you to create components that are inherently thread savvy but less flexible, or components that can handle threading issues themselves.
You can build COM components that have any of the following threading models:
Single threaded
Apartment threaded
Both single and apartment threaded
Free threaded