|
|
In this chapter, youve seen how .NET
COM Callable Wrappers can be produced for .NET types, and these wrappers provide all the behavior COM client code will be expecting. Methods on .NET types are exposed via COM interfaces. .NET interfaces will be converted and exposed as COM interfaces, and if a class doesnt expose its
|
|
|
|
In Chapter 4, you learned techniques for making your .NET
Remember that COM+ relies on COM and DCOM (Microsoft Distributed Component Object Model) to provide part of its functionality. COM is the source of component technology, while DCOM provides the remote procedure call (RPC) functionality. The first two sections of this chapter explore the relationship between the two. The chapter will also discuss how Microsoft Message Queuing (MSMQ) fits within the COM+ Queued Components . Using message queuing can greatly enhance the flexibility of your application and allow it to process data asynchronously.
The final portion of the chapter discusses some COM+-specific issues you need to consider when working with .NET components. For example, the communication with COM+ is two way, so you need to know how to accept as well as send information to COM+. In general, youll find that the hardest thing to understand is the communication
|
|
|
|
From a client-side component-creation perspective, COM and COM+ are about the same. Despite all the hype to the contrary, you can create a component using either form of the technology. In fact, COM+ is merely an
Youll find that the .NET environment reacts somewhat differently from a COM+ application because this application type
In addition to .NET differences, COM and COM+ have some differences that you need to consider when developing your application. For example, the design goals for COM+ are different than those for COM, so naturally, the two technologies work somewhat differently. Its important to know about these differences as you attempt to move a component from COM to COM+
Unlike COM, COM+ interoperability begins at the server. You create the COM+ application using the component as we did in the Creating COM+ Applications and Installing Components section of Chapter 2. Once you export the proxy, you install it on the client machine. For a developer, this means installing the proxy on your workstation. Once you have the proxy installed, youll be able to view the COM+ application using the Component Services console.
At this point, you use the component just as you would any other component within the .NET IDE. In general, the material in the Generating Runtime Callable Wrappers section of Chapter 3 applies to obtaining a reference to the component. However, you need to consider several special issues when working with COM+ applications. The most important issue is that youre working with a proxy generated on another machine.
Chapter 3 discussed three
Figure 5-1:
Youll need the Application ID to locate the components for the COM+ application on your local drive.
The Application ID field is the one that youre interested in because COM+ uses this GUID to keep the components from each application separate. Append this information to the Program Files\ComPlus Applications\ path, and youll have the path to your components. In this case, the complete
Well also see in Chapter 9 that interoperability requires some interesting component planning. The problem is one of getting COM+ to recognize the component. The base class for your component makes a difference. Consequently, well discuss components created with the following base classes:
Object
Component
ServicedComponent
Of the three, the ServicedComponent class provides the greatest flexibility. A component based on the Object or Component class will have limited flexibility and present problems for Windows 2000 developers. However, a component based on the ServicedComponent class can present problems of its own. For example, youll find it difficult to create an application proxy if you install the component using the RegSvcs.exe utility.
As we saw in Chapter 2, creating a simple component results in a COM+ component addition that includes only the methods found in the Component class and those found in the Object class. This includes the Equals() , GetHashCode() , GetType() , and ToString() methods. All this type of component requires is that you add the [ClassInterface(ClassInterfaceType.AutoDual)] attribute.
| Tip |
Whenever you create a component for COM+, you should include the
[Guid]
attribute. Whenever COM+ registers your component as part of the installation process, it makes a registry entry. The common language runtime creates a GUID at random unless you define the
[Guid]
attribute. Cleaning up the registry after a development session can become quite time consuming. Using the same GUID all the time means youll
|
When you design a component based on the Component class, you need to add a few more bits of code. Listing 5-1 shows a component that youll find in the Chapter05\MyMath (Component) folder in the companion content. You can get the companion content at the books Web site: http:// www.microsoft.com/mspress/books/6426.asp .
Listing 5-1: MyMath.cs (Component)
|
|
usingSystem;
usingSystem.Runtime.InteropServices;
usingSystem.ComponentModel;
namespaceMyMath
{
///<summary>
///Asimpleclassthatshowsthefourbasicmathfunctions.
///</summary>
[Guid("0C4340A2-C362-4287-9A03-8CDD3D1F80F6"),
ClassInterface(ClassInterfaceType.AutoDual)]
publicclassMathFunctions:Component
{
///<summary>
///AddtwoInt32valuestogether.
///</summary>
///<paramname="Value1">Thefirstnumber.</param>
///<paramname="Value2">Thesecondnumber.</param>
///<returns>Thetwonumbersadded.</returns>
publicInt32DoAdd(Int32Value1,Int32Value2)
{
returnValue1+Value2;
}
///<summary>
///SubtractoneInt32valuefromanother.
///</summary>
///<paramname="Value1">Initialvalue</param>
///<paramname="Value2">Valuetosubtract</param>
///<returns>Value2subtractedfromValue1</returns>
publicInt32DoSubtract(Int32Value1,Int32Value2)
{
returnValue1-Value2;
}
///<summary>
///MultiplytwoInt32values.
///</summary>
///<paramname="Value1">Thefirstnumber.</param>
///<paramname="Value">Thesecondnumber.</param>
///<returns>Thetwovaluesmultiplied.</returns>
publicInt32DoMultiply(Int32Value1,Int32Value2)
{
returnValue1*Value2;
}
///<summary>
///DivideoneInt32valuebyanother.
///</summary>
///<paramname="Value1">Initialvalue.</param>
///<paramname="Value2">Divisor</param>
///<returns>Value1dividedbyValue2</returns>
publicInt32DoDivide(Int32Value1,Int32Value2)
{
returnValue1/Value2;
}
}
}
|
|
As you can see, this component requires that you subclass the Component class found in the System.ComponentModel namespace. Fortunately, this namespace appears in the System.dll file, so you dont need to reference any additional DLLs in your component. Figure 5-2 shows that creating a component using this technique adds the IComponent and IDisposable interfaces, which include the add_Disposed() , remove_Disposed() , Site() , and Disposed() methods. Youll use the add_Disposed() and remove_Disposed() methods to add and remove event handlers for the Disposed event. Notice that there are also two copies of the Site() methodone to get the Site property value and one to set it.
Figure 5-2:
Creating a component based on the
Component
class adds the
IComponent
and
IDisposable
interfaces.
Creating a component that subclasses the ServicedComponent class gets a little more complex. You need to add a reference to the System.EnterpriseServices.dll. In addition, you need to add the appropriate reference to your code and add some code for the ServicedComponent class. Listing 5-2 shows a typical example of the code you need. (Youll find the complete component in the Chapter05\MyMath (ServicedComponent) folder of the companion content.)
Listing 5-2: MyMath.cs (ServicedComponent)
|
|
usingSystem;
usingSystem.Runtime.InteropServices;
usingSystem.EnterpriseServices;
namespaceMyMath
{
///<summary>
///Asimpleclassthatshowsthefourbasicmathfunctions.
///</summary>
[Guid("0C4340A2-C362-4287-9A03-8CDD3D1F80F6"),
ClassInterface(ClassInterfaceType.AutoDual)]
publicclassMathFunctions:ServicedComponent
{
///<summary>
///AddtwoInt32valuestogether.
///</summary>
///<paramname="Value1">Thefirstnumber.</param>
///<paramname="Value2">Thesecondnumber.</param>
///<returns>Thetwonumbersadded.</returns>
publicInt32DoAdd(Int32Value1,Int32Value2)
{
returnValue1+Value2;
}
///<summary>
///SubtractoneInt32valuefromanother.
///</summary>
///<paramname="Value1">Initialvalue</param>
///<paramname="Value2">Valuetosubtract</param>
///<returns>Value2subtractedfromValue1</returns>
publicInt32DoSubtract(Int32Value1,Int32Value2)
{
returnValue1-Value2;
}
///<summary>
///MultiplytwoInt32values.
///</summary>
///<paramname="Value1">Thefirstnumber.</param>
///<paramname="Value">Thesecondnumber.</param>
///<returns>Thetwovaluesmultiplied.</returns>
publicInt32DoMultiply(Int32Value1,Int32Value2)
{
returnValue1*Value2;
}
///<summary>
///DivideoneInt32valuebyanother.
///</summary>
///<paramname="Value1">Initialvalue.</param>
///<paramname="Value2">Divisor</param>
///<returns>Value1dividedbyValue2</returns>
publicInt32DoDivide(Int32Value1,Int32Value2)
{
returnValue1/Value2;
}
}
}
|
|
The
System.EnterpriseServices
namespace is
Figure 5-3:
Creating a component based on the
ServicedComponent
class adds a number of additional interfaces.
In Chapter 2, we discussed the reasons you might want to use COM+ for development purposes, even with the features that .NET provides. However, we really havent discussed the COM+ part of the picture. To understand what you gain by using COM+, you need to understand some of the goals that the design team had in mind when
Make developing server components as easy as developing client components.
Before COM+, there were a lot of server- specific issues to take care of, such as multiple users
Make it just as easy to develop enterprise applications as it is to develop workgroup applications.
COM+ allows you to create components that will scale to any
These two goals define the essential reasons to use COM+. Unlike COM, Microsoft designed COM+ to allow you to create distributed applications that rely on component technology that
Lets talk about the client and server situation a little further. COM+ is also part of the Microsoft distributed architecture strategy.
One of the more important reasons to use COM+ is ensuring the integrity of the data you transfer from one machine to another. Anytime two machines talk to each other using any media (cable, wireless, or anything else you can think of), you have to create some type of data exchange protocol. Even if the machines are sitting side-by-side in the same room, theres a chance of losing data because of a bad connection or interference. Given todays distributed environment, theres a much greater chance for lost data, which means that you need some type of transaction technology in place.
Microsoft Transaction Server (MTS) is part of the COM+ universe. MTS is the mechanism by which COM+
Transactions
A transaction is a single
Resource pooling
Most servers have huge
Security Getting data from one point to another and ensuring the database records it properly wont do much for you if someone breaks into the connection between the client and server and corrupts the data. As a result, security is one of the more important features that MTS provides.
Administration Managing your MTS setup so that you can get optimum performance from your server is an important task. Microsoft provides a Microsoft Management Console (MMC) snap-in that makes MTS management easier.
As you can see, MTS provides a lot more than just transaction management, although thats its main function. The other services MTS provides merely augment the main function of making sure data gets from one point to another without interruption and that all parties performing operations on that data complete their tasks successfully, or not at all. Without these services, COM+ as a whole would fail because data security is a primary requirement to creating distributed applications.
You wont work directly with MTS in most cases. The
Distributed Transaction Coordinator (DTC)
enables MTS to start transactions on
Figure 5-4:
Use the Distributed Transaction Coordinator folder elements to manage transactions on a server.
One service that doesnt appear in our list is serialization. This particular feature of MTS ensures that both data and commands arrive at the database in the same order the client created them, even if the network protocol used to transfer the packets of information doesnt guarantee any order of delivery. This means you wont need to worry quite as much about the features provided by the underlying network protocolat least not when it concerns the order your data will
COM+ also includes the concept of disconnected applications. A disconnected application is a form of asynchronous processing in which the client and server dont need to exist at the same time. Messaging, a type of e-mail communication, provides the glue required to keep client and server communicating. (The use of message queues isnt precisely the same as e-mail, but the analogy is a good one for understanding how the process works.)
At the heart of this technology is
Queued Components
, which in
| Note |
Youll see MSMQ and Queued Components used throughout the book in an almost interchangeable way. Ill use the term Queued Components whenever Im talking about a Queued Component specific feature such as a wizard, while MSMQ will always refer to the underlying technology or an existing feature like a programming API. Its important to see Queued Components as a slightly augmented version of MSMQ that was designed
|
Better road-
Lower development costs
Older technologies forced the programmer to do more work. Even if the programmer needed to worry only about two data-entry scenarios, that still required twice the amount of code and debugging. In essence, the programmer was writing the same application twice because of a lack of disconnected application tools. Of course, the doubling of
No live connection needed Applications that rely on Queued Components dont care about the current connected state of the application. Data that an application wants to send to the server gets stored in a local message queue if the application cant establish a connection with the server. When the user makes a connection, Queued Components automatically uploads the data in the message queue for the user. In this respect, a Queued Component application acts as an answering machine. A recorder on the client creates the message, a listener on the server alerts the server-side component to the presence of the message, and a player retrieves the message from the queue.
Preemptive resource management
The application can also download resources such as database updates or new application functionality from the server before the user attempts to break the connection with the server. This resource management technique helps the user request at least a subset of the data from the server, even though the user might not realize there isnt a connection in place for getting the requested information. The only time the user will notice any difference is if the application needs the data requested immediately and the preemptive resource management module didnt anticipate the need to download it from the server. For example, the application could automatically request the client records needed for the clients appointments the following day based on appointment-book entries. Even if the task didnt get done, as soon as the user establishes a connection, the resource management module can download the
Unlike many other COM+-
Figure 5-5:
Manage the queues for an application using the Message Queuing folder features.
As you can see, MSMQ provides four levels of message queues: Outgoing Queues, Public Queues, Private Queues, and System Queues. Well use all these queues to create various applications in the book. The System Queues folder is the only folder that actually contains queues when you first install Message Queuing (the name of the option found in the Windows Components Wizard For Windows XP). This queue
Visual Studio .NET makes it almost too easy to use a queue. You can access both local and remote queues using Server Explorer, as shown in Figure 5-6. Notice that you can access all but the Outgoing Queues folder, which makes sense because this folder contains only temporary queues (and only while the client lacks a connection to the server).
Figure 5-6:
Use Server Explorer to access local and remote queues.
To add a queue to your application, drag and drop it onto the form. Visual Studio .NET will create a MessageQueue object for you. The properties will contain all the correct pointers for the queue. Well discuss message queues at length in Chapter 10.
So far, weve seen that COM+ is an amalgam of various existing technologies. In short, nothing Ive said so far is very new. However, the way that COM+ provides a wrapper for making all these technologies work together is a relatively new idea.
Many COM+ concepts well discuss are similar to those found in .NET. COM+ is the predecessor to the .NET technologies. Although I havent mentioned it yet, you should use the remoting features of .NET before you rely on COM+ for managed application requirements. Web services also provide a good alternative to COM+ for Internet applications. In fact, as shown in Figure 5-7, the IDE generates an error if you attempt to directly use a managed component thats part of a COM+ application. Theres no reason to pay a double performance penalty for using the componentuse it directly as a managed component whenever possible. Of course, disconnected applications are one scenario in which you might want to use managed components in a COM+ application from your managed clientits a
Figure 5-7:
Attempting to directly import a managed component thats part of a COM+ application into your application will generate an error message.
COM+ isnt merely a wrapper for existing services, however. It also provides unique services that you wont find in any older Microsoft offerings. The following list provides you with an overview of what these services are. Well study these features in detail as the book progresses.
Events
Applications can receive events generated on the server as if they occurred on the local machine. This means the server-side components you create will have a direct connection to the client. COM+ allows you to use unicast (in which an event is sent to one event sink), multicast (in which an event is sent to multiple event sinks), and unbound (in which an event occurs when client makes contact) events. The
Security
COM+ relies on MTS for the security of its data in many cases. However, COM+ still has security concerns when
Component Load Balancing (CLB)
Large companies normally use more than one server for their data storage needs. Of course, this means the company normally needs to provide access to specific servers to a given user in order to manually balance the server load. Load balancing lets everyone access all the available servers through a router. The router keeps track of how much of a load each server has and
Queued Components Clients require updates to the components they hold from time to time and, in some cases, will require a new component before they can perform a specific task. In the past, the administrator had to install new components individually on each machine or create cumbersome batch files to do the job. The use of Queued Components helps each client to receive automatic component updates. The administrator doesnt need to do much more than install the component on the server. Because all users have access to the same component, they all receive the update at the same time.
Compensating resource manager Legacy applications require this particular service. It builds a framework around the old server application so that clients can access the application using all the new features COM+ provides. The main goal of this service is to help you maintain your investment in established applications, yet allow the old application to interact with other resources on the server. Of course, this feature comes at the cost of performance, so upgrading your components to .NET is one option you should consider.
Administration COM+ uses an MMC snap-in to provide administrative services for your server-side components. This snap-in lets you manage all COM components and helps you administer all new services COM+ provides. This snap-in also provides a programming interface that allows you to install, configure, and automatically deploy your COM+ components. It relies on the Active Directory catalog to store the attributes for each of the components installed on the server. The COM+ component attributes include transactions, security roles, and activation properties.
Publish/subscribe event model COM+ provides a new event model that helps the developer create applications that dont need to know quite as much about each other. The component publishes events and doesnt care where those events go. An application can subscribe to events without really knowing their source. In between these two applications are component services, which manage the connection between publisher and subscriber.
|
|