This blog explains basic knowledge abut how to create and use web services.
Create WebService
- Open Visual Studio 2010
- Click on New project Link on start page
- New Project Window Will Open
- Select Asp.net Web Application in the template window
- Enter the name of project
- Click on ok button
- Open Solution Explorer of your project
- Right click on project, the pop window will open
- Drag cursor on to add menu, the submenu of add menu will open, click on NewItem or Go to Add->NewItem.
- The AddNew Item Window will open, Select WebService and Rename WebService1.asmx to MyWebService.asmx, then click on ok button.
- The MyWebService.asmx file will appear in the solution explorer, open MyWebService.asmx file
- Declare your own method in this file bellow the HellowWorld function. You can remove the HellowWorld function from the WebService.
For Example : the bellow function generate a random number and returns to it.
[WebMethod(Description = "Get Random Number")]//Description about web methodpublic string GetNumber() // Declaration of Method{Random RM=new Random();return RM.Next(1,100).ToString();}[WebMethod(Description = "Adds Two Number And return result")]public string Add(int firstno, int secondno){double result = firstno + secondno;return result.ToString();}
This is the all about create web service in your project, and then we will discuss about how to use or consume web service in your application.
- Right click on your WebService i.e MyService.asmx and click on Browse With. The Browser with window will open, select your installed explorer, for example select Internet Explorer.
- Copy the URL adder from the URL of browser.
- Right click on your project, click AddServiceReference , the Service Reference Window will open.
- Past the copied URL address in the address textbox of service reference window. click to 'Go' button
- Rename the Namespace as required and then Press ok.
- The reference of the Webservice will be appear in the Service Reference folder in solution explorer.
- Go to the default page of your project, take one label, set the text to 'Random Number'.
- Take another label control set this text as '0' and id as 'lblRandom'.
- Take a button set the text to 'Get ' and write the following code in the click event of the Button.MyWebService.MyService MS = new MyService();
Label1.Text= MS.GetNumber();
No comments :
Post a Comment