Section 15.7. Building a Web Service


15.7. Building a Web Service

To illustrate the techniques used to implement a web service in C# using the services classes of the .NET Framework, build a simple calculator and then make use of its functions over the Web.

Begin by specifying the web service. To do so, define a class that inherits from System.Web.Services.WebService. The easiest way to create this class is to open Visual Studio and create a new C# web site. In the Templates section choose ASP.NET Web Service and name your web service CalculatorWS, as shown in Figure 15-18.

Figure 15-18. Creating a web service


Visual Studio .NET creates a skeleton web service and even provides a web service example method for you to replace with your own code, as shown in Example 15-3.

Example 15-3. Skeleton web class generated by Visual Studio .NET
using System.Web; using System.Web.Services; using System.Web.Services.Protocols; [WebServiceBinding(ConformanceClaims=WsiClaims.BP10, EmitConformanceClaims = true)] public class Service : System.Web.Services.WebService {     [WebMethod]     public string HelloWorld( ) {         return "Hello World";     } }

To flesh out the calculator, replace the HelloWorld method with five other methods: Add(), Sub( ), Mult(), Div( ), and Pow( ). Each takes two parameters of type double, performs the requested operation, and then returns a value of the same type. For example, here is the code for raising a number to some specified power:

public double Pow(double x, double y) {    double retVal = x;    for (int i = 0;i < y-1;i++)    {       retVal *= x;    }    return retVal; }

To expose each method as a web service, you simply add the [WebMethod] attribute before each method declaration:

[WebMethod]

You aren't required to expose all the methods of your class as web services. You can pick and choose, adding the [WebMethod] attribute only to those methods you want to expose.

That's all you need to do; .NET takes care of the rest.

WSDL and Namespaces

Your web service will use a WSDL XML document to describe the web-callable end points. Within any WSDL document, an XML namespace must be used to ensure that the end points have unique names. The default XML namespace is http://tempuri.org, but you will want to modify this before making your web service publicly available.

You can change the XML namespace by using the WebService attribute:

[WebService(Namespace=     "http://www.LibertyAssociates.com/webServices/")]

There is no expectation that there will be a document at this URL; URLs are used because they are a convenient source of unique names.


Example 15-4 shows the complete source code for the calculator web service.

Example 15-4. Calculator web service program
using System.Web; using System.Web.Services; using System.Web.Services.Protocols; [WebServiceBinding(ConformanceClaims=WsiClaims.BP10, EmitConformanceClaims = true)] public class Service : System.Web.Services.WebService {    [WebMethod]    public double Add( double x, double y )    {       return x + y;    }    [WebMethod]    public double Sub( double x, double y )    {       return x - y;    }    [WebMethod]    public double Mult( double x, double y )    {       return x * y;    }    [WebMethod]    public double Div( double x, double y )    {       return x / y;    }    [WebMethod]    public double Pow( double x, double y )    {       double retVal = x;       for ( int i = 0; i < y - 1; i++ )       {          retVal *= x;       }       return retVal;    } }

15.7.1. Testing Your Web Service

If you invoke the browser by running the program in Visual Studio .NET, you will see an automatically generated, server-side web page that describes the web service, as shown in Figure 15-19. This page offers a good way to test your web service.

Figure 15-19. Viewing the web service test page


Clicking a method brings you to a page that describes the method and allows you to invoke it by typing in parameters and pressing the Clicking button. Figure 15-20 illustrates.

Figure 15-20. Testing the Pow( ) web service method


If you type 3 into the first value field, 4 into the second field, and click Invoke, you will have asked the web service to raise 3 to the fourth power. The result is an XML page describing the output, as shown in Figure 15-21.

Figure 15-21. Invoking the Pow( ) method


15.7.2. Viewing the WSDL Contract

A lot of work is being done for you automatically. HTML pages describing your web service and its methods are generated, and these pages include links to pages in which the methods can be tested.

All web services can be described in WSDL files. You can see the WSDL document by appending ?WSDL to the web service URL, like this:

http://localhost:19975/CalculatorWS/Service.asmx?wsdl

The browser displays the WSDL document, as shown in Figure 15-22.

Figure 15-22. Viewing the WSDL


The details of the WSDL document are beyond the scope of this book, but you can see that each method is fully described in a structured XML format. This is the information used by SOAP to allow the client browser to invoke your web service methods on the server.



Programming C#(c) Building. NET Applications with C#
Programming C#: Building .NET Applications with C#
ISBN: 0596006993
EAN: 2147483647
Year: 2003
Pages: 180
Authors: Jesse Liberty

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net