Requirements for this tutorial

Visual Studio 2008 along with .Net framework 3.5

Steps to create your first web service:

With Visual Studio 2008 installed in your system, creating your first web service is just few clicks away.

1. Open Visual Studio 2008

2. File -> New -> Website

3. Select ASP.NET web service

4. Enter a project name and select “Ok”

Ta da, you should see a set a files which is actually your first web service

Here are a little more details on your new web service.

App_Code\Services.cs is the class file that will contain all your web methods.

Services.asmx is your service file and point of contact for calling the web methods.

In this project I would be creating a simple calculator website which uses the web services for calculation. Now we will rename the files to make sense.

I have renamed Services.cs has GenericCalcService.cs and the class name from Service to GenericCalcService. I have renamed the Services.asmx to Calculator.asmx. As soon as you rename the class and the service files make sure that your service file(asmx) points to the new class name.

After modifications your files will look as follows

GenericCalcService.cs

Calculator.asmx



Let us start building the code behind, i.e. the class file for the web service.

Double click on the GenericCalcService.cs file. By default you should be seeing a default web method HelloWorld(). You can start building a web method as a normal method. To convert any method to a web method, you just have to add the “[WebMethod]” decorative tag above the method’s definition as shown below.

You can add normal methods also into this class file which can be called from other web methods, but these methods will not get exposed outside the class file. Now in order to build this simple calculator, I have created four web methods, each takes two double variables as input and returns the answer as a string variable. Also to convert the double value to string I have added a normal method (d2s) to convert incoming double variable to string.

Also before you compile, change the namespace to a unique url. This URL will be used for calling your web service.

[WebService(Namespace = "http://localhost/calculator/")]

[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]

public class GenericCalcService : System.Web.Services.WebService

{

public GenericCalcService()

{

//Uncomment the following line if using designed components

//InitializeComponent();

}

[WebMethod]

public string add(double a, double b)

{

return d2s(a + b);

}

[WebMethod]

public string subtract(double a, double b)

{

return d2s(a – b);

}

[WebMethod]

public string multiply(double a, double b)

{

return d2s(a * b);

}

[WebMethod]

public string divide(double a, double b)

{

return d2s(a / b);

}

public string d2s(double c)

{

return c.ToString();

}

}

Once this is complete right click on this project in the solution explorer and select Build Solution. If there are any errors try and clear the same. Once you have cleared the errors, right click on the asmx page and select “View in Browser”. You should see a web page as shown below.

In this page you can see the available methods. The available methods will not contain the normal methods.

The WSDL Document

Select the “Service Description” link to view WSDL document of your new web service. The WSDL document is a XML document that contains description of the web service and web methods. Each method will contain the request and response tags describing the format of the input and the output. This document will only contain request and response using SOAP1.1, HTTP GET and POST and MIME.

Testing the web service:

For testing the web service, select a method from the service asmx page. This will open a sample default application that can help you to invoke a request and get the response. If you have used the same class and methods I have used above you should see a test page as below. Enter the input values and click on “invoke”. This will invoke a HTTP POST request and display the response from the service.

Built in Test Page:

Result Page:

Congratulations, you have successfully created your first web service using ASP.NET. In the next article I will describe you on how to implement and invoke this web service in real time.

Linq is short for Language Integrated Query. If you are used to using SQL to query databases, you are going to have something of a head start with Linq, since they have many ideas in common. Before we dig into Linq itself, let’s step back and look at what makes SQL different from C#.

Imagine we have a list of orders. For this example, we will imagine they are stored in memory, but they could be in a file on disk too. We want to get a list of the costs of all orders that were placed by the customer identified by the number 84. If we set about implementing this in C# before version 3 and a range of other popular languages, we would probably write something like (assuming C# syntax for familiarity):

List<double> Found = new List<double>();

foreach (Order o in Orders)

if (o.CustomerID == 84)

Found.Add(o.Cost);

Here we are describing how to achieve the result we want by breaking the task into a series of instructions. This approach, which is very familiar to us, is called imperative programming. It relies on us to pick a good algorithm and not make any mistakes in the implementation of it; for more complex tasks, the algorithm is more complex and our chances of implementing it correctly decrease.

If we had the orders stored in a table in a database and we used SQL to query it, we would write something like:

SELECT Cost FROM Orders WHERE CustomerID = 84

Here we have not specified an algorithm, or how to get the data. We have just declared what we want and left the computer to work out how to do it. This is known as declarative or logic programming.

Linq brings declarative programming features into imperative languages. It is not language specific, and has been implemented in the Orcas version of VB.Net amongst other languages. In this series we are focusing on C# 3.0, but the principles will carry over to other languages.

Understanding A Simple Linq Query

Let’s jump straight into a code example. First, we’ll create an Order class, then make a few instances of it in a List as our test data. With that done, we’ll use Linq to get the costs of all orders for customer 84.

class Order

{

private int _OrderID;

private int _CustomerID;

private double _Cost;

public int OrderID

{

get { return _OrderID; }

set { _OrderID = value; }

}

public int CustomerID

{

get { return _CustomerID; }

set { _CustomerID = value; }

}

public double Cost

{

get { return _Cost; }

set { _Cost = value; }

}

}

class Program

{

static void Main(string[] args)

{

// Set up some test orders.

var Orders = new List<Order> {

new Order {

OrderID = 1,

CustomerID = 84,

Cost = 159.12

},

new Order {

OrderID = 2,

CustomerID = 7,

Cost = 18.50

},

new Order {

OrderID = 3,

CustomerID = 84,

Cost = 2.89

}

};

// Linq query.

var Found = from o in Orders

where o.CustomerID == 84

select o.Cost;

// Display results.

foreach (var Result in Found)

Console.WriteLine(“Cost: ” + Result.ToString());

}

}

The output of running this program is:

Cost: 159.12

Cost: 2.89

Let’s walk through the Main method. First, we use collection and object initializers to create a list of Order objects that we can run our query over. Next comes the query – the new bit. We declare the variable Found and request that its type be inferred for us by using the “var” keyword.

We then run across a new C# 3.0 keyword: “from”.

from o in Orders

This is the keyword that always starts a query. You can read it a little bit like a “foreach”: it takes a collection of some kind after the “in” keyword and makes what is to the left of the “in” keyword refer to a single element of the collection. Unlike “foreach”, we do not have to write a type.

Following this is another new keyword: “where”.

where o.CustomerID == 84

This introduces a filter, allowing us to pick only some of the objects from the Orders collection. The “from” made the identifier “o” refer to a single item from the collection, and we write the condition in terms of this. If you type this query into the IDE yourself, you will notice that it has worked out that “o” is an Order and intellisense works as expected.

The final new keyword is “select”.

select o.Cost

This comes at the end of the query and is a little like a “return” statement: it states what we want to appear in the collection holding the results of the query. As well as primitive types (such as int), you can instantiate any object you like here. In this case, we will end up with Found being a List<int>, though.

You may be thinking at this point, “hey, this looks like SQL but kind of backwards and twisted about a bit”. That is a pretty good summary. I suspect many who have written a lot of SQL will find the “select comes last” a little grating at first; the other important thing to remember is that all of the conditions are to be expressed in C# syntax, not SQL syntax. That means “==” for equality testing, rather than “=” in SQL. Thankfully, in most cases that mistake will lead to a compile time error anyway.