|Delphi Clinic||C++Builder Gate||Training & Consultancy||Delphi Notes Weblog||Dr.Bob's Webshop|
Delphi 2010 and WCF Clients
In this article, I'll demonstrate how we can use Delphi 2010 to import and consume WCF (Windows Communication Foundation) applications (written using Delphi Prism in the June 2009 column).
WCF Demo with Delphi Prism
A couple of months ago, I wrote a simple WCF Demo using Delphi Prism, which was deployed on my www.bobswart.net server in a virtual directory called Deployment. The URL to the WCF Demo is http://www.bobswart.net/Deployment/MoneyService.svc
If you visit that URL, you'll see that it mentions a WSDL URL of the following format: http://trinity/Deployment/MoneyService.svc?wsdl, which is interesting since it s using the name of my machine (trinity) instead of the domain. A better URL for the WSDL is http://www.bobswart.net/Deployment/MoneyService.svc?wsdl which also works for people outside of my local network.
Importing the WCF Demo
Start a new Delphi 2010 VCL Forms Application project. Do File | New - Other, and in the Object Repository go to the WebServices category and double-click on the WSDL Importer icon. The WSDL Importer wizard will now start, where we can paste the URL to the WSDL, which is http://www.bobswart.net/Deployment/MoneyService.svc?wsdl.
Click on Next,
Next and finally on Finish to produce a MoneyService.pas import unit.
This generated unit represents the WCF Demo service, although we must make on change to really make it work.
Inside the MoneyService.pas unit, you can find the definition as well as the implementation of the function GetIMoneyService. The implementation contains four string constants: defWSDL, defURL, defSvc and defPrt. The value for defURL is not correct, since it still points to http://trinity/Deployment/MoneyService.svc. We should manually change that to the right URL, as follows:
function GetIMoneyService(UseWSDL: Boolean; Addr: string; HTTPRIO: THTTPRIO): IMoneyService; const defWSDL = 'http://www.bobswart.net/Deployment/MoneyService.svc?wsdl'; //defURL = 'http://trinity/Deployment/MoneyService.svc'; defURL = 'http://www.bobswart.net/Deployment/MoneyService.svc'; defSvc = 'MoneyService'; defPrt = 'BasicHttpBinding_IMoneyService';
That way, the defURL will point to the actual deployed WFC service on my www.bobswart.net server.
Delphi 2010 WCF Client
We can now write a few lines of code to use the MoneyService, by creating an instance of the IMoneyService interface and then calling the Num2Word method, passing the contents of the TEdit as argument:
We can also pass the Money2Word method, passing a hardcoded 1234567 Euro and 42 cents, resulting in a full string with the money details.
procedure TForm7.Button1Click(Sender: TObject); var Amount: MoneyContract; begin Amount := MoneyContract.Create; Amount.Amount := 1234567; Amount.AmountCurrency := 'Euro'; Amount.Cents := 42; Amount.CentsCurrency := 'cent'; ShowMessage( GetIMoneyService.Money2Word(Amount, ' en')); end;
If we run the WCF client, and click on the button, the result is the following:
Which is the correct answer (in case you cannot read Dutch). So with only a few simple steps, we were able to consume a WCF service in Delphi Prism.And since it s hosted on my development server, you are free to play along and try to consume this WCF service for yourself.
In this article, I've continued with Windows Communication Foundation using Delphi Prism and Delphi 2010, by consuming the WCF Service Library, hosted in IIS. We had to make a minor change in the generated import unit, but other than that, Delphi 2010 is capable of working with the WCF service without problems.
For more recent information on this topic, check out my Delphi 2010 XML, SOAP & Web Services courseware manual.
Pawel Glowacki - Simple Delphi Prism WCF service and Delphi 2009 client