Part 1 – How to Test Web Service – How to Create a Web Service

Automation Guild Online Conference Top ad

How to test a web service series

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:

http://localhost/WebServices/DEMOAddNumbers.asmx/AddThis?x=40&y=2

The page should return the value of 42 (Which by the way is also the answer to the meaning of life – hehe)


Conclusion:
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?

18 comments
How to test web services – Intro — Joe Colantonio -- QuickTest Pro, Tech Geek, Bibliomanic, The Software Test Automation Imagineer - April 5, 2011

[…] Part 1 – How to Test Web Service – How to Create a Web Service Tweet […]

Reply
Part 2 – How to Test a Web Service – Its all about the WSDL — Joe Colantonio -- QuickTest Pro, Tech Geek, Bibliomanic, The Software Test Automation Imagineer - April 13, 2011

[…] you are following along and have read Part One of this series, you should now have a working web […]

Reply
Josh - April 19, 2011

Hi Joe,
I’m having some issues trying to get this to work. I’ve followed the instructions, but i’m getting the following error when i try to navigate to the service in the browser.
Server Error in ‘/’ Application.
Parser Error
Description: An error occurred during the parsing of a resource required to service this request. Please review the following specific parse error details and modify your source file appropriately.

Parser Error Message: The directive is missing a ‘class’ attribute.

Source Error:

Line 1:
Line 2: using System;
Line 3: using System.Web.Services;

Source File: /WebServices/DEMOAddNumbers.asmx Line: 1

Version Information: Microsoft .NET Framework Version:2.0.50727.4927; ASP.NET Version:2.0.50727.4927

I have the .NET sdk 2.0
installed and IIS 7 on windows 7. I created the directory and the files in the location you specified. Any help would be appreciated.
Thanks,
Josh

Reply
    Joe Colantonio - April 19, 2011

    Hi Josh,

    Can you email me your DEMOAddNumbers.asmx file to joecolantonio@gmail.com. I have only tried this on a Windows XP machine — i will also try on a Win7 machine and let you know if it works for me. Thanks ~Joe

    Reply
Rich - August 19, 2011

the first line (the compiler directive) will be missing if you have copy and pasted from this page – if you do a view source on this page and look for “using System;” you’ll see the preceeding line which you need to include in your source file:

using System;

Reply
    Joe Colantonio - August 19, 2011

    Rich » Hi Rich – thanks for the tip. I guess I need to find a wordpress plugin that solves this issue. Cheers~Joe

    Reply
Rich - August 19, 2011

<%@ WebService Language=”c#” Class=”DEMOAddNumbers”%>

Reply
dao - April 2, 2013

Hi Joe,
I can not see what is in the web.config file.

Reply
kim - May 2, 2013

Hello Joe,

May i know what is the content/input for web.config file? The above post seems to be taken out. I am not able to view at all :(

Appreciate your help. Thank you

Reply
Taran - October 2, 2014

When i launched IE and pasted the url, i received an error.
404 page not found.
What should i do next? Where can i verify this?

Reply
Andre - January 29, 2015

the same here.

Reply
Ike Ugochuku - November 13, 2016

Got the same 404 page not found. My environment
Windows 2012 R2
IIS 8.5

Fix: Go to Add Roles and features and select
Roles\Security
-Basic Authentication

Roles\Application Development
-Application Initialization
-ASP
-ASP.NET 3.5
-ASP.NET 4.5

Features\.NET Framework 4.5\WCF Services
-Http Activation

Reply
srinu - February 17, 2017

thanks giving this information

Reply
Click here to add a comment

Leave a comment: