.NET is the new platform from Microsoft used to build component-based applications, from standalone desktop applications to web-based applications and services. The platform will be available on forthcoming Microsoft operating systems and supported by the next release of Visual Studio, called Visual Studio.NET. In addition to providing a modern object-oriented framework for building distributed applications, .NET also provides several specialized application frameworks. These frameworks include Windows Forms for rich Windows clients, ADO.NET for data access, and ASP.NET for dynamic web applications. Another important framework is Web Services, which is used to expose and consume remote objects using the emerging SOAP and other XML-based protocols.
.NET is Microsoft’s next-generation component technology. It is designed from the ground up to simplify component development and deployment, as well as to support interoperability between programming languages.
Despite its innovations and modern design, .NET is essentially a
component technology. Like COM, .NET provides
you with the means to rapidly build binary components, and Microsoft
intends for .NET to eventually succeed COM. Like COM, .NET does not
provide its own component services. Instead, .NET relies on COM+ to
provide it with instance management, transactions, activity-based
synchronization, granular role-based security, disconnected
asynchronous queued components, and loosely coupled events. The .NET
namespace that contains the types necessary to use COM+ services was
named System.EnterpriseServices
to reflect the
pivotal role it plays in building .NET enterprise applications.
A .NET component that uses COM+ services is called a serviced component to distinguish it from the standard managed components in .NET. If you are not familiar with .NET, you should first read Appendix C or pick up a copy of .NET Framework Essentials by Thuan Thai and Hoang Lam (O’Reilly, 2001).
If you are already familiar with the basic .NET concepts, such as the runtime, assemblies, garbage collection, and C# (pronounced “C sharp”), continue reading. This chapter shows you how to create .NET serviced components that can take advantage of the COM+ component services that you have learned to apply throughout this book.
A .NET component
that
takes advantage of COM+ services needs to derive from the .NET base
class
ServicedComponent
.
ServicedComponent
is defined in the
System.EnterpriseServices
namespace. Example 10-1 demonstrates how to write a .NET serviced
component that implements the IMessage
interface
and displays a message box with “Hello” in it when the
interface’s ShowMessage( )
method is called.
Example 10-1. A simple .NET serviced component
namespace MyNamespace {using System.EnterpriseServices;
using System.Windows.Forms;//for the MessageBox class public interface IMessage { void ShowMessage( ); } /// <summary> /// Plain vanilla .NET serviced component /// </summary> public class MyComponent:ServicedComponent
,IMessage { public MyComponent( ) {}//constructor public void ShowMessage( ) { MessageBox.Show("Hello!","MyComponent"); } } }
A serviced component is not allowed to have
parameterized constructors.
If you require such parameters, you can either design around them by
introducing a Create( )
method that accepts
parameters, or use a constructor string.
There are two ways to configure a serviced component to use COM+
services. The first is COM-like: you derive from
ServicedComponent
, add the component to a COM+
application, and configure it there. The second way is to
apply
special attributes to the component,
configuring it at the source-code level. When the component is added
to a COM+ application, it is configured according to the values of
those attributes. Attributes are discussed in greater detail
throughout this chapter as you learn about configuring .NET
components to take advantage of the various COM+ services.
.NET allows you to apply attributes to your serviced components with great flexibility. If you do not apply your own attributes, a serviced component is configured using default COM+ settings when it is added to a COM+ application. You can apply as many attributes as you like. A few COM+ services can only be configured via the Component Services Explorer. These services are mostly deployment-specific configurations, such as persistent subscriptions to COM+ Events and allocation of users to roles. In general, almost everything you can do with the Component Services Explorer can be done with attributes. I recommend that you put as many design-level attributes as possible (such as transaction support or synchronization) in the code and use the Component Services Explorer to configure deployment-specific details.