Hi! I have a WCF service built on C# hosted on IIS server, I would like to make a client to consume this service on Lazarus, I haven't worked before with Services on Lazarus, so if there's an example or how to i'd appreciate it
I tried this for you, may help others too..
A public webservice is available here to convert temperatures:
http://www.webservicex.net/WS/WSDetails.aspx?WSID=31&CATID=131. To use in Lazarus Webserver Toolkit I copied their WSDL info (Web Service Definition Language) to my local drive, called it convert.wsdl. It is XML and looks like this:
convert.wsdl<?xml version="1.0" encoding="utf-8"?>
<definitions name="http://www.webserviceX.NET/" xmlns="http://schemas.xmlsoap.org/wsdl/" xmlns:tns="http://www.webserviceX.NET/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" targetNamespace="http://www.webserviceX.NET/">
<types>
<xsd:schema xmlns="http://www.w3.org/2001/XMLSchema" xmlns:tns="http://www.webserviceX.NET/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" targetNamespace="http://www.webserviceX.NET/">
<xsd:complexType name="ConvertTemp">
<xsd:sequence>
<xsd:element name="Temperature" type="xsd:double"/>
<xsd:element name="FromUnit" type="tns:TemperatureUnit"/>
<xsd:element name="ToUnit" type="tns:TemperatureUnit"/>
</xsd:sequence>
</xsd:complexType>
<xsd:complexType name="ConvertTempResponse">
<xsd:sequence>
<xsd:element name="ConvertTempResult" type="xsd:double"/>
</xsd:sequence>
</xsd:complexType>
<xsd:simpleType name="TemperatureUnit">
<xsd:restriction base="xsd:string">
<xsd:enumeration value="degreeCelsius"/>
<xsd:enumeration value="degreeFahrenheit"/>
<xsd:enumeration value="degreeRankine"/>
<xsd:enumeration value="degreeReaumur"/>
<xsd:enumeration value="kelvin"/>
</xsd:restriction>
</xsd:simpleType>
<xsd:element name="_double" type="xsd:double"/>
</xsd:schema>
</types>
<message name="ConvertTemp">
<part name="ConvertTemp" type="tns:ConvertTemp"/>
</message>
<message name="ConvertTempResponse">
<part name="ConvertTempResponse" type="tns:ConvertTempResponse"/>
</message>
<portType name="ConvertTemperatureSoap">
<documentation>
<GUID value="{682745A9-8683-42ED-832A-CE9DF2E67FA9}"/>
</documentation>
<operation name="ConvertTemp">
<input message="tns:ConvertTemp"/>
<output message="tns:ConvertTempResponse"/>
</operation>
</portType>
<binding name="ConvertTemperatureSoap" type="tns:ConvertTemperatureSoap">
<soap:binding style="rpc" transport="http://schemas.xmlsoap.org/soap/http"/>
<operation name="ConvertTemp">
<soap:operation soapAction="http://www.webserviceX.NET/ConvertTemp"/>
<input>
<soap:body use="literal" namespace="http://www.webserviceX.NET/"/>
</input>
<output>
<soap:body use="literal" namespace="http://www.webserviceX.NET/"/>
</output>
</operation>
</binding>
<service name="ConvertTemperatureSoap">
<port name="ConvertTemperatureSoapPort" binding="tns:ConvertTemperatureSoap">
<soap:address location="http://www.webservicex.net/ConvertTemperature.asmx"/>
</port>
</service>
</definitions>
2. Create a new Lazarus project (and assuming you have installed the WST design type package) go to Menu/Project/Web_Services_Toolkit/Type_Library_Editor and open the saved convert.WSDL file.
3. The type library editor displays the Lazarus units needed to interact with the webservice, so save them all by selecting "Save Generated Files" from within the type library editor.
4. Include the generated files in your own project
uses clause:
// files generated by wst type library editor --
, convert
, convert_binder
, convert_imp
, convert_proxy
// ---------------------------------------------
5. You will also need these in your
uses clause
// SYNAPSE
, synapse_http_protocol
// WST
, soap_formatter
6. The code to Connect to the conversion service with some parameters looks likes this (requires 1 button & 1 label on the form):
procedure TForm1.Button1Click(Sender: TObject);
var myConvertClient : ConvertTemperatureSoap ; // connection
myConvertTemp_Type : ConvertTemp_Type ; // parameter in
myResult : ConvertTempResponse ; // result out
begin
myConvertClient := wst_CreateInstance_ConvertTemperatureSoap( ) ;
myConvertTemp_Type := ConvertTemp_Type.Create() ;
// prepare: convert zero degrees celcius to farenheit
myConvertTemp_Type.Temperature := 0.0 ;
myConvertTemp_Type.FromUnit := degreeCelsius ;
myConvertTemp_Type.ToUnit := degreeFahrenheit ;
// perform the conversion
myResult := myConvertClient.ConvertTemp( myConvertTemp_Type );
// display the result (should = 32)
Label1.Caption := FloatToStr( myResult.ConvertTempResult ) ;
end;
7. Your application will need to register HTTP Transport e.g. on the form create or initialization.
procedure TForm1.FormCreate(Sender: TObject);
begin
// WST
SYNAPSE_RegisterHTTP_Transport();
end;
8. Your project will need the source for WST and in this case synapse, but you could also use other (e.g. Indy). So inlcude the paths to the source in your project source path:
..\third_party\wst;..\third_party\synapse40\source\libI will zip and include the source for this project, you will need to modify these paths to suit your own environment.
9 . Here is the entire code:
unit main;
{$mode objfpc}{$H+}
interface
uses
Classes, SysUtils, FileUtil, Forms, Controls, Graphics, Dialogs, StdCtrls
// SYNAPSE
, synapse_http_protocol
// WST
, soap_formatter
{
, binary_formatter
, json_formatter
, xmlrpc_formatter
}
// files generated by wst type library editor --
, convert
, convert_binder
, convert_imp
, convert_proxy
// ---------------------------------------------
;
type
{ TForm1 }
TForm1 = class(TForm)
Button1: TButton;
Label1: TLabel;
procedure Button1Click(Sender: TObject);
procedure FormCreate(Sender: TObject);
private
{ private declarations }
public
{ public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.lfm}
{ TForm1 }
procedure TForm1.Button1Click(Sender: TObject);
var myConvertClient : ConvertTemperatureSoap ; // connection
myConvertTemp_Type : ConvertTemp_Type ; // parameter in
myResult : ConvertTempResponse ; // result out
begin
myConvertClient := wst_CreateInstance_ConvertTemperatureSoap( ) ;
myConvertTemp_Type := ConvertTemp_Type.Create() ;
// prepare: convert zero degrees celcius to farenheit
myConvertTemp_Type.Temperature := 0.0 ;
myConvertTemp_Type.FromUnit := degreeCelsius ;
myConvertTemp_Type.ToUnit := degreeFahrenheit ;
// perform the conversion
myResult := myConvertClient.ConvertTemp( myConvertTemp_Type );
// display the result (should = 32)
Label1.Caption := FloatToStr( myResult.ConvertTempResult ) ;
end;
procedure TForm1.FormCreate(Sender: TObject);
begin
// WST
SYNAPSE_RegisterHTTP_Transport();
end;
end.