Wednesday, February 27, 2008

basics of web services in C#, VS 2005

This post is dedicated to Jesse Liberty. I’ve been reading his books for a long time and I have to say, I love the way he breaks things down so simply. The sample code used in this simple tutorial is from his book called Programming ASP.NET.

There are two main parts to Web Services. First, create the web service. Second, consume the web service.

1. Have IIS installed on your windows machine
2. Visual Studio.NET 2005

Step one (Create a new Virtual Directory using IIS):
Open up Windows Explorer => right click on My Computer => choose Manage => open Services and Applications=> Open Internet Information Services=> Open Web Sites =>right click on Default Web Site and choose New Virtual Directory
Give the virtual directory a name: For this example, I am naming the Alias RequestAQuote
Give the virtual director a location: For this example, I have created a directory C:\RequestAQuote and I am mapping the virtual directory to this location. I am giving the permissions Read and Run scripts

Step two (Create the .asmx file that has the web services method stubs):
Open a text file and then save it with extension .asmx
This file I have named RequestAQuote.asmx
<%@ WebService Language="C#" Class="ProgAspNet.RequestAQuote" %>

using System;
using System.Web.Services;

namespace ProgAspNet
//This example can be found in Programming ASP.NET by Jesse Liberty and Dan Hurwitz
//I have modified this sample in trivial ways
public class RequestAQuote : System.Web.Services.WebService
// Construct and fill an array of stock symbols and price.
// Note: the stock prices are of 5/1/05
string[,] stocks =
{"DELL","Dell computers", "34.83"},
{"HPQ","Hewlett Packard", "20.47"},
{"YHOO","Yahoo!", "34.50"},
{"GE","General ElectricDell computers", "36.20"},
{"IBM","International Business Machine","76.38"},
{"GM","General Motors", "26.68"},
{"F","Ford Motor Company", "9.11"}

public string GetAvailableStockNames()
string str = string.Empty;

for (int i = 0; i < stocks.GetLength(0); i++)

for (int x = 0; x < stocks.GetLength(1); x++)
str = str + "" + stocks[i, x];

str = str + "" + Environment.NewLine;

return str;


Step 4 (Go to the website created by this file and test methods):
You should get links to the available functions. Double click on them and test the methods.

Step 5 (Write a Client):
Click on References in VS2005 in the solution explorer. Add the Web reference URL http://localhost/RequestAQuote/RequestAQuote.asmx
Start up Visual Studio 2005 and start a new project. Create an ASP.NET Web Application. Navigate to the Default.aspx.cs file under Default.aspx to actually code the project.
In this section add this code:
namespace Requestquotes
public partial class _Default : System.Web.UI.Page
localhost.RequestAQuote request = new Requestquotes.localhost.RequestAQuote();

protected void Page_Load(object sender, EventArgs e)


protected void btnButton1_Click(object sender, EventArgs e)
string strOfAvailableNames = request.GetAvailableStockNames();
Label1.Text = strOfAvailableNames;


Then in the file named Default.aspx add the following code:

a. Create a button in asp so that when you click on it btnButton1_Click is called.

b. Create a label in asp so that all of the text from the web services method called by btnButton1_Click can output to this label

Here is a picture of this code

Basics of Web Services on windows using C#.NET VS2005

1 comment:

Anonymous said...

Hello. This post is likeable, and your blog is very interesting, congratulations :-). I will add in my blogroll =). If possible gives a last there on my blog, it is about the Home Broker, I hope you enjoy. The address is A hug.