Microsoft Visual C++ .NET Professional Projects - Premier Press
.pdf
IMPLEMENTATION OF COM AND .NET INTEROPERABILITY Chapter 26 791
return “Error”;
}
}
Testing the Component
When you try to access a COM component from a client, the COM services will search the registry for information on the component. To access a .NET component from COM clients, you need to export the metadata in an assembly and place it in the registry. You will use the RegAsm.exe utility to accomplishes this:
RegAsm.exe LoginComp.dll /tlb:LoginCompAssem.tlb
The RegAsm utility creates a type library and registers the type library. Now that the metadata is in the registry, you can access the component from any COMbased client.
Remember that using the RegAsm utility makes the .NET assembly accessible to COM-based clients. It does not convert the .NET component into a COM component. Hence, when you run the client, both the assembly and the client have to be in the same folder, unless the assembly is in the Global Assembly Cache.
You will add the assembly to the Global Assembly Cache by using the gacutil.exe command-line utility:
Gacutil /i LoginComp.dll
To test the component from a COM-based client, you can use either a Visual Basic 6.0 application or a scripting client.
We will first test the application using a VBScript-based client. Create a VBS file with the following code:
dim objasmb
set objasmb=CreateObject(“LoginComp.Login”)
msgbox objasmb.getPass(“User1”)
objasmb.updatePass “User1”,”oldpass”,”newpass”
Save the file and execute it. Verify the contents of the database.
Next, you will execute the assembly from a Visual Basic 6.0 client:
792Project 8 .NET ASSEMBLIES WITH MANAGED C++ AND .NET
1.Run Visual Basic 6.0 and create a Standard EXE application.
2.From the Project menu, select References and add a reference to the LoginComp type library.
3.Add a button control to the form, and in its Click event, add the following code:
Private Sub Command1_Click()
Dim LObj As New LoginComp.Login
MsgBox LObj.getPass(“User1”)
LObj.updatePass “User1”, “oldpass”, “newpass”
End Sub
Run the application by pressing F5 and check the working of the component.
Summary
In this chapter, you created a .NET assembly using Managed C++. You also saw how the Interop feature allows COM clients to access .NET assemblies. Companies that have already invested in creating components using COM need not discard and re-create all the code again. COM Interop provides a way to integrate existing code, when the transition to .NET happens.
You have acquired the basic knowledge of COM and COM+ services in Chapter 17. You have also learned the basics of assemblies in Chapters 25. You created an assembly using Managed C++ and accessed it from COM clients using the Interop services. In this chapter, you will learn to access the features of COM+ Services from .NET. Before getting into programming the assembly, you will be introduced to the scenario for which the project is to be designed. Then you will
be taken through the steps required to create the project.
Art-Shop Kiosks – Placing an Order
You know from the previous chapter that Art-Shop is planning to promote its products through kiosks. These kiosks are connected to a central server and allow a registered user to purchase artifacts using the system. As discussed, the central servers are being upgraded to .NET. Also, you learned that the interface software that operates the kiosks has been developed using Visual Basic 6.0. Because of the upgrade to .NET, one of the components that needs to be re-created is the business logic component that allows a customer to make a purchase through the kiosk. When a registered customer makes a purchase, an entry is made in the Orders table of the ArtShop database. Also, the value of the ProdStatus field in the Product table has to be changed to Sold for the purchased item.
Project Life Cycle
The development life cycle of a project, as discussed in Chapter 8, usually involves three phases:
Project initiation
Project execution
Project deployment
In the project initiation phase, the project plan was prepared and the development team for the project was identified. The development team further prepared a comprehensive list of tasks involved in the execution and the deployment phases of the project’s life cycle.
USING COM+ SERVICES FROM .NET ASSEMBLIES Chapter 27 795
Moving further, you will look into the tasks performed by the development team in the following stages of the project execution phase, to create the Order Pro-
cessing Component for the site:
Requirements analysis
Design
Construction
Testing
The next few sections detail the tasks performed by the team in each of these stages.
Requirements Analysis
During the requirements analysis phase, the NewTech team came up with the following list of requirements:
When a visitor has made a purchase and the credit card has been validated, the details of the purchase have to be stored in the Orders table of the ArtShop database.
The ProdStatus field in the Product table should be set to Sold for the products that have been purchased. Both of these operations should be atomic.
NOTE
Two transactions are said to be atomic if both of them either fail together or succeed. If either of them fails, then the other transaction should also be committed.
The component should be developed as a .NET assembly, but should be available to COM-based clients because the kiosks still run software built using Visual Basic 6.0.
Construction
During this phase, the NewTech team will construct an Order Processing assembly.
796 Project 8 .NET ASSEMBLIES WITH MANAGED C++ AND .NET
Testing
You will test the .NET assembly using a COM-based client.
Before getting into the construction of the assembly, let me spend some time discussing the .NET and COM+ services.
.NET and COM+ Services
Microsoft has made .NET its standard platform for Web-based development. At the center of the .NET platform is the Common Language Runtime (CLR), a component technology that is a replacement for COM. Remember, CLR is a replacement for COM, not for COM+ services. You have already learned about COM+ services in a previous chapter. As of now, .NET does not have any plumbing for its own platform. Instead, it is dependent on COM+ services to provide the plumbing required to develop enterprise-level applications. It achieves this by making .NET components backward-compatible with COM. This chapter discusses COM+ programming using .NET components.
COM+ gives runtime service support to classes that need it. If a class needs a service, COM+ ensures that classes supported by it are always created within the context that provides the required services. For instance, if a class requires transaction support, then COM+ services will ensure that all of its instances are within a context that provides the services for the instance’s use. A class indicates that it requires a transaction by using the transaction attribute. Classes that require the services provided by COM+ runtime services and are marked with these attributes are called configured classes. The data about the services required by a class is stored in a repository called a catalog. You can configure your components as COM+ applications. There can be two kinds of COM+ applications:
Library applications
Server applications
In the case of a library application, an instance of the class is loaded into the process space of the client, while in the case of server applications, new objects are created within an instance of a dedicated surrogate process, dllhost.exe.
798 Project 8 .NET ASSEMBLIES WITH MANAGED C++ AND .NET
Using Transactions
All transactions show properties called ACID properties. The term ACID stands for Atomicity, Consistency, Isolation, and Durability. The Distributed Transaction Coordinator (DTC) is the COM+ component that handles transactions. When a client creates a component that is configured in COM+, the COM+ service creates an object, called ObjectContext, which wraps all transactional context information. Using methods exposed by the COM+ services, you can access the ObjectContext object and use it to handle transactions.
In the .NET Framework, the ObjectContext is wrapped in an object called ContextUtil. The ContextUtil object is used to ensure that certain commands execute within the context of a transaction. (You will use the ContextUtil object in the assembly that you will create in this chapter.)
Along with ContextUtil, other attributes and classes in the EnterpriseServices namespace allow you to integrate COM+ services with .NET applications.
Now that you know that COM+ services are accessible in .NET applications, you will proceed to create a project that uses the transaction services.
Creating the Database Tables
You have created a database named ArtShop in the previous chapter. You also created the Customer table. To use the ProcessOrder assembly, you need to add two more tables. The fields and sizes of the Product table are as shown in Figure 27-1. The Product table contains the catalog of all the art items on sale at the ArtShop. ProdID is the primary key of the table.
FIGURE 27-1 The design of the Product table
The Orders table contains the details of the orders placed by customers. The table structure is shown in Figure 27-2. OrderID is the primary key of this table. The following is the field description of the Product table.
USING COM+ SERVICES FROM .NET ASSEMBLIES Chapter 27 799
ProdID. Primary key field of the table
ProdType. Category of the art object on sale
ProdName. Descriptive name of the art object on sale
ProdPrice. Price of the object
PictureFile. Path to a GIF or JPEG file
ProdStatus. Status of the product, either Sold or Unsold
The following are the field descriptions of the Orders table:
OrderID. Primary key field of the table
FIGURE 27-2 The design of the Orders table
CustID. Customer number. It has a foreign key relationship with the CustID field of the Customer table.
SessionID. A value that uniquely identifies the user’s session. It is normally retrieved from the Web server.
OrderDate. Date on which the order was placed
OrderStatus. Indicates whether the order is Pending or Fulfilled
ProdID. The ID of the product this order refers to. It has a foreign key relationship with the ProdID field of the Product table.
Creating the ProcessOrder Assembly
You will now create the basic assembly using the Managed Extensions Class Library Wizard. Perform the following steps to accomplish this task:
1.Start Visual Studio .NET and create a new project. In the New Project dialog box, shown in Figure 27-3, select Managed C++ Class Library as the project type.
