Building XML Web Services with Visual FoxPro 8

 <  Day Day Up  >  

With Visual FoxPro 8, it's quite a bit simpler. You start with a Web service. A Web service is a project with a single .PRG , compiled to a multithreaded DLL. It must start with a DEFINE CLASS < name > AS SESSION OLEPUBLIC declaration. It must declare the type of data to be returned ” String in this case because we want to return XML ”and it must return the XML string.

Getting the XML string is a snap, thanks to the new XMLAdapter class. We create a connection to SQL Server, execute the SELECT statement, and then instantiate an XMLAdapter and use its ToXML method to create the XML. Listing 5.15 shows the code to return a list of company names and customer IDs for the list box.

Listing 5.15. An XML Web Service (FoxPro 8)
 * FoxClass DEFINE CLASS FoxClass AS Session OLEPUBLIC ConnectionString = ;  [Driver={SQL Server};Server=(local);Database=Northwind;UID=sa;PWD=;] FUNCTION AllCustomers AS String Cmd    = "SELECT CompanyName, CustomerID FROM Customers" Handle = SQLSTRINGCONNECT(THIS.ConnectionString) lr     = SQLEXEC(Handle, Cmd, "Customers") LOCAL xa AS XMLAdapter xa     = CREA ("XMLAdapter") WITH xa     .AddTableSchema("Customers")     .ToXML("lcXML") ENDWITH xa     = NULL RELEASE xa USE IN Customers RETURN lcXML ENDFUNC ENDDEFINE 

The project is called FoxServer, and the class is stored in FoxClass.PRG . I pick the Build option and select the radio button labeled Multithreaded COM Server (dll) in the Build Options dialog, as shown in Figure 5.3.

Figure 5.3. Building the multithreaded DLL for the XML Web Service.

graphics/05fig03.jpg


TIP

You can type BUILD MTDLL FoxServer FROM FoxServer in the command window instead of opening up the project and clicking on the Build button.


To test it, use the following code:

 

 o = CREATEOBJECT ("FoxServer.FoxClass") ? o.AllCustomers() 

The first page of the result appears in Listing 5.16.

Listing 5.16. XML Output from the FoxPro XML Web Service
 <?xml version = "1.0" encoding="Windows-1252" standalone="yes"?> <VFPDataSet> <xsd:schema id="VFPDataSet" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:msdata="urn graphics/ccc.gif :schemas-microsoft-com:xml-msdata">     <xsd:element name="VFPDataSet" msdata:IsDataSet="true">       <xsd:complexType>         <xsd:choice maxOccurs="unbounded">           <xsd:element name="Customers" minOccurs="0" maxOccurs="unbounded">             <xsd:complexType>               <xsd:sequence>                 <xsd:element name="companyname">                   <xsd:simpleType>                     <xsd:restriction base="xsd:string">                       <xsd:maxLength value="40"/>                     </xsd:restriction>                   </xsd:simpleType>                 </xsd:element>                 <xsd:element name="customerid">                   <xsd:simpleType>                     <xsd:restriction base="xsd:string">                       <xsd:maxLength value="5"/>                     </xsd:restriction>                   </xsd:simpleType>                 </xsd:element>               </xsd:sequence>             </xsd:complexType>           </xsd:element>         </xsd:choice>         <xsd:anyAttribute namespace="http://www.w3.org/XML/1998/namespace" graphics/ccc.gif processContents="lax"/>       </xsd:complexType>     </xsd:element>   </xsd:schema>   <Customers>     <companyname>Ana Trujillo Emparedados y helados</companyname>     <customerid>ANATR</customerid>   </Customers>   <Customers>     <companyname>Antonio Moreno Taquera Terrible</companyname>     <customerid>ANTON</customerid>   </Customers> 

However, it's not a Web service unless it can be called from the Internet. To begin with, the service needs to be located in an IIS virtual directory. If you create a directory under Inetpub\WWWRoot , it's automatically accessible. Otherwise , you can open up the IIS Management Console and add the directory where you created this code, and it will also work.

You must also register the DLL. You can do this with a single line of code. The following will open a DOS command window and register your XML Web Service DLL:

 

 ! Regsvr32 FoxServer..dll 

Finally, you need to register your Web service and publish a Web Service Description Language (WSDL, pronounced "wizdil") file so that potential users can see what's available. Open up the Task Pane Manager in Visual FoxPro 8 and click on the XML Web Services tab. Select Publish Your XML Web Service, and follow the dialog to the creation of a WSDL file. If that worked, pick Register an XML Web Service and enter the name of the WSDL file you just created. Yeah, you should have written it down.

You're now ready to consume your Web service in your client application. The code's already written for you. Open the Task Pane in Visual FoxPro and select the Web Services tab. Go down to the next section on the Task Pane, Explore and XML Web Service, and pull down the combo box. Your new Web service will be in the list. Select it, and the edit box below fills with about 15 lines of code. You can copy and paste this code into your consuming application, right where you would otherwise have executed a SQLEXEC() to get your company and customerid cursor. You can also open the toolbox and drag and drop the code from the Web Services tab. You only need to add six lines of code, as shown in Listing 5.17.

Listing 5.17. Web Service Code Generated by FoxPro
 LOCAL loFoxClass AS "XML Web Service" * Do not remove or alter following line. It is used to support IntelliSense *__VFPWSDef__: loFoxClass = http://localhost/foxws/foxclass.wsdl , FoxClass , FoxClassSoapPort LOCAL loException, lcErrorMsg, loWSHandler TRY    loWSHandler = NEWOBJECT("WSHandler",IIF(VERSION(2)=0,"",;                            HOME()+"FFC\")+"_ws3client.vcx")    loFoxClass = loWSHandler.SetupClient(;    "http://localhost/foxws/foxclass.wsdl", "FoxClass", "FoxClassSoapPort")    * I inserted the following 6 lines of code:    lcXML = loFoxClass.AllCustomers()    XMLTOCURSOR(lcXML, [DataFromWebService])    SELECT (.MainTable)    ZAP    APPEND FROM DBF ([DataFromWebService])    USE IN DataFromWebService CATCH TO loException    lcErrorMsg="Error: " + TRANSFORM(loException.Errorno) ;                         + " - "+loException.Message    DO CASE       CASE VARTYPE(loFoxClass)#"O"       CASE !EMPTY(loFoxClass.FaultCode)            lcErrorMsg=lcErrorMsg+CHR(13)+loFoxClass.Detail       OTHERWISE    ENDCASE    MESSAGEBOX(lcErrorMsg) FINALLY ENDTRY 

Writing a Web Service function to accept a parameter is just as easy. Simply declare the function with parameters ”not the PARAMETERS statement used in FoxPro, however. You have to include them in parentheses, with data type declarations, just as you would do in Visual Basic. Add the function shown in Listing 5.18 to the FoxClass.PRG and rebuild the DLL.

Listing 5.18. Additional Web Service to Return a Single Customer
 FUNCTION OneCustomer (CustID as String) as String Cmd    = "SELECT * FROM Customers WHERE CustomerID ='" + CustID + "'" Handle = SQLSTRINGCONNECT(THIS.ConnectionString) lr     = SQLEXEC(Handle, Cmd, "Customers") LOCAL xa AS XMLAdapter xa     = CREA ("XMLAdapter") WITH xa     .AddTableSchema("Customers")     .ToXML("lcXML") ENDWITH xa     = NULL RELEASE xa USE IN Customers RETURN lcXML ENDFUNC 

Finally, you'll need a Web service to send UPDATE , INSERT , and DELETE statements back to the server.

You can also write a Web service to accept a diffgram and apply the changes back to SQL. However, be aware that FoxPro diffgrams are not identical to SQL Server diffgrams , so you will need to do some additional work. For now, sending SQL pass-through statements works fine, and you already know how to write them. The function shown in Listing 5.19 shows how to handle them.

Listing 5.19. Additional Web Service to Accept a Diffgram
 FUNCTION SPT (Cmd as String) AS String Handle = SQLSTRINGCONNECT(THIS.ConnectionString) lr     = SQLEXEC(Handle, Cmd, "Customers") Return IIF (lr < 0, [Error], [Ok]) ENDFUNC 

Now that you've made those changes, your FoxPro SQL application needs only to change from calling SQLExec to calling the appropriate WebService function and passing the SQL command built using the SendInsert , SendUpdate , or SendDelete function.

 <  Day Day Up  >  


Visual Fox Pro to Visual Basic.NET
Visual FoxPro to Visual Basic .NET
ISBN: 0672326493
EAN: 2147483647
Year: 2004
Pages: 130
Authors: Les Pinter

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