Kamis, 12 Januari 2012

Walkthrough: Creating Web Services with Visual FoxPro


McAlister Merchant
Microsoft Corporation
March 2001
Applies To: Microsoft Visual FoxPro 7.0
Summary: Web Services can be available anywhere. You can either create them yourself or access them over the Web from wherever they happen to be published. You can use Visual FoxPro to create Web Services, and if you register them in IntelliSense, Visual FoxPro makes it possible for you to access them almost automatically. You also can use Visual FoxPro to access Web Services created in other languages. (6 printed pages)

Contents

Introduction
Web Services
Establishing a Virtual Directory
Creating a Project
Creating a COM Server
Creating and Publishing a Web Service
Registering a Web Service
Using a Web Service

Introduction

Creating Web Services using Microsoft® Visual FoxPro® 7.0 is a complex process that involves using Microsoft® Internet Information Services (IIS), Visual FoxPro COM servers, and Simple Object Access Protocols (SOAP). You can use IIS, which installs automatically with Visual FoxPro, to create a virtual directory that references the location on your hard disk where you place your services.
Then, you can use the Project manager to create or to compile COM objects. When the COM objects are created, you can use the Visual FoxPro Web Services wizard to publish them in your virtual directory.
You can use the IntelliSense manager to register Web Services with Visual FoxPro, so that the placement of the Web Service and necessary calling routines in code is expedited.
This paper walks you through the processes involved in making Web Services of your existing COM servers and preparing Web Services for use. From a COM server, you create a very simple Web Service that is accessible by anyone through HTTP. This Web Service provides a simple report of the current directory and how many .jpg files are there.

Web Services

Web Services extend the accessibility to your own COM servers, as well as making Web Services from other developers accessible to you through your browser.
Web Services provide functionality and access to data between platforms and across diverse connections. You create and use Web Services to provide or to gain functionality from wherever it happens to reside. The most important aspect of Web Services is that, because of the way they use the HTTP protocol and the SOAP proxy, functionality is available through firewalls. Because HTTP and SOAP are platform agnostic, Web Services are also platform agnostic. In some cases, this can provide functionality that was unavailable under COM alone.
To publish a Web Service, you must create a virtual directory in IIS, and then create or copy a COM server from which you generate a Web Service to your virtual directory.

Establishing a Virtual Directory

You must create or identify a directory into which you will place your Web Services. Although you can use an existing directory, you might want to create a separate place on your hard disk to store these files, making it easier to manage your Web Service files.

To create the virtual directory:

  1. Create a folder named Web Services in the main Visual FoxPro directory (C:\Program Files\Microsoft Visual FoxPro 7).
  2. In the Administrative Tools folder of the Microsoft Windows® control panel, double click the Internet Services Manager shortcut.
  3. The Internet Information Services window opens, displaying a tree in the left pane that shows your hard disk. Expand this node to see available sub folders.
  4. Select the Default Web Site node in the Tree pane
  5. From the right-click menu, select New, and click Virtual Directory. This opens the Virtual Directory Creation wizard.
  6. Follow the steps in the wizard, and specify the Web Services folder that you created earlier as your virtual directory.
You have created a virtual directory that references the Web Services folder in which you will store all the Web Services files you create.

Creating a Project

The Project manager is the most fundamental application-building tool in Visual FoxPro. Within a Visual FoxPro project, you can assemble documents of all types, data, classes, code, and other file types. A project provides access to all the components you might want to include in a server and contains them for compilation. In this example, you use the project to compile your Web Service.

To create a project:

  1. In the File menu, select New.
  2. In the New dialog box, select Project, and click New file.
  3. In the Create dialog box, select the Web Services folder you created earlier, and name your project myWServ1.
  4. Click Save.
Now, you have created your project in the Web Services folder. This makes it possible for you to save and compile the project to the correct folder automatically when you have finished adding components to it.

Creating a COM Server

Because, a Web Service is a .dll or .exe file, you must use a COM server as the basis for your Web Service. A COM server in Visual FoxPro is a file with .dll or .exe extension containing a class built as OLEPUBLIC from a project. Objects specified as OLEPUBLIC are accessible by Automation clients. From the .dll file, a new .wsdl file will be generated when you publish the Web Service. This .wsdl file has the necessary XML code that describes the classes in the .dll and references which .dll file is the driving force behind that particular Web Service.
For this COM server example, you will create a custom class that reports some very simple information. In this process, you will write a program that creates the class with two methods and compile the project containing it into both a COM server and then a published Web Service.

To create a simple class:

  1. In the File menu, select New.
  2. In the New dialog box, select Project, and click New file. This opens the Project Manager.
  3. In the All tab of the Project Manager, expand Code, select Programs, and then click New.
  4. Copy the following code into the code edit window.
    *-- returns curdir() and number of JPEG files
    
    DEFINE CLASS showem AS session OLEPUBLIC
    
       Name = "showem"
    
       PROCEDURE justshow
       howmany=ADIR(afilArry,"*.jpg")
       IF howmany <1
          fileNm="no"
       ELSE
          fileNm=ALLTRIM(STR(howmany))
       ENDIF
       
       X = "about   " + CURDIR() + " ... "
       Y = "There are "  + fileNm + " JPEG files here."
       RETURN x + y 
       ENDPROC
    ENDDEFINE
    
    This program defines one class, showem, which has one method, justshow.
  5. Close the edit window. In the Save As dialog box, name the program myWServ1, and click Save.
  6. Make sure myWServ1 is set to Main. (You can tell if it is set to Main, because it will be in bold font.) If it is not, in the Project Manager, right-click myWServ1 and click Set Main in the shortcut menu.

To compile the COM server:

  1. In the Project Manager, click the Build button.
  2. Select Single-threaded COM server.
  3. Click OK, and accept the name myWServ1.
Visual FoxPro will compile the myWServ1 file into a COM server, after which you can turn this COM server into a Web Service.

Creating and Publishing a Web Service

To add the components necessary to turn your COM server into a Web Service, you must compile the COM server as a Web Service. After you specify the settings, Visual FoxPro creates the XML and SOAP files necessary to turn your server into a Web Service, registers your new Web Service, and publishes it in your virtual directory.

To compile and publish a Web Service:

  1. After you have completed the previous procedures and compiled the COM server myWServ1.dll, right-click the Project Manager, and choose Builder from the shortcut menu.
  2. In the Wizard Selection dialog box, choose Web Services Publisher, and then click OK.
  3. In the Visual FoxPro Web Services Publisher dialog, use the Ellipsis button (...) to locate myWServ1, and then select the class you want to register from that server. In the COM server you created, myWServ1, you have only one class, showem.
    Note   It is a good idea to click the Advanced button to make sure the Web Services Publisher dialog box is referencing the correct files, especially if you have created similarly-named servers or services in different folders. If the correct file is not displayed in the drop-down list, use the Ellipsis button (...) to locate the correct file. The IntelliSense scripts field displays the name under which you (and the SOAP toolkit) will find this Web Service. After verifying the information in the Advanced Web Services Publisher dialog box, click OK.
  4. Click the Generate button. After Visual FoxPro generates the Web Service files, the Web Services Publishing Results dialog box appears, displaying a list containing the COM server, .wsdl file, ISAPI or ASP listener, and IntelliSense path names and compilation results.
At this point, you have created and published your Web Service. Anyone who has access to the virtual directory you established earlier can use HTTP to access the Web Service you just created.
To use this Web Service, someone must write code that invokes the SOAP client, a listener, and the browser and must employ several layers of protocol and process. In Visual FoxPro, this is simplified by registering Web Services in the IntelliSense manager.

Registering a Web Service

You can register Web Services (both those created by you and those created by others) in the IntelliSense manager. When you do this, IntelliSense provides the supporting code whenever you access a registered Web Service.

To Register a Web Service:

  1. On the Tools menu, select Intellisense Manager.
  2. On the Types tab of Intellisense Manager, click the Web Services button to register your new Web Service (your new class also will appear in the Types list).
Now, IntelliSense will provide your Web Service as an optional selection whenever you add a strongly typed variable to your Visual FoxPro code.

Using a Web Service

You use a Web Service the same way you would any COM server to expose functionality of one application to another application or program. For example, you can create a Visual FoxPro program that calls the sample Web Service, myWServ1. In a Visual FoxPro program, declare a strongly typed variable as in the following line of code, in which you reference your Web Service, myServ1, by choosing it from the IntelliSense drop-down list:
LOCAL x AS myWServ1
Visual FoxPro IntelliSense uses the registered information on this Web Service to complete the following lines of code:
x = CREATEOBJECT("MSSOAP.SoapClient")
x.MSSoapInit("http://TheVirtualDisk/web services/ myWServ1.WSDL", , " myWServ1SoapPort")
Then, you call the method of that Web Service just as you do any COM object or class method as in the following code:
? x.justshow()
You can add similar code to access your own or other Web Services to Visual FoxPro programs and applications.
For more information on Web Services, see "Web Services Overview" in the Visual FoxPro Help. For more information on COM servers, see "Creating Automation Servers" in the Visual FoxPro Help.

Tidak ada komentar:

Posting Komentar