Over the past few months, I have spoken to many, many
developers about the XML Web Services model. While many have
immediately seen its value and started building a new breed of
applications, I find others are somewhat reluctant to make the
switch to this new model because of a perceived learning
curve. Usually they assume they need to ramp up on Internet
protocols in general and XML and SOAP in particular before
they can begin writing XML Web Services. Fortunately,
Microsoft ASP.NET shields you from all these details.
To see for yourself, imagine you have an existing
application you want to turn into a Web Service. To keep
things simple, let's assume that it is a console application
that takes two command line arguments and prints out the
sum.
Listing 1 shows the source code in Microsoft C#. Even
if you haven't seen a single line of Microsoft C# code before,
you should be able to follow this example, so I won't walk
through it. (To learn about Microsoft C#, turn to Tom Archer's
Microsoft Press� book Inside
C#.) As always, I have placed the source code for this on
my
Web site.
Listing 1 Console version of
MyMath
using MyMath;
class Host{ public static void Main() {
// Declare our
variables string[]
args; int
i; double
d;
// Get command line
args args =
System.Environment.GetCommandLineArgs()
;
// Instantiate My Class
MyMath.Calc oCalc = new
MyMath.Calc();
// Call the add method after converting
// the string args to
int i =
oCalc.Add(System.Convert.ToInt32(args[1]),
System.Convert.ToInt32(args[2])) ;
// Call the add method after converting
// the string args to double d =
oCalc.Divide(System.Convert.ToDouble(args[1]),
System.Convert.ToDouble(args[2])) ;
// Print results to
console
System.Console.WriteLine(System.String.Concat("Add says: ", i))
;
System.Console.WriteLine(System.String.Concat("Divide says: ",
d));
} }
namespace MyMath { public class Calc{
//Add
Method public int Add (int
x, int y )
{
return x + y; }
// Divide
Method public double Divide
(double x, double y)
{ //
Not ready for prime
time!
// No handling for divide by
zero
return x / y ;
} } }
To turn this C# application into a Microsoft ASP.NET Web
Service all you need to do is copy the Calc class as shown in
Figure 1 and paste it into a new file with an .asmx extension;
I called mine NewMath.asmx.
 Figure 1: To create an XML
Web Service, you start off by simply defining a class, and
ASP.NET will do nearly everything else for you.
Next you have to add a directive line at the top of
the file so that Microsoft ASP.NET knows how to process the
file. You also need to import the System.Web.Services library.
<%@ WebService
Language="C#" class="Calc" %> using
System.Web.Services;
Finally you have to tag the methods as Web Methods so that
Microsoft ASP.NET knows to expose them to clients. This is
done by adding [WebMethod]
directive right above the method itself. On the other hand, if
you do not want to expose a particular method�for example the
Divide method, which has an intentional bug in it�you would
simply remove the [Web
Method] tag. For now, leave the bug in. If you have the
.NET Framework installed, download the code, enter zero as the
divisor, and see what happens. If you don't have the .NET
Framework, you can download it from the Microsoft
.NET Framework site. The final source code looks like
this:
<%@ WebService Language="C#"
class="Calc" %>
using System.Web.Services;
public class Calc {
//Add Method [ WebMethod
] public int Add (int x, int y )
{ return x +
y; }
// Divide Method [
WebMethod ] public double Divide (double x,
double y) { // Not ready for
prime time! // No handling
for divide by zero. return x
/ y ; }
}
Counting the comments and the pretty formatting, you have
written an XML Web Service in 20 lines of code. If you
are keeping score, the console app required 49 lines. (Yes, I
realize this sample needs some error checking and input
validation, but in the interest of space I have decided to
leave it out. Remember, this is sample code and not template
code.) Now that you are feature complete on your first ASP.NET
Web Service, fire up Microsoft Internet Explorer and see what
it looks like. Your Internet Explorer window should look
similar to Figure 2.
 Figure 2: Your first ASP.NET Web
Service, complete with built-in test harness.
You will notice three things about the output. First, it
has created a customized test harness for you so that you can
exercise your Web Service. Microsoft ASP.NET has automatically
provided links to each of the methods you tagged as Web
Methods. Second, Microsoft ASP.NET has noticed a problem with
your Web Service and provided a recommended solution. Third,
it has given you a link where you can inspect the Web Service
Description Language (WSDL). Let's look at each of these items
in more detail. |