Mega Code Archive

 
Categories / Delphi / Ide Indy
 

Simple Web Service in Delphi 8 (Part 1)

Title: Simple Web Service in Delphi 8 (Part 1) Question: How do you create a simple web service in Delphi 8? Answer: Creating your first web service in Delphi 8 is extremely easy. By using the wizards in Delphi 8 you can have your first service up and running in minutes. For this example I will created a service called Math. In Delphi 8 you need to Select File = Other = Delphi ASP Projects = ASP.NET Web Service Application =OK You will be prompted for an Application name type Math click OK! Webserve1.asmx screen will be displayed click on the label switch to code view Create a public function called Add it will take to two integer parameters and returns an integer. Add the attribute [WebMethod] to the function. This tag [WebMethod] will make the function available to clients of your web service. unit WebService1; interface uses System.Collections, System.ComponentModel, System.Data, System.Diagnostics, System.Web, System.Web.Services; type TWebService1 = class(System.Web.Services.WebService) {$REGION 'Designer Managed Code'} strict private components: IContainer; procedure InitializeComponent; {$ENDREGION} strict protected procedure Dispose(disposing: boolean); override; private { Private Declarations } public constructor Create; [WebMethod] function Add(a,b:integer): integer; end; implementation [WebMethod] function TWebService1.Add(a,b:integer): integer; begin Result := a + b ; end; Now save your work and press F9 to run you webservice. When you do this IE will be launch and you webservice default page will be displayed. This page will list all the Web Methods declared in your service. Click on the add method a page will display that will allow you to input the parameters and click on invoke. You have a webservice that will add two numbers and return the sum in XML. There you have it your first Webservice.