Login | Register   
LinkedIn
Google+
Twitter
RSS Feed
Download our iPhone app
TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   FORUMS  |   TIP BANK
Browse DevX
Sign up for e-mail newsletters from DevX


advertisement
 

COM Interop: Making .NET and VFP Talk to Each Other : Page 2

Whether you use a COM component from .NET or a .NET component from a COM-enabled environment (such as VFP), COM Interop is the mechanism that allows for such interoperability.


advertisement
Calling .NET Components from VFP
Many .NET classes are useful to VFP developers via COM interop. For instance, you might want to use the classes that provide GDI+ features. Listing 1 shows a class created in .NET that wraps up some GDI+ functionality. We compiled the class into a class library project in .NET. The most important thing to note here is that the project has been marked as "Register for COM Interop." To do that, right-click on the project, select Properties, select Configuration Properties—Build, and then set the "Register for COM Interop" option to True to expose a .NET class as a COM component (Figure 3).

 
Figure 3: A .NET project set up for COM Interop.
After you compile the project you can immediately use the class through COM from VFP. However, in order to provide a better experience for the user of such class, you might want to apply some attributes to the class. For instance, the class shown in Listing 1 has the following attributes applied:

[ClassInterface(ClassInterfaceType.AutoDual)] [ProgId("VFPAndDotNet.ImageHelper")]

The ClassInterface attribute, set to ClassInterfaceType.AutoDual, enables the IntelliSense support in VFP. The ProgId attribute specifies the ProgId that VFP will use when instantiating the .NET component as a COM component. For example, you can use the ImageHelper class in VFP like so:

*-- Instantiate the .NET class as a COM component. oHelper = CreateObject("VFPAndDotNet.ImageHelper") *-- Set some properties. oHelper.Copyright = "Claudio Lassala" oHelper.ImageFile = "C:\MyImage.BMP" oHelper.SaveFileAs = "C:\CopyOfMyImage.jpg" *-- Call a method on the class. oHelper.ProcessImage()

From the VFP side there is no indication that the object being used is a .NET object.

Calling .NET Web Services from VFP
The one.NET feature that was (and still is) mentioned more than any other is the ability to use .NET to create XML Web services. Web services are methods of functions that are exposed to the Web through a standardized protocol called SOAP. SOAP enables you to access to components in a platform and language neutral fashion. This means that any language and operating system can call any Web service no matter how the Web service was created. This, of course, means that Visual FoxPro can call Web services created in Visual Studio .NET.

We'll show you how to create a .NET Web service before we call one You can easily do this using the Visual Studio .NET IDE.

Authors' Note: If you do not have Visual Studio .NET installed, you can probably follow the example by calling an existing Web service such as one of the many Web services found listed with www.UDDI.org).

If you're following along you have Visual Studio .NET loaded. First create a new ASP.NET Web service project. The language you choose to use does not matter. This example will use VB .NET but if you are more familiar with C#, you should have no difficulty following the examples. Figure 4 shows the New Project dialog box.

 
Figure 4: Web service Project: The figure shows the project template to select when creating a new Web service project in Visual Studio .NET.
When you create a new ASP.NET Web service project, the Visual Studio .NET IDE automatically includes all the required references and creates a Web service source code file (Service1.asmx), with a HelloWorld method as a template. For our purposes we'll delete that method and instead change the code to what you see in Listing 2. You may have noticed that most of the code in Listing 2 is inside a "designer region," which means that developers should never have to touch it. The important part of Listing 2 is the following method:



<WebMethod()> _ Public Function GetCurrentTime() _ As DateTime Return DateTime.Now End Function

This method simply returns the current date and time as a DateTime data type. The only unusual aspect about this is the <WebMethod()> attribute. This attribute tells the ASP.NET runtime that this method is to be exposed through a Web service according to the SOAP standard.

You can start your Web service project (simply press F5) to see a test bench interface in Internet Explorer. In this example, the service is rather simple and has only one exposed method. Click the link to that method and then click the "Invoke" button to run the service. (Note: If your method accepted parameters this interface would provide textboxes to enter those parameters.) You can see the result in Figure 5. The return value of the method is wrapped in XML, which is the key mechanism that allows you to call this service from Visual FoxPro.

 
Figure 5. Testing a Web service: Here's the way the simple Web service looks when using the test bench application provided by Visual Studio and running in Internet Explorer.
You can register a Web service in VFP through the Task Pane Manager under its special "Web services" tab. Click the first link provided in the window, titled "Register an XML Web service." In the Web service registration dialog box (see Figure 6) you specify a URL that describes the Web service and tells VFP what methods as well as parameters and return values are supported by the service. ASP.NET-based Web services provide a WSDL (Web service Description Language) URL that provides exactly that information. You can find the URL by launching the test bench in Visual Studio .NET (click F5), to launch the service test bench start page. At the very top of the page there is a link to the "Service Description" of the Web service. In our example, the URL is similar to the following:

http://localhost/ExampleService/ Service1.asmx?WSDL

 
Figure 6. Adding a Web service Reference to VFP: The figure shows the process of adding a Web service reference using the Visual FoxPro 9 Task Pane.
Note that in a real-life scenario, you need to replace the host name localhost with the name of the domain the Web service resides on (such as www.codefocus.com).

After you register a Web service with the VFP Task Pane you can test it immediately through the Task Pane Manager. Simply pick the service you would like to test ("Service 1" in our example) and the method you would like to call, and click on the icon next to the method drop-down list. You can see the result in Figure 7. You now know that the Web service works in VFP and you can start using it from within a VFP program. Doing this requires a little bit of code. The good news, however, is that the Task Pane also provides sample code (the bottom left of Figure 7 shows the start of the sample code) that you can use directly by dragging and dropping that code into a source code window. Listing 3 shows code created based on the sample code provided by the Task Pane. Note that Listing 3 contains a lot of code that is not strictly required, particularly the error handling. The important code is the following:

loWSHandler = NEWOBJECT("WSHandler",; IIF(VERSION(2)=0,"",; HOME()+"FFC\")+"_ws3client.vcx") loSvc = loWSHandler.SetupClient(; "http://.../Svc.asmx?WSDL",; "Service1", "Service1Soap") MessageBox(loSvc.GetCurrentTime())

Authors' Note: We shortened the URL to make it more readable. Please replace the URL with the URL of the service you created.

 
Figure 7. Registered Web service: Here's the sample Web service registered in the VFP Task Pane Manager and ready to be tested.
The sample code instantiates the WSHandler object, which is VFP's connector to a Web service, and then configures it with the WSDL URL. Subsequently, the code calls the GetCurrentTime() method, which returns a .NET DateTime variable. VFP automatically assigns the return value to a VFP DataTime variable even though the two formats differ slightly internally. Because the code returns the value as a DateTime you can perform additional operations on it. For instance, you can retrieve the time portion using the following commands:

LOCAL ldCurrentDateTime ldCurrentDateTime = ; loService1.GetCurrentTime() ? TToC(ldCurrentDateTime,2)

Note that automatic type assignment does not happen all the time. It is possible—some would say likely—that the Web service will return a data type not natively supported in Visual FoxPro. This typically happens when the return value is a complex object, such as an ADO.NET DataSet. In that case the return value would be complex XML, which you must parse before VFP can use it. In the case of a DataSet VFP has an XMLAdapter class. (Note: For more information about the XMLAdapter class, see the "What's New with Data in Visual FoxPro 9" article in this issue, or search for "XMLAdapter" on www.code-magazine.com.) For other complex objects, parsing the XML may be a little more complex, but using tools like XMLDOM, it is never overly hard.



Comment and Contribute

 

 

 

 

 


(Maximum characters: 1200). You have 1200 characters left.

 

 

Sitemap
Thanks for your registration, follow us on our social networks to keep up-to-date