COM Components

Snoops

   

 
Migrating to .NET: A Pragmatic Path to Visual Basic .NET, Visual C++ .NET, and ASP.NET
By Dhananjay  Katre, Prashant  Halari, Narayana  Rao  Surapaneni, Manu  Gupta, Meghana  Deshpande

Table of Contents
Chapter 4.   Post-migration Changes


COM+ Components

Microsoft Transaction Server (MTS) components and COM+ services are used in the majority of Visual Basic 6.0 business applications. Normally COM+ applications can be classified as server or library applications.

The Microsoft .NET Framework exposes all the existing COM+ services to Visual Basic .NET applications. Thus Visual Basic .NET supports COM+ services. However, the way that the COM+ services are used by Visual Basic .NET applications is different from the way they are used in Visual Basic 6.0 applications. The Microsoft .NET Framework provides the namespace System.EnterpriseServices . This namespace provides all the classes that provide COM+ services to .NET applications.

Code Examples

Let us create a COM+ component in Visual Basic 6.0 and use it in a client Visual Basic 6.0 application. The class DatabaseClass has one method called PerformOperations . In this method two string parameters are passed. These strings basically contain SQL update queries. If one of them fails, the entire transaction is rolled back. The following code is in the MyComPlus folder for this chapter:

 graphics/icon01.gif Public Sub PerformOperations(strCommand1 As String,_   strCommand2 As String)     Dim objContext As ObjectContext     Set objContext = GetObjectContext()     On Error GoTo AbortTransaction     Dim objConnection As ADODB.Connection     Set objConnection = New ADODB.Connection     objConnection.Open "DSN=ADOTest" & ";UID=sa;PWD="     MsgBox ("Connection opened")     objConnection.Execute (strCommand1)     MsgBox ("First Command")     objConnection.Execute (strCommand2)     MsgBox ("Second Command")     objConnection.Close     objContext.SetComplete     Exit Sub  AbortTransaction:     MsgBox ("Failure")     objContext.SetAbort  End Sub 

The client project to use this COM+ component follows . This project contains two command buttons . The button with the caption Transaction invokes the method PerformOperations in the COM+ component. This application connects to the pubs database on the SQL Server 7.0 database. It updates the authors table through two update queries. The following code is in the COMClient folder for this chapter:

 graphics/icon01.gif Private Sub cmdExit_Click()        End     End Sub     Private Sub cmdTransaction_Click()        Dim X As Object        Set X = GetObject("", _         "DatabaseUsingCOMPlus.DatabaseClass")        If X Is Nothing Then           MsgBox "Failed to get the object"        Else           X.PerformOperations _  "Update authors set zip='66666' where au_lname='White'", _  "Update authors set au_lname='Curved'where au_fname='Dean'"        End If     End Sub 

First we will migrate the client application and check whether the migrated Visual Basic .NET application can use the original COM+ component. The upgraded application follows. It is kept in the COMClient-VB.NET folder for this chapter.

 graphics/icon01.gif Private Sub cmdExit_Click(ByVal eventSender As _      System.Object, ByVal eventArgs As System.EventArgs) _        Handles cmdExit.Click        End     End Sub     Private Sub cmdTransaction_Click(ByVal eventSender As _      System.Object, ByVal eventArgs As System.EventArgs) _       Handles cmdTransaction.Click        Dim X As Object        X = GetObject("", _            "DatabaseUsingCOMPlus.DatabaseClass")        If X Is Nothing Then           MsgBox("Failed to get the object")        Else           X.PerformOperations(_  "Update authors set zip='66666' where au_lname='White'",_  "Update authors set au_lname='Curved' where au_fname='Dean'")        End If       End Sub 

This upgraded code works in the same manner as the original Visual Basic 6.0 application. Thus, it is seen that the Visual Basic .NET applications that use COM+ components can be upgraded. When the COM+ project is upgraded to Visual Basic .NET, there are compilation errors. The preceding code can be rewritten as follows. The code is kept in the MyComPlus-VB.NET folder for this chapter.

 graphics/icon01.gif Option Strict Off  Option Explicit On  Imports System.EnterpriseServices  Imports System.Reflection  <Assembly: AssemblyKeyFile("Text.snk")>  <System.Runtime.InteropSer- vices.ProgId("DatabaseClass_NET.DatabaseClass"), Transac- tion(TransactionOption.Required)> Public Class DatabaseClass      Inherits ServicedComponent      <AutoComplete()> Public Sub PerformOperations(ByRef  strCommand1 As String, ByRef strCommand2 As String)          Try              Dim objConnection As ADODB.Connection              objConnection = New ADODB.Connection()              objConnection.Open("DSN=ADOTest" & ";UID=sa;PWD=")              MsgBox("Connection opened")              objConnection.Execute(strCommand1)              MsgBox("First Command")              objConnection.Close()              objConnection = New ADODB.Connection()              objConnection.Open("DSN=ADOTest" & ";UID=sa;PWD=")              objConnection.Execute(strCommand2)              MsgBox("Second Command")              objConnection.Close()          Catch              MsgBox("Failure")    ContextUtil.SetAbort()          End Try      End Sub  End Class 

First of all a key-pair file called test.snk is generated using the sn.exe utility provided with the Microsoft .NET Framework. This file is used for strong naming the assembly. Note that the System.EnterpriseServices and System. Reflection namespaces are imported into the assembly. The System.EnterpriseServices namespace provide for COM+ services, and the System.Reflection namespace provides attributes for specifying the key file for the assembly. The COM+ services are provided by the attribute Transaction at the class level and the attribute AutoComplete at the method level. Once the code is compiled into a DLL file, it has to be registered on the machine with the command regsvcs utility provided with the Microsoft .NET Framework. Then it is put into the global assembly cache with the gacutil.exe utility. The COM+ component gets the name DatabaseClass_NET.DatabaseClass by default. The Visual Basic .NET client application can be changed to access the Microsoft .NET COM+ component as follows. This code is kept in the COMClient-VB. NET-Modified folder for this chapter.

 graphics/icon01.gif Private Sub cmdExit_Click(ByVal eventSender As _      System.Object, ByVal eventArgs As System.EventArgs)_       Handles cmdExit.Click        End     End Sub     Private Sub cmdTransaction_Click(ByVal eventSender As _      System.Object, ByVal eventArgs As System.EventArgs) _       Handles cmdTransaction.Click        Dim X As Object        X = GetObject("", "DatabaseClass_NET.DatabaseClass")        If X Is Nothing Then           MsgBox("Failed to get the object")        Else           X.PerformOperations(_  "Update authors set zip='22222' where au_lname='White'", _  "Update authors set au_lname='Curved' where au_fname='Dean'")        End If     End Sub 

Even though the example is very simple, most of the COM+ components are upgraded in the same manner. That is, appropriate classes from the System.EnterpriseServices must be used as attributes in the assembly to make it a COM+ component. Also, these assemblies must have strong names and must be registered using the regsvcs utility provided by the .NET Framework.


Snoops

   
Top


Migrating to. NET. A Pragmatic Path to Visual Basic. NET, Visual C++. NET, and ASP. NET
Migrating to. NET. A Pragmatic Path to Visual Basic. NET, Visual C++. NET, and ASP. NET
ISBN: 131009621
EAN: N/A
Year: 2001
Pages: 149

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net