Partager via


Walkthrough: Creating XML Web Services with Visual FoxPro

XML Web services are the fundamental building blocks for distributed computing on the Internet. An XML Web service is an object or class that is deployed on the Internet and that you can access programmatically through typical method calls. XML Web services provide functionality and access to data between platforms and across diverse connections. The most important aspect of XML Web services is that because of how they use HTTP and SOAP, functionality is available through firewalls. You can call an XML Web service regardless of the platform you are running because HTTP and SOAP operate independently from your computer platform. In some cases, this can provide functionality that was unavailable under distributed COM.

You can create XML Web services or access them on the Web from wherever they are published. You can use Visual FoxPro to create an XML Web service, or you can use Visual FoxPro to access an XML Web service created in other programming languages.

This walkthrough explains how to create a COM Server and publish it as an XML Web service. Additionally, it guides you through using your newly created XML Web service. The XML Web service provides a list of customers from a table where the customers meet a certain requirement. The list is sent as an XML string that is then placed into a cursor using the XMLAdapter class.

This walkthrough contains the following sections:

  • Prerequisites

  • Internet Information Services

  • Creating a COM Server

  • Creating and Publishing an XML Web Service

  • Testing an XML Web Service

  • Using an XML Web Service

Prerequisites

This walkthrough requires the following:

  • Microsoft Internet Information Services (IIS)

  • SOAP

  • Visual FoxPro COM Servers

SOAP is automatically installed when you install Visual FoxPro. IIS is an optional component for Windows 2000 and is automatically installed on Windows 2000 Server. IIS serves your XML Web services from your virtual directory to users on the Internet. Its virtual directory references the location on your hard disk where you place your XML Web services. You use Visual FoxPro to create COM objects. After your COM objects are created, you can then use the Visual FoxPro XML Web Service builder to publish them to your virtual directory.

Internet Information Services

IIS provides the reference to the location of your XML Web service to users accessing it from the Internet. To publish an XML Web service, you must create a virtual directory in IIS that contains the necessary Web Services Description Language (.wsdl) files. These files are created by the Web Service Wizard from your COM component.

To create a virtual directory

  1. In the main Visual FoxPro directory ..\Program Files\Microsoft Visual FoxPro VersionNumber, create a folder named Web Services.

  2. In the Windows Control Panel, double-click Administrative Tools, and then double-click Internet Information Services.

    The Internet Information Services window opens, displaying a tree control on the left that displays a list of Internet information services on your computer.

  3. Right-click the Default Web Site node.

  4. Click New, and then click Virtual Directory to open the Virtual Directory Creation Wizard.

  5. Follow the instructions in the wizard, and specify the Web Services folder that you created in step 1 as your virtual directory.

    This virtual directory references the Web Services folder where you will store all the XML Web service files you create.

Creating a COM Server

Because an XML Web service is a dynamic-link Library (.dll) or executable (.exe) file, you must use a COM Server as the basis for your XML Web service. A COM Server in Visual FoxPro is a file with a .dll or .exe extension, containing a custom class based on the Session object. The custom class is specified as OLEPUBLIC when it is defined. This exposes functionality that can be used and reused by other applications through automation.

For this COM Server example, you create a custom class that reports some basic information. You write a program that creates a class with one method, compile the project containing it into a COM Server, and then publish it as an XML Web service.

To create a COM Server

  1. On the File menu in Visual FoxPro, click New.

  2. In the New dialog box, click Project, and then click New file. Save the project as myWServ1.

  3. In the All tab of Project Manager, expand Code, click Programs, and then click New.

  4. Copy the following code into the Editing window.

    DEFINE CLASS ShowCustomers AS Session OLEPUBLIC
       PROCEDURE CustomersInGermany AS String
          LOCAL loXMLAdapter AS XMLAdapter
          LOCAL lcXMLCustomers AS String
    
          loXMLAdapter = CREATEOBJECT("XMLAdapter")
    
          OPEN DATABASE "C:\Program Files\Microsoft Visual FoxPro 9\" ;
             + "Samples\Northwind\northwind.dbc"
    
          USE customers
          SELECT * ;
             FROM customers ;
             WHERE country LIKE "Germany%" ;
             INTO CURSOR curCustomers
    
          loXMLAdapter.AddTableSchema("curCustomers")
          loXMLAdapter.UTF8Encoded = .T.
          loXMLAdapter.ToXML("lcXMLCustomers")
    
          CLOSE DATABASES ALL
    
          RETURN lcXMLCustomers
       ENDPROC
    ENDDEFINE
    

    This program defines one class, ShowCustomers, that has one method, CustomersInGermany.

  5. On the File menu, click Save.

  6. In the Save As dialog box, name the program myWServ1, and click Save.

  7. Close the Editing window.

  8. In Project Manager, click Build.

  9. Select Multi-threaded COM server (dll), and click OK. In the Save As dialog locate the Web Services directory you created under the main Visual FoxPro directory. Save the DLL as myWServ1.dll into the Web Services directory.

Note

If the DLL is saved in a directory that IIS/SOAP cannot access, the calls to the WebService result in the SOAP error "Class not registered."

Visual FoxPro now compiles the file myWServ1 into a COM Server called myWServ1.dll. You can now publish it as an XML Web service.

Creating and Publishing an XML Web Service

From the myWServ1.dll file, created in the previous procedure, a new Web Services Description Language (.wsdl) file is generated when you publish the XML Web service. The .wsdl file is an XML file that describes the XML Web service and the classes it contains.

To add the components necessary to turn your COM Server into an XML Web service, you must compile the COM Server as an XML Web service. After you specify the settings in the Web Services Publisher, Visual FoxPro creates the SOAP files necessary to turn your COM Server into an XML Web service, publishes them in your virtual directory, and registers your new XML Web service in IntelliSense.

To compile and publish an XML Web service

  1. On the Tools menu, click Task Pane.

  2. In the Task Pane Manager, click XML Web Services.

  3. Under XML Web Service Tools, select Publish Your XML Web Service.

  4. If the file myWServ1.dll is not listed in the COM Server field, click the ellipsis (...) button, and select myWServ1.dll.

  5. Click Advanced to display the Visual FoxPro XML Web Services Publisher dialog box.

  6. Make sure the Listener URI box displays your virtual directory. If it does not, click the ellipsis (...) button, and select your virtual directory.

  7. Click the Methods tab, and make sure that CustomersInGermany is selected.

  8. Click the Namespaces table, replace tempuri.org with your domain in all fields, and click OK.

    If you do not have your own domain, you can leave tempuri.org for this example.

  9. Click OK to close the Visual FoxPro XML Web Services Publisher dialog box.

  10. Click Generate.

    Visual FoxPro generates the XML Web service files. After it is finishes, the XML Web Services Publishing Results dialog box appears, displaying a list of files generated, .wsdl file, ISAPI or ASP listener, and whether the file was generated properly.

At this point, you have created and published your XML Web service. Anyone who has access to the virtual directory you established earlier can use HTTP to access the XML Web service you published.

Testing an XML Web Service

After you have published your new XML Web service, you should test it to make sure it is working correctly. Visual FoxPro provides a mechanism to test XML Web services that is available in the XML Web Service pane in Task Pane Manager. The test mechanism displays any results returned from the XML Web service, XML schemas used, error feedback.

To test a XML Web service

  1. On the Tools menu in Visual FoxPro, click Task Pane.

  2. In Task Pane Manager, click XML Web Services.

  3. Under Explore an XML Web Service, click ShowCustomers in the Service Name box.

  4. In the The following methods are supported by this XML web service box, click CustomersInGermany.

    This box lists all the available methods on the XML Web service.

  5. Click Test this XML Web Service.

If testing is successful, the Web Service Test Results window appears. It displays the results from the XML Web service, and you can view the .wsdl file associated with the XML Web service. If there is an error in testing, the Web Service Test Results window displays information on the error.

Using an XML Web Service

You use an XML Web service the same way you would any COM Server: to expose functionality of one application to another. In the following procedure, you create a program (.prg) file to access your XML Web service and use the Toolbox to add the required code that references the XML Web service properly.

To use an XML Web service

  1. On the File menu in Visual FoxPro, click New.

  2. In the New dialog box, select Project, and click New file. Save the project as wsTest.

  3. On the All tab of Project Manager, expand Code, select Programs, and then click NewOn the Tools menu, click Toolbox.

  4. In the Toolbox, click the My XML Web Services.

  5. Click and drag the ShowCustomers XML Web service from the Toolbox to the Editing window.

    This adds the proper code to your XML Web service.

  6. In the Editing window, below the line that reads, "Call your XML Web service here," insert the following code.

    LOCAL lcXML, lcAlias AS String
    LOCAL loXMLAdapter AS XMLAdapter
    
    lcXML = loShowCustomers.CustomersInGermany
    
    loXMLAdapter = CREATEOBJECT("XMLAdapter")
    loXMLAdapter.LoadXML(lcXML)
    
    lcAlias = loXMLAdapter.Tables.Item(1).Alias
    loXMLAdapter.Tables.Item(1).ToCursor()
    
    SELECT(lcAlias)
    BROWSE
    CLOSE DATABASES ALL
    
  7. On the File menu, click Save.

  8. In the Save As dialog box, name the program WebServiceTest, and click Save.

  9. Close the Editing window.

  10. In Project Manager, select the WebServiceTest program file, and click Run.

    A browse window appears with all the customers from Germany selected in the Northwind database. This cursor was populated from XML provided from your XML Web service using the XMLAdapter class.

For more information on Web Services, see Web Services and Components. For more information on COM Servers, see How to: Create Automation Servers.

See Also

Other Resources

Walkthroughs