Home > Articles > Programming > C#

C# in Action - Part III: Creating a Web Service

  • Print
  • + Share This
The newest buzzword in distributed Internet computing is "Web services." Joe Mayo shows here how to create a Web service for his example C# investment calculation program.
From the author of

From the author of

The newest buzzword in distributed Internet computing is "Web services." Several companies—notably, IBM, Microsoft, and Sun—have put different marketing twists on what Web services are, but, in my opinion, it all boils down to one thing: Web services are distributed software components that provide reusable functionality, using standard Internet protocols.

The primary architectural difference in this program is that the implementation of the EstimateReturn() method has been gutted and replaced with a call to a Web service that provides the same functionality. The Investment Info program in this article has the same user interface as Part II. Click here for the entire listing.

Creating a Web Service

This article describes how to create an ASP.NET Web service, created with Visual Studio.NET. To start a new Web service in Visual Studio.NET, select New, Project from the File menu, and click on the ASP.NET Web Service icon in the explorer window that appears. I gave my Web service a Name of InvestmentService at Location http://localhost/InformIT. You can create any location you want below localhost, but it's easier to follow the examples if the name is InvestmentService. The following code shows the Web service class declaration:

using System;
using System.Web;
using System.Web.Services;

namespace InvestmentService
{
   /// <summary>
   /// Summary description for InvestmentService.
   /// <summary>
   [WebService(Namespace="http://csharp/InformIT/InvestmentService")]
   public class InvestmentService : System.Web.Services.WebService
   {
     . . .
   }

Pertinent namespaces for a Web service are System.Web and System.Web.Services. The WebService class inherits the System.Web.Services.WebService class, which provides all the behind-the-scenes plumbing necessary for this Web service to operate. The plumbing that I'm referring to is the network communication that receives XML/SOAP method calls from a client using HTTP, unmarshals incoming data, invokes functions, and marshals responses back to XML/SOAP so that they can be transmitted via HTTP back to the client.

There's a WebService attribute just above the InvestmentService class declaration. Attributes are a part of the C# language that enables decoration of program elements with additional metadata. This metadata can be read at runtime and used to support various late bound operations. This particular attribute is used to add a namespace declaration to the XML/SOAP packet generated by this Web service.

Web services have methods that are similar to any other class method. The difference is that they have WebMethod attributes, making them publicly accessible. Any methods that don't have a WebMethod attribute are not part of the Web service's public interface. The following code shows how to expose Web service functionality.

     [WebMethod]
     public decimal EstimateReturn(decimal principal, float interest, int months)
     {
        return principal*(decimal)Math.Pow((double)(1+interest/12), (double)months);
     }

This is nearly the same method as the EstimateReturn() method in the InvestmentController class from Part I and Part II. The difference is that this method must be passed three parameters.

  • + Share This
  • 🔖 Save To Your Account