How to test a web service series
- Part 1 – How to Test Web Service – How to Create a Web Service
- Part 2 – How to Test a Web Service – It’s all about the WSDL
- Part 3 – How to Test a Web Service: Response & Requests
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.
Why create a web service?
If you’re anything like me, the best way for you to learn is by actually doing. I’ve found that, to understand how something works it sometimes helps to actually try it yourself –so I devised a simple example of how to create a web service to help teach some of the concepts of web service testing.
Create a Web Service
Now we will create a simple service that contains one method that adds two numbers together. Okay… I admit it’s not the coolest web service, but it will serve our purpose. Before you start, make sure you have the following setup:
- Microsoft’s Internet Information Services (IIS) is up and running on your machine
- The latest .Net Frmaework 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:
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?