Creating the Web Service
- Go to the File menu, New and click New Web Site.
- In the text box where is defined the location (ex: http://localhost/WebSite ), change "WebSite" to the name that you want for the Web Service. I'm going to use, PoolService, so, "http://localhost/PoolSerives".
- Select Visual C# in the installed templates, and proceed to select ASP.NET Web Service. Click OK
Now if you have IIS working and you press CTRL + F5, something like this should appear. HelloWorld is a method from your, just created, web service.
Now to add some more methods...
Now to add some more methods...
Adding Methods to the Web Service
What we have now is a web service called "Service". Let's add another web service.
- In Solution Explorer, right-click the Web site name (http://localhost/PoolServices), and then click Add New Item.
- Under the Visual C# Installed Templates, select Web Service, and change the name. I'm going to use the name Calc.asmx
- Make sure you don't forget to check the box Place code in separate file, and click Add.
- Now open the Calc.cs file and add this code inside the Class Calc, for exemple after the HelloWorld method:
- Now if you press CTRL + F5 again you should see the "Add" method next to the "helloworld" method. If you click it, you should see two text boxes. This text boxes represent the two variables that we require for the "add" method, and if you invoke the method after typing the numbers, a xml file will appear with the result.
- You can add more methods, for example, add a method to subtract the numbers. just below the add method (don't forget that every method requires writing [WebMethod] before his definition).
Adding a Web Service to another Project
Now the cool part. Now that we have a web service with one method (or more), we can use it in another projects giving real use to the Web Service. Go to the File menu, New and click New Web Site.
- Go to the File menu, New and click "Project..."
- On C# installed Templates, select Windows Forms Application, give it a name and click OK.
- With the Design layout add 2 textbox, 1 button and a label (you can find them in the Toolbox).
- With the help of the Properties window change the name of the button to "addBtn".
- Now, go to the Solution Explorer, right-click the name of the project, and then click "Add Service Reference...".
- In the address textbox insert the path to your webservice. For exemple: "http://localhost/PoolServices/Calc.asmx" click Go to test it, and it should appear your service in the "Services:" zone. by expanding the service found, you can see his methods on the "Operations:" zone. Define the namespace for the Service (ServiceCalc for example) and click OK.
- To add the code in the form double click on the button that was just created with the name "addBtn". Add this code to the addBtn_Click method:
- CTRL + F5 and it should work :)