Ok to get started – lets create our very own web service. We will then use the web service we created as the bases for the rest of this series. When we start testing and looking at the WSDL and the services request, and response the information should make more sense since we will be able to map the values back to the source where they came from.
Create a Web Service
Now we will create a simple service that contains one method that adds two numbers together. Ok I admit this I not the sexist web service but it will serve our purpose. Before you start make sure you have the following setup:
- IIS is up and running on your machine
- The latest .Net SDK is installed
- Create a directory under your C:\Inetpub\www.root named WebServices (wwroot image)
Next go ahead and copy the following code into notepad:
public class DEMOAddNumbers : WebService
public int AddThis(int x, int y)
mySum = x + y;
Basically the first line s saying that this a a web service and the language used is C#. Then we import two namespaces System and System.Web.Services. Next we create our class DEMOAddNumbers and add our method named AddThis that accepts two parameters of type integer -- x and y. Finally we add the values together and return a integer value mySum that contains the sum of the passed parameters.
Save the file as DEMOAddNumbers.asmx and place it under your wwwroot/WebServices directory. FYI asmx is the file extension used for ASP.NET.
Create a web.config file
To get this to work on my machine I also had to add a web.config file to my C:\Inetpub\wwwroot directory. Open notepad and copy the following:
name the file "web.config" and save it under C:\Inetpub\wwwroot
Verify that the web service works
Next open up your browser and enter the following address:
The page should return the value of 42 (Which by the way is also the answer to the meaning of life – hehe)
Sweet – we now have a working web service! In Part 2 of this series we will us this web service to help answer the question “what is a WSDL? “