Simple Delphi Prism WCF service and Delphi 2009 client

Posted by on in Blogs
It has been just few days since Delphi Prism May 2009 has been released. One of the most visible new things is its support for AOP through Cirrus framework. Jim McKeeth demonstrated Delphi Prism AOP demo during the recent DelphiLive! conference and it was enlightning experience to see how aspects are modifying code during the compilation time. The AOP is very interesting.

Yesterday I have been presenting ".NET Programming with Delphi Prism" webinar. Of course AOP was the first demo, but I also thought that it would be cool to adapt my old Delphi for .NET WCF demos to Delphi Prism. It did not take me long to have a simple Delphi Prism WCF "Simple Calculator" demo application up and running.

The WCF architecture is a natural evolution of the first-generation web services. It decouples XML messaging from the underlying communication protocol and offers additional message exchange patterns. It is possible to build WCF services in such a way that they can be accessed by traditional Web Services clients, including Delphi 2009 VCL Forms applications. WCF services can be hosted (for example in IIS) or self-hosted (for example in a Console application).

For demo purposes I have decided to host my WCF service in Delphi Prism Console application. Instead of starting from "WCF" template, I have started from an empty Delphi Prism console project. The first thing to do is to add to project references "System.ServiceModel" assembly where most of the WCF types are defined.

In the second step I have added to my project two units for service interface definition ("SimpleCalcIntf.pas") and for the actual service implementation class ("SimpleCalcImpl.pas").

Note that the interface is decorated with "[ServiceContract]" and methods with "[OperationContract]" attributes defined in the "System.ServiceModel" namespace.

The WCF service implementaion class is trivial. Note that it is enough to decorate interface with WCF attributes. The implementation class does not need them.

That's our WCF service. In the WCF architecture a service can be access only through WCF endpoints. You can define endpoints either declaratively with configuration file or in code. For demo purposes I'm going to use the second approach. The last part of the WCF application is hosting code that I'm going to place directly inside the program's "main" method.

If you now run this program you should see the following screen.

Now it you type into the browser the service URL, it should display a test page with the URL for the WSDL file that you need as a staring point for building web service client to it.

In order to import a web service into Delphi 2009 you need to use "WSDL Importer" from the "New Items" dialog and specify the URL of the WSDL file that descibes the service. In our case it is http://localhost:8080/DelphiPrism/?wsdl. The detailed steps for building a client application can be found here.

The source code for this demo project can be downloaded from CodeCentral.

Gold User, Rank: 9, Points: 364
Crazy about Delphi Programming!


Check out more tips and tricks in this development video: