Intro
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:
using System;
using System.Web.Services;
public class DEMOAddNumbers : WebService
{
[WebMethod]
public int AddThis(int x, int y)
{
int mySum;
mySum = x + y;
return mySum;
}
}
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? “

{ 8 comments… read them below or add one }
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
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
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;
…
Rich » Hi Rich – thanks for the tip. I guess I need to find a wordpress plugin that solves this issue. Cheers~Joe
<%@ WebService Language=”c#” Class=”DEMOAddNumbers”%>
Hi Joe,
I can not see what is in the web.config file.
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
kim » Sorry about that, I mistakenly deleted a bunch of files on my website. I will re-post once I find it.
{ 3 trackbacks }