Microsoft KB Archive/317880

= How to expose an ActiveX DLL as a Web service by using COM+ =

Article ID: 317880

Article Last Modified on 12/30/2005

-

APPLIES TO


 * Microsoft COM+ 1.5
 * Microsoft .NET Framework Class Libraries 1.0
 * Microsoft Visual Studio .NET 2002 Professional Edition
 * Microsoft Visual Basic .NET 2002 Standard Edition

-



This article was previously published under Q317880



IN THIS TASK
SUMMARY
 * Requirements
 * Create a COM+ Application
 * Add the ActiveX Component to the COM+ Application
 * Expose the DLL as a Web Service
 * Create a Windows Application to Consume the Web Service
 * Add a Web Reference
 * Test the Procedure
 * Troubleshooting

Complete Code Listings
 * Complete Code Listing (Products.cls)
 * Complete Code Listing (Form1.vb)

REFERENCES



SUMMARY
This step-by-step article describes how to use the COM+ Application Install Wizard that ships with the Component Services console in Microsoft Windows XP Professional to expose and consume a legacy Microsoft Visual Basic COM component as a Web service.

back to the top

Requirements
The following list outlines the recommended hardware, software, network infrastructure, and service packs that are required:
 * Windows XP Professional or Windows Server 2003 with the Microsoft .NET Framework installed
 * Microsoft Visual Basic 5.0 or later
 * Microsoft Visual Studio .NET
 * Microsoft SQL Server 7.0 or later, with the Northwind database

back to the top

Create a COM+ application
To create a COM+ application, follow these steps:  Create a small Visual Basic 5.0 or 6.0 application that contains a single method called OrderTotal. When you pass in an OrderID, this method issues a SELECT statement to the database and calculates the total price for that order. It then returns the order total. On the Start menu, point to Programs, point to Microsoft Visual Studio 5.0 or Microsoft Visual Studio 6.0, and then click Microsoft Visual Basic 5.0 or Microsoft Visual Basic 6.0. In New Project dialog box, select ActiveX DLL, and then click Open. Press CTRL+R to open Project Explorer, select Project1, and then press F4 to access the properties for the project. Change the Name property to HowToCOMXP. Select Class1.cls, and then change the Name property to Products.  In the Code Editor window, paste the following code in the Class module. Note that you may need to modify the connection string for your computer: Public Function OrderTotal(OrderID As String) As Double Dim cn As ADODB.Connection Set cn = New ADODB.Connection cn.ConnectionString = &quot;provider=sqloledb;data source=(local);initial catalog=northwind;uid=sa&quot; cn.Open

Dim rs As ADOR.Recordset Set rs = New ADOR.Recordset rs.CursorLocation = adUseClient rs.CursorType = adOpenStatic rs.LockType = adLockBatchOptimistic

rs.Open &quot;select sum(unitprice * quantity) from [order details] where orderid=&quot; & OrderID, cn       rs.ActiveConnection = Nothing cn.Close

OrderTotal = rs(0) End Function This code creates a database connection and a recordset, and then returns the order total for any order in the Northwind database.

 On the Project menu, click References. In the References dialog box, select Microsoft ActiveX Data Objects 2.7 Library and Microsoft ActiveX Data Objects Recordset 2.7 Library under Available References, and then click OK.</li> On the File menu, click Make HowToCOMXP.dll. To keep the path simple, select drive C in the Save in list. Accept the default file name, and then click OK.</li> On the Start menu, click Control Panel, click Administrative Tools, and then click Component Services.</li> In the Component Services dialog box, expand Component Services, expand Computers, and then expand My Computer. Right-click COM+ Applications, click New, and then click Application.</li> When the COM+ Application Install Wizard starts, click Next.</li> Click Create an empty application.</li> Type HowToCOMXP as the name of the application, and then click Next.</li> In the Set Application Identity page, verify that Interactive user is selected, click Next, and then click Finish. This creates the COM+ application.</li></ol>

back to the top

Add the ActiveX component to the COM+ application
After you have created the COM+ application, you must add the ActiveX component to that application. To do this, follow these steps:
 * 1) In the left pane of the Component Services dialog box, expand COM+ Applications, and then expand HowToCOMXP. Right-click Components, click New, and then click Component.
 * 2) When the COM+ Component Install Wizard starts, click Next.
 * 3) Click Install new components. Browse to drive C, select HowToCOMXP.dll, and then click Open. Click Next, and then click Finish. This installs the dynamic-link library (DLL) in the COM+ application.

back to the top

Expose the DLL as a Web service
After you install the DLL in the COM+ application, you must expose the component as a Web service. To do this, follow these steps:
 * 1) Right-click HowToCOMXP, and then click Properties.
 * 2) On the Activation tab, click Uses SOAP. In the SOAP VRoot field, type HowToCOMXPWebService, and then click OK.
 * 3) Browse to C:\Windows\system32\Com\SOAPVRoots\HowToCOMXPWebService to see the virtual root that is created automatically for that COM+ application.
 * 4) Note the Default.aspx page in this folder. This is a valid URL. To browse to this page, start Microsoft Internet Explorer, and then type localhost/HowToCOMXPWebService in the Address field.
 * 5) When the Web page for the Web service appears, click the link to view the Web Service Description Language (WSDL) document for the component that you installed in the HowToCOMXP COM+ application.
 * 6) Close Component Services.

back to the top

Create a Windows application to consume the Web service
To create a Windows application that will serve as a client for (that is, &quot;consume&quot;) the Web service, follow these steps:
 * 1) On the Start menu, click Programs, click Microsoft Visual Studio .NET, and then click Microsoft Visual Studio .NET.
 * 2) Click New Project. Under Project Types, click Visual Basic Projects, and then select Windows Application under Templates.
 * 3) In the Name field, type COMXPClient.
 * 4) When the project opens, press CTRL+ALT+X to open the Toolbox. Drag a TextBox control and a Button control onto Form1. Position the Button control to the right of the TextBox control.
 * 5) Click the lower right corner of Form1, and then drag the cursor up and to the left to shrink Form1 so that an equal amount of space surrounds the controls.
 * 6) Click the Button control, and then press F4 to access the properties for the control. Change the Text property to Order Total.
 * 7) Click the TextBox control. In the Properties dialog box, delete the value for the Text property.

back to the top

Add a Web reference
To use the legacy COM object, you must add a Web reference. To do this, follow these steps: <ol> In Solution Explorer, right-click References, and then click Add Web Reference.</li> In the Address field, type http://localhost/HowToCOMXPWebService/Legacy.Products.soap?wsdl .</li> Press ENTER. When the Add Reference button is enabled, click it to add the reference. You can now call into the COM object with standard Web service protocols.</li>  On Form1, double-click the Button control to create a Click event handler, and then paste the following code in the event handler: Dim ws As New localhost.ProductsClassBinding Dim Total As Double = ws.OrderTotal(TextBox1.Text) MessageBox.Show(&quot;Total Order : $&quot; & Total) The OrderTotal method that you created in the legacy Visual Basic DLL appears as a method of the Web service Proxy class. </li></ol>

back to the top

Test the procedure

 * 1) Press F5 to run the application in Debug mode.
 * 2) When the form appears, type 10248 in the field, and then click Order Total.
 * 3) When you see &quot;Total Order : $440&quot; in the message box, click OK.
 * 4) To view a different result, type 10249, and then click Order Total.

back to the top

Troubleshooting
If you open http://localhost/HowToCOMXPWebService in Internet Explorer and you can see the page but not the Web methods, the interface and method information for the component are not registered. This can occur when you use the Legacy Component Import Wizard to install the legacy components. To resolve this problem, delete and reinstall the COM+ application in Component Services.

back to the top

Complete code listing (Products.cls)
Public Function OrderTotal(OrderID As String) As Double Dim cn As ADODB.Connection Set cn = New ADODB.Connection cn.ConnectionString = &quot;provider=sqloledb;data source=(local);initial catalog=northwind;uid=sa&quot; cn.Open

Dim rs As ADOR.Recordset Set rs = New ADOR.Recordset rs.CursorLocation = adUseClient rs.CursorType = adOpenStatic rs.LockType = adLockBatchOptimistic

rs.Open &quot;select sum(unitprice * quantity) from [order details] where orderid=&quot; & OrderID, cn       rs.ActiveConnection = Nothing cn.Close

OrderTotal = rs(0) End Function back to the top

Complete code listing (Form1.vb)
Public Class Form1 Inherits System.Windows.Forms.Form


 * 1) Region &quot; Windows Form Designer generated code &quot;

Public Sub New MyBase.New

'This call is required by the Windows Form Designer. InitializeComponent

'Add any initialization after the InitializeComponent call.

End Sub

'The form overrides Dispose to clean up the component list. Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean) If disposing Then If Not (components Is Nothing) Then components.Dispose End If       End If        MyBase.Dispose(disposing) End Sub

'This is required by the Windows Form Designer. Private components As System.ComponentModel.IContainer

'NOTE: The following procedure is required by the Windows Form Designer. 'It can be modified using the Windows Form Designer. 'Do not modify it using the code editor. Friend WithEvents TextBox1 As System.Windows.Forms.TextBox Friend WithEvents Button1 As System.Windows.Forms.Button <System.Diagnostics.DebuggerStepThrough> Private Sub InitializeComponent Me.TextBox1 = New System.Windows.Forms.TextBox Me.Button1 = New System.Windows.Forms.Button Me.SuspendLayout '       'TextBox1 '       Me.TextBox1.Location = New System.Drawing.Point(24, 16) Me.TextBox1.Name = &quot;TextBox1&quot; Me.TextBox1.TabIndex = 0 Me.TextBox1.Text = &quot;&quot; '       'Button1 '       Me.Button1.Location = New System.Drawing.Point(136, 16) Me.Button1.Name = &quot;Button1&quot; Me.Button1.TabIndex = 1 Me.Button1.Text = &quot;Order Total&quot; '       'Form1 '       Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13) Me.ClientSize = New System.Drawing.Size(248, 62) Me.Controls.AddRange(New System.Windows.Forms.Control {Me.Button1, Me.TextBox1}) Me.Name = &quot;Form1&quot; Me.Text = &quot;Form1&quot; Me.ResumeLayout(False)

End Sub


 * 1) End Region

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

End Sub

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click Dim ws As New localhost.ProductsClassBinding Dim Total As Double = ws.OrderTotal(TextBox1.Text) MessageBox.Show(&quot;Total Order : $&quot; & Total) End Sub End Class back to the top