Mega Code Archive

 
Categories / Delphi / Ide Indy
 

Simple Web Service in Delphi 8 (Part 2)

Title: Simple Web Service in Delphi 8 (Part 2) Question: How can you create a simple client for a simple web service and use it to debug the web service. Answer: In Simple Web Service in Delphi 8 (Part 1) we created a simple web service to add to integers and return the sum. Now lets create a client to use this web service. In Delphi 8 you need to: Select File = WinForms Forms Application Your new form appears in the forms designer. Press F11 in the Object Inspector change the text to Simple Web Service Client. Press Alt+Ctrl+P this brings up the tool palette select the Windows Forms tab. (1) Drop 2 TextBox , 2 Label and a Button on the form. Line the 2 tedit up vertical and set the text to blank. (2) Place label1 between TextBox1 and TextBox2 set the text to + . (3) Place label2 below TextBox2 set the text to blank. (4) Place the button below Label2 and change the text to add. procedure TWinForm.InitializeComponent; begin // // TextBox1 // Self.TextBox1.Location := System.Drawing.Point.Create(72, 48); Self.TextBox1.Name := 'TextBox1'; Self.TextBox1.TabIndex := 0; Self.TextBox1.Text := ''; // // TextBox2 // Self.TextBox2.Location := System.Drawing.Point.Create(72, 128); Self.TextBox2.Name := 'TextBox2'; Self.TextBox2.TabIndex := 1; Self.TextBox2.Text := ''; // // Button1 // Self.Button1.Location := System.Drawing.Point.Create(88, 216); Self.Button1.Name := 'Button1'; Self.Button1.TabIndex := 2; Self.Button1.Text := 'Add'; // // Label1 // Self.Label1.Location := System.Drawing.Point.Create(80, 88); Self.Label1.Name := 'Label1'; Self.Label1.TabIndex := 3; Self.Label1.Text := ' +'; // // Label2 // Self.Label2.Location := System.Drawing.Point.Create(80, 168); Self.Label2.Name := 'Label2'; Self.Label2.TabIndex := 4; Self.Label2.Text := ' '; end; Now we need to save our project save it as serviceclient. Now we can add a web reference to our project. To add a web reference we need to Project = Add Web Reference The add a web reference screen is displayed enter the url for the math web service that we created in Simple Web Service in Delphi 8 (Part 1) it should be http://localhost/Math/WebService1.asmx Your webservice default page will be displayed. Click on the button "Add Reference" The add a web reference screen will close and you are back to your form. Now select File = Use Unit and the use unit dialog displays. You will notice that the web reference localhost1.WebService1 is now listed select it and click OK This adds the following line to our form. implementation uses localhost1.WebService1; Now we are ready to write are code for calling the service. Click on button1 and add the following code. procedure TWinForm.Button1_Click(sender: System.Object; e: System.EventArgs); var mathservice : TWebService1; begin mathservice := TWebService1.Create; label2.Text := Convert.ToString( mathservice.Add(Convert.ToInt32(TextBox1.Text), Convert.ToInt32(TextBox2.Text))); end; First we declare a variable of type TwebService1. We create an instance of that class. We use that instance to call a public function in that class called add passing this function two integers. We get the result of that function (the sum of the two numbers we passed) and display it in in the text property of label2 There is a simple web service client. How can we use it to debug the simple web service? From outside of Delphi 8 run the client ServiceClient.exe Verify that you client is working correctly In Delphi 8 open the math web service project by Project =Open c:\inetpub\www.root\Math\Math.bdsproj" Bring up the unit WebService1.pas, locate the function add, set a breakpoint on the line. Result := a + b ; Now compile you project then Run = Attach to Process the process screen appears listing the processes available to attach to. Select the aspnet_wp.exe process then click on attach button Minimize your Delphi. In your client application ServiceClient enter two integers in the editboxes and click Add. Your Delphi 8 should now be blinking on you taskbar. Click on you Delphi and you should see that the webservice has stopped on you breakpoint you can examine your variable change values debug like a normal win32 application. That all there is to setting breakpoints in a web service.