Interoperation Between COM+ and Web Services If you are using any of the Windows XP family of operating systems, which includes XP Professional or Windows .NET Server, you can expose an existing COM+ application as an XML Web service by clicking a single checkbox. This effectively allows you to substitute the SOAP protocol for the DCOM protocol. Microsoft calls this feature COM+ Web services. Although this functionality exposes an XML Web service, it actually uses the .NET remoting infrastructure. The best part about this functionality is that you can use it from either a .NET client or an unmanaged client. Exposing a COM+ Application as an XML Web ServiceI'll walk through a quick example to show you how to expose a COM+ application as an COM+ Web service. I'll use an unmanaged version of the financial component that I built in Chapter 7 of The COM and COM+ Programming Primer . (I included the source code for this component in this book). A COM object diagram for this financial component is shown in Figure 11-4. The component consists of a single COM class with progID of FinancialComponent.TimeValue.1, which implements two interface: ITimeValue(the default) and ITaxCalculator. Figure 11-4. A COM object diagram for the financial component.
You will need to create a server (out of process) COM+ application and add the financial component to it. Now perform the following steps to expose this COM+ application as an XML Web service:
Note If you do not see the Uses SOAP checkbox on the Activation tab, you are probably not on a supported platform. Remember that this functionality is currently only available on Windows XP Professional and Windows .NET Server. If the Uses SOAP checkbox appears on the Activation tab, but it's disabled, check that your COM+ application is a server application and not a library application. When you click OK, the COM+ runtime creates a new virtual directory called FinancialWebService (or whatever name you specified), and it adds the files shown in Table 11-3 to this virtual directory. Table 11-3. The files in a SOAP VRoot
The most important of these files, and the one you are most likely to edit, is the web.config file. The content of this file is shown below. <?xml version="1.0" encoding="utf-8"?> <configuration> <system.runtime.remoting> <application> <service> <wellknown mode="SingleCall" type="FINANCIALCOMPONENTLib.TimeValueClass, FINANCIALCOMPONENTLib, Version=1.0.0.0, Culture=neutral, PublicKeyToken=a59f5f36bc9d34c0" objectUri="Financialcomponent.TimeValue.1.soap" /> <activated type="FINANCIALCOMPONENTLib.TimeValueClass, FINANCIALCOMPONENTLib" /> </service> </application> </system.runtime.remoting> </configuration> The key information is contained in the service subelement. By now the contents of this file should be familiar to you. Notice that this configuration file specifies that the server supports server activation in single call mode (see the wellknown subelement beneath the service subelement). It also supports client activation (see the activated subelement beneath the service subelement). Given that the Web service supports both server and client activation, the choice of which one to use is determined by the client. You will see how to make this choice when I show you how to create a client for the COM+ Web service. You can change the server-activated mode to Singleton by altering the web.config file as follows : <?xml version="1.0" encoding="utf-8"?> <configuration> <system.runtime.remoting> <application> <service> <wellknown mode="Singleton" type="FINANCIALCOMPONENTLib.TimeValueClass, FINANCIALCOMPONENTLib, Version=1.0.0.0, Culture=neutral, PublicKeyToken=a59f5f36bc9d34c0" objectUri="Financialcomponent.TimeValue.1.soap" /> <activated type="FINANCIALCOMPONENTLib.TimeValueClass, FINANCIALCOMPONENTLib" /> </service> </application> </system.runtime.remoting> </configuration> You can control the life cycle of client-activated object using the Lifetime subelement exactly as I showed you before. For instance, add the following bolded text to the web.config file: <?xml version="1.0" encoding="utf-8"?> <configuration> <system.runtime.remoting> <application> <service> <wellknown mode="SingleCall" type="FINANCIALCOMPONENTLib.TimeValueClass, FINANCIALCOMPONENTLib, Version=1.0.0.0, Culture=neutral,PublicKeyToken=a59f5f36bc9d34c0" objectUri="Financialcomponent.TimeValue.1.soap" /> <activated type="FINANCIALCOMPONENTLib.TimeValueClass, FINANCIALCOMPONENTLib" /> </service> <lifetime leaseTime="45S" renewOnCallTime="45s" /> </application> </system.runtime.remoting> </configuration> Adding that bolded text causes the initial lifetime of a client-activated object to be 45 seconds. The renewOnCallTime attribute specifies that the life cycle will be extended 45 seconds each time a call is made on the object. The COM+ runtime will create the web.config, default.aspx , and default.disco files in the following physical directory: [System32]\com\ SoapVRoots\[vrootname] where [System32] is the system directory for your machine, and vrootname is the name you entered for the SOAP VRoot (FinancialWebService for this example). On my machine, the complete, physical path for the Web service is C:\WINNT\system32\com\SoapVRoots\ FinancialWebService . Your COM+ Web service will have the following URL : [ servername ]/[vrootname]/ . If you followed the directions spelled out earlier, the URL for the FinancialWebService should be //localhost/FinancialWebService/ . If you navigate to this URL, you will see the page shown in Figure 11-7. This page is generated from the default.aspx and will contain one entry for each COM class that is exposed by the COM+ application or Web service. Each entry is a hyperlink to the WSDL file that corresponds to that class. Figure 11-7. The default Web page.
The URI for each WSDL file is //[Servername]/[vrootname]/[ProgID].soap? WSDL . In this case, there is only one class. This has a ProgID of FinancialComponent.TimeValue.1, so the URI to retrieve the WSDL file for the Web service is //localhost/FinancialWebService/FinancialComponent.TimeValue.1? WSDL . If you navigate to this URL using Internet Explorer, you will see the WSDL file shown in Figure 11-8. Figure 11-8. The WSDL file for the Web service.
Let's look at building a client for the COM+ Web service. Building a Client for a COM+ Web ServiceNow that you have exposed the COM+ application as a Web service, you can access it from managed code or unmanaged code. CREATING A MANAGED CODE CLIENTYou can use the Web service from managed code the same way that you would use any other Web service. Simply use the Add Web Reference command in Visual Studio .NET the same way that you would use it for any other Web service. Enter the URI for the WSDL file in the Address field of the Add Web Reference as shown in Figure 11-9. Figure 11-9. Using a COM+ Web service from a managed client.
The generated proxy for the COM+ Web service will contain the types shown in Table 11-4. Table 11-4. The types in a proxy for a COM+ Web service
The following code shows client code that calls the COM+ Web service through the generated proxy: 1. private void cmdGetPayment_Click(object sender, 2. System.EventArgs e) 3. { 4. double result; 5. localhost.TimeValueClassBinding obj= 6. new localhost.TimeValueClassBinding(); 7. result=obj.MonthlyPayment(8. short.Parse(txtNumMonths.Text), 9. double.Parse(txtRate.Text), 10. double.Parse(txtLoanAmt.Text)); 11. lblMonthlyPayment.Text=result.ToString(); 12. } Lines 5 and 6 create an instance of the TimeValueClassBinding class. Lines 7 through 10 call the MonthlyPayment method, and line 11 displays the result in a text box. This code will use server activation. You will see how to use client activation shortly. CREATING AN UNMANAGED CODE CLIENTWindows XP Professional and Windows .NET Server contain COM monikers that make it easy to call an XML Web service from an unmanaged client application. Note If your client platform is something other than Windows XP Professional or .NET Server (Windows 2000), you can use the SOAP Toolkit to call an XML Web Service from unmanaged code. The SOAP toolkit is freely available at //msdn.microsoft.com/; search for SOAP Toolkit. Even though I'm showing you how to use these Monikers in the context of demonstrating a COM+ Web service, you can actually use these Monikers to call any XML Web service. Using these Monikers, you can call a Web service using Well Known Object (WKO) mode or Client-Activated Object (CAO) mode, assuming that the Web service supports both modes.
The following code shows how you would use the SOAP Moniker in server-activated mode to access a Web service from an unmanaged Visual Basic client: Set obj = GetObject("soap:wsdl=http://[server]/[vroot]/[progID].soap?WSDL") result = obj.Method(parameters...) Notice that I use the GetObject API method with the SOAP Moniker (see the sidebar in this chapter about Monikers). The soap: at the beginning of the display name that I pass to GetObject indicates that I am using the SOAP Moniker. [Server] is the name of the server, [vroot] is the virtual root for the Web service on that server, and [progID] is the ProgID for the COM component. The following code shows example code for the financial COM+ Web service: Private Sub Command1_Click() Dim obj As Object Dim result As Double Set obj = GetObject("soap:wsdl=http://localhost/financialwebservice/Financialcomponent.TimeValue.1.soap?WSDL") result = obj.MonthlyPayment(360, 5.75, 360000) End Sub CLIENT-ACTIVATED OBJECT (CAO) MODEIn order to use a COM+ Web service in CAO mode, you have to export the SOAP-enabled COM+ application from your server in proxy mode and then import the application into the client from which you want to access the COM+ application as an XML Web service. After you have done this, you can instantiate the application's components in the same way that you would instantiate a local object or a DCOM object, using GetObject and CoCreateInstance for instance. To export a COM+ application from your server, first make sure that you have selected the Uses SOAP checkbox on the Activation tab of the property page for your COM+ application in the Component Services Explorer (see Figure 11-6) and then perform the following steps:
You can now take the MSI file that the Export Wizard generated and run it on a remote client computer. On that computer, you can now use GetObject or CoCreateInstance to use the objects that are exposed through SOAP. For instance, the following code will work on a remote client: Private Sub Command1_Click() Dim obj As Object Dim result As Double Set obj = CreateObject("Financialcomponent.TimeValue") result = obj.MonthlyPayment(360, 5.75, 360000) End Sub If you installed the SOAP-enabled proxy on a client, the client will communicate with the Web service using SOAP; if you have not done this, it will use DCOM. This last point is an important one to emphasize . Just because you have configured a COM+ application to use SOAP does not mean you are limited only to using SOAP. You can still access the COM+ application using DCOM if you would like to. You can also use a COM+ Web service in CAO mode using the type name and assembly moniker as shown here (this is another Moniker supported by Windows XP and Windows .NET Server): Private Sub Command1_Click() Dim obj As Object Dim result As Double Set obj = GetObject("soap:typename=Financialcomponent.TimeValue, assembly=Financialcomponent") result = obj.MonthlyPayment(360, 5.75, 360000) End Sub Remember that the main advantage of CAO activation is that you can have a stateful persistent connection to the server much like what you have with DCOM. |
Team-Fly |
Top |